fix(medusa): Deleting product prices (#3152)

This commit is contained in:
Kasper Fabricius Kristensen
2023-02-08 07:13:34 -05:00
committed by GitHub
parent 08324355a4
commit 86c87c7b10
5 changed files with 313 additions and 81 deletions

View File

@@ -6,7 +6,7 @@ const { initDb, useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const productSeeder = require("../../helpers/product-seeder")
const { Product, ProductCategory } = require("@medusajs/medusa")
const { ProductCategory } = require("@medusajs/medusa")
const {
ProductVariant,
@@ -20,6 +20,7 @@ const {
simpleProductFactory,
simpleDiscountFactory,
simpleProductCategoryFactory,
simpleRegionFactory,
} = require("../../factories")
const { DiscountRuleType, AllocationType } = require("@medusajs/medusa/dist")
const { IdMap } = require("medusa-test-utils")
@@ -447,7 +448,10 @@ describe("/admin/products", () => {
})
describe("Product Category filtering", () => {
let categoryWithProduct, categoryWithoutProduct, nestedCategoryWithProduct, nested2CategoryWithProduct
let categoryWithProduct
let categoryWithoutProduct
let nestedCategoryWithProduct
let nested2CategoryWithProduct
const nestedCategoryWithProductId = "nested-category-with-product-id"
const nested2CategoryWithProductId = "nested2-category-with-product-id"
const categoryWithProductId = "category-with-product-id"
@@ -455,14 +459,11 @@ describe("/admin/products", () => {
beforeEach(async () => {
const manager = dbConnection.manager
categoryWithProduct = await simpleProductCategoryFactory(
dbConnection,
{
id: categoryWithProductId,
name: "category with Product",
products: [{ id: testProductId }],
}
)
categoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
id: categoryWithProductId,
name: "category with Product",
products: [{ id: testProductId }],
})
nestedCategoryWithProduct = await simpleProductCategoryFactory(
dbConnection,
@@ -496,52 +497,45 @@ describe("/admin/products", () => {
it("returns a list of products in product category without category children", async () => {
const api = useApi()
const params = `category_id[]=${categoryWithProductId}`
const response = await api
.get(
`/admin/products?${params}`,
adminHeaders
)
const response = await api.get(
`/admin/products?${params}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.products).toHaveLength(1)
expect(response.data.products).toEqual(
[
expect.objectContaining({
id: testProductId,
}),
]
)
expect(response.data.products).toEqual([
expect.objectContaining({
id: testProductId,
}),
])
})
it("returns a list of products in product category without category children explicitly set to false", async () => {
const api = useApi()
const params = `category_id[]=${categoryWithProductId}&include_category_children=false`
const response = await api
.get(
`/admin/products?${params}`,
adminHeaders
)
const response = await api.get(
`/admin/products?${params}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.products).toHaveLength(1)
expect(response.data.products).toEqual(
[
expect.objectContaining({
id: testProductId,
}),
]
)
expect(response.data.products).toEqual([
expect.objectContaining({
id: testProductId,
}),
])
})
it("returns a list of products in product category with category children", async () => {
const api = useApi()
const params = `category_id[]=${categoryWithProductId}&include_category_children=true`
const response = await api
.get(
`/admin/products?${params}`,
adminHeaders
)
const response = await api.get(
`/admin/products?${params}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.products).toHaveLength(3)
@@ -555,7 +549,7 @@ describe("/admin/products", () => {
}),
expect.objectContaining({
id: testProductFilteringId1,
})
}),
])
)
})
@@ -564,11 +558,10 @@ describe("/admin/products", () => {
const api = useApi()
const params = `category_id[]=${categoryWithoutProductId}&include_category_children=true`
const response = await api
.get(
`/admin/products?${params}`,
adminHeaders
)
const response = await api.get(
`/admin/products?${params}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.products).toHaveLength(0)
@@ -1594,7 +1587,8 @@ describe("/admin/products", () => {
})
describe("Categories", () => {
let categoryWithProduct, categoryWithoutProduct
let categoryWithProduct
let categoryWithoutProduct
const categoryWithProductId = "category-with-product-id"
const categoryWithoutProductId = "category-without-product-id"
@@ -1620,12 +1614,15 @@ describe("/admin/products", () => {
const payload = {
title: "Test",
description: "test-product-description",
categories: [{ id: categoryWithProductId }, { id: categoryWithoutProductId }]
categories: [
{ id: categoryWithProductId },
{ id: categoryWithoutProductId },
],
}
const response = await api
.post("/admin/products", payload, adminHeaders)
.catch(e => e)
.catch((e) => e)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
@@ -1649,16 +1646,18 @@ describe("/admin/products", () => {
const payload = {
title: "Test",
description: "test-product-description",
categories: [{ id: categoryNotFoundId }]
categories: [{ id: categoryNotFoundId }],
}
const error = await api
.post("/admin/products", payload, adminHeaders)
.catch(e => e)
.catch((e) => e)
expect(error.response.status).toEqual(404)
expect(error.response.data.type).toEqual("not_found")
expect(error.response.data.message).toEqual(`Product_category with product_category_id ${categoryNotFoundId} does not exist.`)
expect(error.response.data.message).toEqual(
`Product_category with product_category_id ${categoryNotFoundId} does not exist.`
)
})
it("updates a product's categories", async () => {
@@ -1668,8 +1667,11 @@ describe("/admin/products", () => {
categories: [{ id: categoryWithoutProductId }],
}
const response = await api
.post(`/admin/products/${testProductId}`, payload, adminHeaders)
const response = await api.post(
`/admin/products/${testProductId}`,
payload,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
@@ -1687,21 +1689,21 @@ describe("/admin/products", () => {
it("remove all categories of a product", async () => {
const api = useApi()
const category = await simpleProductCategoryFactory(
dbConnection,
{
id: "existing-category",
name: "existing category",
products: [{ id: "test-product" }]
}
)
const category = await simpleProductCategoryFactory(dbConnection, {
id: "existing-category",
name: "existing category",
products: [{ id: "test-product" }],
})
const payload = {
categories: [],
}
const response = await api
.post("/admin/products/test-product", payload, adminHeaders)
const response = await api.post(
"/admin/products/test-product",
payload,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
@@ -1720,11 +1722,13 @@ describe("/admin/products", () => {
const error = await api
.post("/admin/products/test-product", payload, adminHeaders)
.catch(e => e)
.catch((e) => e)
expect(error.response.status).toEqual(400)
expect(error.response.data.type).toEqual("invalid_data")
expect(error.response.data.message).toEqual("property incorrect should not exist, id must be a string")
expect(error.response.data.message).toEqual(
"property incorrect should not exist, id must be a string"
)
})
})
})
@@ -2126,6 +2130,193 @@ describe("/admin/products", () => {
])
)
})
it("successfully deletes a region price", async () => {
const api = useApi()
const createRegionPricePayload = {
prices: [
{
currency_code: "usd",
amount: 1000,
},
{
region_id: "test-region",
amount: 8000,
},
{
currency_code: "eur",
amount: 900,
},
],
}
const variantId = "test-variant_3"
const createRegionPriceResponse = await api.post(
"/admin/products/test-product1/variants/test-variant_3",
createRegionPricePayload,
adminHeaders
)
const initialPriceArray =
createRegionPriceResponse.data.product.variants.find(
(v) => v.id === variantId
).prices
expect(createRegionPriceResponse.status).toEqual(200)
expect(initialPriceArray).toHaveLength(3)
expect(initialPriceArray).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 1000,
currency_code: "usd",
}),
expect.objectContaining({
amount: 8000,
currency_code: "usd",
region_id: "test-region",
}),
expect.objectContaining({
amount: 900,
currency_code: "eur",
}),
])
)
const deleteRegionPricePayload = {
prices: [
{
currency_code: "usd",
amount: 1000,
},
{
currency_code: "eur",
amount: 900,
},
],
}
const deleteRegionPriceResponse = await api.post(
"/admin/products/test-product1/variants/test-variant_3",
deleteRegionPricePayload,
adminHeaders
)
const finalPriceArray =
deleteRegionPriceResponse.data.product.variants.find(
(v) => v.id === variantId
).prices
expect(deleteRegionPriceResponse.status).toEqual(200)
expect(finalPriceArray).toHaveLength(2)
expect(finalPriceArray).toEqual([
expect.objectContaining({
amount: 1000,
currency_code: "usd",
}),
expect.objectContaining({
amount: 900,
currency_code: "eur",
}),
])
})
it("successfully updates a variants prices by deleting both a currency and region price", async () => {
const api = useApi()
await Promise.all(
["reg_1", "reg_2", "reg_3"].map(async (regionId) => {
return await simpleRegionFactory(dbConnection, {
id: regionId,
currency_code: regionId === "reg_1" ? "eur" : "usd",
})
})
)
const createPrices = {
prices: [
{
region_id: "reg_1",
amount: 1,
},
{
region_id: "reg_2",
amount: 2,
},
{
currency_code: "usd",
amount: 3,
},
{
region_id: "reg_3",
amount: 4,
},
{
currency_code: "eur",
amount: 5,
},
],
}
const variantId = "test-variant_3"
await api
.post(
`/admin/products/test-product1/variants/${variantId}`,
createPrices,
adminHeaders
)
.catch((err) => {
console.log(err)
})
const updatePrices = {
prices: [
{
region_id: "reg_1",
amount: 100,
},
{
region_id: "reg_2",
amount: 200,
},
{
currency_code: "usd",
amount: 300,
},
],
}
const response = await api.post(
`/admin/products/test-product1/variants/${variantId}`,
updatePrices,
adminHeaders
)
const finalPriceArray = response.data.product.variants.find(
(v) => v.id === variantId
).prices
expect(response.status).toEqual(200)
expect(finalPriceArray).toHaveLength(3)
expect(finalPriceArray).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
region_id: "reg_1",
}),
expect.objectContaining({
amount: 200,
region_id: "reg_2",
}),
expect.objectContaining({
amount: 300,
currency_code: "usd",
}),
])
)
})
})
describe("variant creation", () => {