fix: Disallow creating duplicate prices (#7866)

* fix: Disallow creating duplicate prices

* fix: Don't pass id to manager create in upsertWithReplace
This commit is contained in:
Stevche Radevski
2024-07-02 17:06:58 +02:00
committed by GitHub
parent 87375db9ef
commit b4aa7fb9a7
12 changed files with 560 additions and 350 deletions
@@ -799,7 +799,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
{
id: "price-set-PLN",
is_calculated_price_price_list: true,
calculated_amount: 116,
calculated_amount: 232,
is_original_price_price_list: false,
original_amount: 400,
currency_code: "PLN",
@@ -586,6 +586,63 @@ moduleIntegrationTestRunner<IPricingModuleService>({
})
)
})
it("should take the later price when passing two equivalent prices twice", async () => {
const [created] = await service.createPriceLists([
{
title: "test",
description: "test",
starts_at: "10/10/2010",
ends_at: "10/20/2030",
prices: [
{
amount: 400,
currency_code: "EUR",
price_set_id: "price-set-1",
rules: {
region_id: "DE",
},
},
{
amount: 600,
currency_code: "EUR",
price_set_id: "price-set-1",
rules: {
region_id: "DE",
},
},
],
},
])
const [priceList] = await service.listPriceLists(
{
id: [created.id],
},
{
relations: ["prices", "prices.price_rules"],
}
)
expect(priceList).toEqual(
expect.objectContaining({
id: expect.any(String),
prices: [
expect.objectContaining({
rules_count: 1,
price_rules: [
expect.objectContaining({
id: expect.any(String),
value: "DE",
}),
],
amount: 600,
currency_code: "EUR",
}),
],
})
)
})
})
describe("addPriceListPrices", () => {
@@ -810,6 +867,69 @@ moduleIntegrationTestRunner<IPricingModuleService>({
})
)
})
it("should do an update to the price if an equivalent price already exists", async () => {
const [priceSet] = await service.createPriceSets([{}])
await service.addPriceListPrices([
{
price_list_id: "price-list-1",
prices: [
{
id: "test-price-id",
amount: 123,
currency_code: "EUR",
price_set_id: priceSet.id,
rules: {
region_id: "test",
},
},
],
},
])
await service.addPriceListPrices([
{
price_list_id: "price-list-1",
prices: [
{
id: "test-price-id",
amount: 234,
currency_code: "EUR",
price_set_id: priceSet.id,
rules: {
region_id: "test",
},
},
],
},
])
const [priceList] = await service.listPriceLists(
{ id: ["price-list-1"] },
{
relations: ["prices", "prices.price_rules"],
}
)
expect(priceList).toEqual(
expect.objectContaining({
id: expect.any(String),
prices: expect.arrayContaining([
expect.objectContaining({
price_rules: [
expect.objectContaining({
value: "test",
attribute: "region_id",
}),
],
amount: 234,
currency_code: "EUR",
}),
]),
})
)
})
})
describe("removePrices", () => {
@@ -398,6 +398,40 @@ moduleIntegrationTestRunner<IPricingModuleService>({
])
)
})
it("should upsert the later price when setting a price set with existing equivalent rules", async () => {
await service.updatePriceSets(id, {
prices: [
{
amount: 100,
currency_code: "USD",
rules: { region_id: "1234" },
},
{
amount: 200,
currency_code: "USD",
rules: { region_id: "1234" },
},
],
})
const priceSet = await service.retrievePriceSet(id, {
relations: ["prices", "prices.price_rules"],
})
expect(priceSet.prices).toEqual([
expect.objectContaining({
amount: 200,
currency_code: "USD",
price_rules: [
expect.objectContaining({
attribute: "region_id",
value: "1234",
}),
],
}),
])
})
})
describe("create", () => {
@@ -512,6 +546,43 @@ moduleIntegrationTestRunner<IPricingModuleService>({
})
)
})
it("should take the later price when passing two prices with equivalent rules", async () => {
await service.createPriceSets([
{
id: "price-set-new",
prices: [
{
amount: 100,
currency_code: "USD",
rules: { region_id: "1234" },
},
{
amount: 200,
currency_code: "USD",
rules: { region_id: "1234" },
},
],
} as unknown as CreatePriceSetDTO,
])
const priceSet = await service.retrievePriceSet("price-set-new", {
relations: ["prices", "prices.price_rules"],
})
expect(priceSet.prices).toEqual([
expect.objectContaining({
amount: 200,
currency_code: "USD",
price_rules: [
expect.objectContaining({
attribute: "region_id",
value: "1234",
}),
],
}),
])
})
})
describe("addPrices", () => {
@@ -523,7 +594,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
{
amount: 100,
currency_code: "USD",
rules: { currency_code: "USD" },
rules: { region_id: "1234" },
},
],
},
@@ -574,7 +645,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
{
amount: 100,
currency_code: "USD",
rules: { currency_code: "USD" },
rules: { region_id: "region-1" },
},
],
},
@@ -616,6 +687,57 @@ moduleIntegrationTestRunner<IPricingModuleService>({
}),
])
})
it("should do an update if a price exists with the equivalent rules", async () => {
await service.addPrices([
{
priceSetId: "price-set-1",
prices: [
{
amount: 100,
currency_code: "USD",
rules: { region_id: "123" },
},
],
},
])
await service.addPrices([
{
priceSetId: "price-set-1",
prices: [
{
amount: 200,
currency_code: "USD",
rules: { region_id: "123" },
},
],
},
])
const priceSet = await service.retrievePriceSet("price-set-1", {
relations: ["prices", "prices.price_rules"],
})
expect(
priceSet.prices?.sort((a: any, b: any) => a.amount - b.amount)
).toEqual([
expect.objectContaining({
amount: 200,
currency_code: "USD",
price_rules: [
expect.objectContaining({
attribute: "region_id",
value: "123",
}),
],
}),
expect.objectContaining({
amount: 500,
currency_code: "USD",
}),
])
})
})
})
},