feat(medusa): Support deleting prices from a price list by product or variant (#1555)
This commit is contained in:
committed by
GitHub
parent
ad9cfedf04
commit
fa031fd28b
@@ -1207,4 +1207,158 @@ describe("/admin/price-lists", () => {
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete prices from price list related to the specified product or variant", () => {
|
||||
let product1, product2
|
||||
|
||||
function getCustomPriceIdFromVariant(variantId, index) {
|
||||
return "ma_" + index + "_" + variantId
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
product1 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-prod-1",
|
||||
title: "some product",
|
||||
variants: [
|
||||
{
|
||||
id: `simple-test-variant-${Math.random() * 1000}`,
|
||||
title: "Test",
|
||||
prices: [{ currency: "usd", amount: 100 }],
|
||||
},
|
||||
{
|
||||
id: `simple-test-variant-${Math.random() * 1000}`,
|
||||
title: "Test 2",
|
||||
prices: [{ currency: "usd", amount: 200 }],
|
||||
}
|
||||
]
|
||||
},
|
||||
1
|
||||
)
|
||||
|
||||
product2 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-prod-2",
|
||||
title: "some product 2"
|
||||
},
|
||||
2
|
||||
)
|
||||
|
||||
await simplePriceListFactory(dbConnection, {
|
||||
id: "test-list",
|
||||
customer_groups: ["test-group"],
|
||||
prices: [
|
||||
...product1.variants.map((variant, i) => (
|
||||
{
|
||||
id: getCustomPriceIdFromVariant(variant.id, i),
|
||||
variant_id: variant.id,
|
||||
currency_code: "usd",
|
||||
amount: (i + 1) * 150
|
||||
}
|
||||
)),
|
||||
...product2.variants.map((variant, i) => (
|
||||
{
|
||||
id: getCustomPriceIdFromVariant(variant.id, i),
|
||||
variant_id: variant.id,
|
||||
currency_code: "usd",
|
||||
amount: (i + 1) * 150
|
||||
}
|
||||
)),
|
||||
]
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it('should delete all the prices that are part of the price list for the specified product', async () => {
|
||||
const api = useApi()
|
||||
|
||||
response = await api
|
||||
.get("/admin/price-lists/test-list", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.data.price_list.prices.length).toBe(3)
|
||||
|
||||
let response = await api
|
||||
.delete(`/admin/price-lists/test-list/products/${product1.id}/prices`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.data).toEqual({
|
||||
ids: product1.variants.map((variant, i) => {
|
||||
return getCustomPriceIdFromVariant(variant.id, i)
|
||||
}),
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
response = await api
|
||||
.get("/admin/price-lists/test-list", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.data.price_list.prices.length).toBe(1)
|
||||
})
|
||||
|
||||
it('should delete all the prices that are part of the price list for the specified variant', async () => {
|
||||
const api = useApi()
|
||||
|
||||
response = await api
|
||||
.get("/admin/price-lists/test-list", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.data.price_list.prices.length).toBe(3)
|
||||
|
||||
const variant = product2.variants[0]
|
||||
let response = await api
|
||||
.delete(`/admin/price-lists/test-list/variants/${variant.id}/prices`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.data).toEqual({
|
||||
ids: [getCustomPriceIdFromVariant(variant.id, 0)],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
response = await api
|
||||
.get("/admin/price-lists/test-list", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.data.price_list.prices.length).toBe(2)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -112,5 +112,5 @@ export const simpleProductFactory = async (
|
||||
await simpleProductVariantFactory(connection, factoryData)
|
||||
}
|
||||
|
||||
return manager.findOne(Product, { id: prodId }, { relations: ["tags"] })
|
||||
return manager.findOne(Product, { id: prodId }, { relations: ["tags", "variants", "variants.prices"] })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user