fix(medusa): Allow de-selecting configurations in price lists (#1596)

This commit is contained in:
Philip Korsholm
2022-05-30 19:34:34 +02:00
committed by GitHub
parent d0c679fc7e
commit 3f23edea23
4 changed files with 104 additions and 68 deletions
@@ -388,6 +388,41 @@ describe("/admin/price-lists", () => {
await db.teardown() await db.teardown()
}) })
it("removes configuration with update", async () => {
const priceList = await simplePriceListFactory(dbConnection, {
ends_at: new Date(),
starts_at: new Date(),
customer_groups: ["customer-group-1"],
})
const api = useApi()
const getResult = await api.get(`/admin/price-lists/${priceList.id}`, {
headers: {
Authorization: "Bearer test_token",
},
})
expect(getResult.status).toEqual(200)
expect(getResult.data.price_list.starts_at).toBeTruthy()
expect(getResult.data.price_list.ends_at).toBeTruthy()
expect(getResult.data.price_list.customer_groups.length).toEqual(1)
const updateResult = await api.post(
`/admin/price-lists/${priceList.id}`,
{ ends_at: null, starts_at: null, customer_groups: [] },
{
headers: {
Authorization: "Bearer test_token",
},
}
)
expect(updateResult.status).toEqual(200)
expect(updateResult.data.price_list.starts_at).toBeFalsy()
expect(updateResult.data.price_list.ends_at).toBeFalsy()
expect(updateResult.data.price_list.customer_groups.length).toEqual(0)
})
it("updates a price list", async () => { it("updates a price list", async () => {
const api = useApi() const api = useApi()
@@ -1209,7 +1244,8 @@ describe("/admin/price-lists", () => {
}) })
describe("delete prices from price list related to the specified product or variant", () => { describe("delete prices from price list related to the specified product or variant", () => {
let product1, product2 let product1
let product2
function getCustomPriceIdFromVariant(variantId, index) { function getCustomPriceIdFromVariant(variantId, index) {
return "ma_" + index + "_" + variantId return "ma_" + index + "_" + variantId
@@ -1234,8 +1270,8 @@ describe("/admin/price-lists", () => {
id: `simple-test-variant-${Math.random() * 1000}`, id: `simple-test-variant-${Math.random() * 1000}`,
title: "Test 2", title: "Test 2",
prices: [{ currency: "usd", amount: 200 }], prices: [{ currency: "usd", amount: 200 }],
} },
] ],
}, },
1 1
) )
@@ -1244,7 +1280,7 @@ describe("/admin/price-lists", () => {
dbConnection, dbConnection,
{ {
id: "test-prod-2", id: "test-prod-2",
title: "some product 2" title: "some product 2",
}, },
2 2
) )
@@ -1253,23 +1289,19 @@ describe("/admin/price-lists", () => {
id: "test-list", id: "test-list",
customer_groups: ["test-group"], customer_groups: ["test-group"],
prices: [ prices: [
...product1.variants.map((variant, i) => ( ...product1.variants.map((variant, i) => ({
{ id: getCustomPriceIdFromVariant(variant.id, i),
id: getCustomPriceIdFromVariant(variant.id, i), variant_id: variant.id,
variant_id: variant.id, currency_code: "usd",
currency_code: "usd", amount: (i + 1) * 150,
amount: (i + 1) * 150 })),
} ...product2.variants.map((variant, i) => ({
)), id: getCustomPriceIdFromVariant(variant.id, i),
...product2.variants.map((variant, i) => ( variant_id: variant.id,
{ currency_code: "usd",
id: getCustomPriceIdFromVariant(variant.id, i), amount: (i + 1) * 150,
variant_id: variant.id, })),
currency_code: "usd", ],
amount: (i + 1) * 150
}
)),
]
}) })
} catch (err) { } catch (err) {
console.log(err) console.log(err)
@@ -1282,66 +1314,67 @@ describe("/admin/price-lists", () => {
await db.teardown() await db.teardown()
}) })
it('should delete all the prices that are part of the price list for the specified product', async () => { it("should delete all the prices that are part of the price list for the specified product", async () => {
const api = useApi() const api = useApi()
response = await api response = await api.get("/admin/price-lists/test-list", {
.get("/admin/price-lists/test-list", { headers: {
headers: { Authorization: "Bearer test_token",
Authorization: "Bearer test_token", },
} })
})
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.data.price_list.prices.length).toBe(3) expect(response.data.price_list.prices.length).toBe(3)
let response = await api let response = await api.delete(
.delete(`/admin/price-lists/test-list/products/${product1.id}/prices`, { `/admin/price-lists/test-list/products/${product1.id}/prices`,
{
headers: { headers: {
Authorization: "Bearer test_token", Authorization: "Bearer test_token",
} },
}) }
)
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.data).toEqual({ expect(response.data).toEqual({
ids: product1.variants.map((variant, i) => { ids: product1.variants.map((variant, i) => {
return getCustomPriceIdFromVariant(variant.id, i) return getCustomPriceIdFromVariant(variant.id, i)
}), }),
object: "money-amount", object: "money-amount",
deleted: true, deleted: true,
}) })
response = await api response = await api.get("/admin/price-lists/test-list", {
.get("/admin/price-lists/test-list", { headers: {
headers: { Authorization: "Bearer test_token",
Authorization: "Bearer test_token", },
} })
})
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.data.price_list.prices.length).toBe(1) 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 () => { it("should delete all the prices that are part of the price list for the specified variant", async () => {
const api = useApi() const api = useApi()
response = await api response = await api.get("/admin/price-lists/test-list", {
.get("/admin/price-lists/test-list", { headers: {
headers: { Authorization: "Bearer test_token",
Authorization: "Bearer test_token", },
} })
})
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.data.price_list.prices.length).toBe(3) expect(response.data.price_list.prices.length).toBe(3)
const variant = product2.variants[0] const variant = product2.variants[0]
let response = await api let response = await api.delete(
.delete(`/admin/price-lists/test-list/variants/${variant.id}/prices`, { `/admin/price-lists/test-list/variants/${variant.id}/prices`,
{
headers: { headers: {
Authorization: "Bearer test_token", Authorization: "Bearer test_token",
} },
}) }
)
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.data).toEqual({ expect(response.data).toEqual({
@@ -1350,12 +1383,11 @@ describe("/admin/price-lists", () => {
deleted: true, deleted: true,
}) })
response = await api response = await api.get("/admin/price-lists/test-list", {
.get("/admin/price-lists/test-list", { headers: {
headers: { Authorization: "Bearer test_token",
Authorization: "Bearer test_token", },
} })
})
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.data.price_list.prices.length).toBe(2) expect(response.data.price_list.prices.length).toBe(2)
@@ -128,10 +128,10 @@ export class AdminPostPriceListsPriceListPriceListReq {
description?: string description?: string
@IsOptional() @IsOptional()
starts_at?: Date starts_at?: Date | null
@IsOptional() @IsOptional()
ends_at?: Date ends_at?: Date | null
@IsOptional() @IsOptional()
@IsEnum(PriceListStatus) @IsEnum(PriceListStatus)
+2 -2
View File
@@ -31,10 +31,10 @@ export class PriceList extends SoftDeletableEntity {
type: resolveDbType("timestamptz"), type: resolveDbType("timestamptz"),
nullable: true, nullable: true,
}) })
starts_at: Date starts_at: Date | null
@Column({ type: resolveDbType("timestamptz"), nullable: true }) @Column({ type: resolveDbType("timestamptz"), nullable: true })
ends_at: Date ends_at: Date | null
@JoinTable({ @JoinTable({
name: "price_list_customer_groups", name: "price_list_customer_groups",
+10 -6
View File
@@ -146,12 +146,6 @@ class PriceListService extends TransactionBaseService<PriceListService> {
const { prices, customer_groups, ...rest } = update const { prices, customer_groups, ...rest } = update
for (const [key, value] of Object.entries(rest)) {
priceList[key] = value
}
await priceListRepo.save(priceList)
if (prices) { if (prices) {
const prices_ = await this.addCurrencyFromRegion(prices) const prices_ = await this.addCurrencyFromRegion(prices)
await moneyAmountRepo.updatePriceListPrices(id, prices_) await moneyAmountRepo.updatePriceListPrices(id, prices_)
@@ -161,6 +155,16 @@ class PriceListService extends TransactionBaseService<PriceListService> {
await this.upsertCustomerGroups_(id, customer_groups) await this.upsertCustomerGroups_(id, customer_groups)
} }
for (const [key, value] of Object.entries(rest)) {
if (typeof value === "undefined") {
continue
}
priceList[key] = value
}
await priceListRepo.save(priceList)
return await this.retrieve(id, { return await this.retrieve(id, {
relations: ["prices", "customer_groups"], relations: ["prices", "customer_groups"],
}) })