fix(utils,promotion): fix case where mutliple percentage promotions werent applied (#11992)

what:

- fix case where mutliple percentage promotions werent applied
This commit is contained in:
Riqwan Thamir
2025-03-26 22:46:50 +00:00
committed by GitHub
parent 1f8fab3636
commit c870a7a1ba
3 changed files with 330 additions and 9 deletions
@@ -736,7 +736,7 @@ moduleIntegrationTestRunner({
{
action: "addItemAdjustment",
item_id: "item_cotton_tshirt",
amount: 2,
amount: 5,
code: "PROMOTION_TEST_2",
},
{
@@ -748,6 +748,81 @@ moduleIntegrationTestRunner({
])
})
it("should compute actions when 2 percentage promotions are applied to the same item", async () => {
await createDefaultPromotion(service, {
code: "PROMO_PERCENTAGE_1",
rules: [],
application_method: {
type: ApplicationMethodType.PERCENTAGE,
target_type: "items",
allocation: "each",
value: 100,
max_quantity: 1,
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: ["prod_tshirt"],
},
],
},
})
await createDefaultPromotion(service, {
code: "PROMO_PERCENTAGE_2",
rules: [],
application_method: {
type: ApplicationMethodType.PERCENTAGE,
target_type: "items",
allocation: "each",
value: 100,
max_quantity: 1,
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: ["prod_tshirt"],
},
],
},
})
const result = await service.computeActions(
["PROMO_PERCENTAGE_1", "PROMO_PERCENTAGE_2"],
{
currency_code: "usd",
items: [
{
id: "item_cotton_tshirt",
quantity: 4,
subtotal: 200,
product_category: {
id: "catg_cotton",
},
product: {
id: "prod_tshirt",
},
},
],
}
)
expect(JSON.parse(JSON.stringify(result))).toEqual([
{
action: "addItemAdjustment",
item_id: "item_cotton_tshirt",
amount: 50,
code: "PROMO_PERCENTAGE_1",
},
{
action: "addItemAdjustment",
item_id: "item_cotton_tshirt",
amount: 50,
code: "PROMO_PERCENTAGE_2",
},
])
})
it("should not compute actions when applicable total is 0", async () => {
await createDefaultPromotion(service, {
rules: [
@@ -1695,6 +1770,160 @@ moduleIntegrationTestRunner({
])
})
it("should compute actions when 2 percentage promotions are applied to the same item", async () => {
await createDefaultPromotion(service, {
code: "PROMO_PERCENTAGE_1",
rules: [],
application_method: {
type: ApplicationMethodType.PERCENTAGE,
target_type: "items",
allocation: "across",
value: 50,
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: ["prod_tshirt"],
},
],
},
})
await createDefaultPromotion(service, {
code: "PROMO_PERCENTAGE_2",
rules: [],
application_method: {
type: ApplicationMethodType.PERCENTAGE,
target_type: "items",
allocation: "across",
value: 50,
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: ["prod_tshirt"],
},
],
},
})
const result = await service.computeActions(
["PROMO_PERCENTAGE_1", "PROMO_PERCENTAGE_2"],
{
currency_code: "usd",
items: [
{
id: "item_cotton_tshirt",
quantity: 4,
subtotal: 300,
product: {
id: "prod_tshirt",
},
},
{
id: "item_wool_tshirt",
quantity: 4,
subtotal: 100,
product: {
id: "prod_tshirt",
},
},
],
}
)
expect(JSON.parse(JSON.stringify(result))).toEqual([
{
action: "addItemAdjustment",
item_id: "item_cotton_tshirt",
amount: 150,
code: "PROMO_PERCENTAGE_1",
},
{
action: "addItemAdjustment",
item_id: "item_wool_tshirt",
amount: 50,
code: "PROMO_PERCENTAGE_1",
},
{
action: "addItemAdjustment",
item_id: "item_cotton_tshirt",
amount: 75,
code: "PROMO_PERCENTAGE_2",
},
{
action: "addItemAdjustment",
item_id: "item_wool_tshirt",
amount: 25,
code: "PROMO_PERCENTAGE_2",
},
])
await createDefaultPromotion(service, {
code: "PROMO_PERCENTAGE_3",
rules: [],
application_method: {
type: ApplicationMethodType.PERCENTAGE,
target_type: "items",
allocation: "across",
value: 100,
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: ["prod_tshirt"],
},
],
},
})
const result2 = await service.computeActions(
[
"PROMO_PERCENTAGE_1",
"PROMO_PERCENTAGE_2",
"PROMO_PERCENTAGE_3",
],
{
currency_code: "usd",
items: [
{
id: "item_cotton_tshirt",
quantity: 4,
subtotal: 300,
product: {
id: "prod_tshirt",
},
},
{
id: "item_wool_tshirt",
quantity: 4,
subtotal: 100,
product: {
id: "prod_tshirt",
},
},
],
}
)
// Should only apply the most valuable promotion (100% off - PROMO_PERCENTAGE_3) that covers
// the entire total of the line item
expect(JSON.parse(JSON.stringify(result2))).toEqual([
{
action: "addItemAdjustment",
item_id: "item_cotton_tshirt",
amount: 300,
code: "PROMO_PERCENTAGE_3",
},
{
action: "addItemAdjustment",
item_id: "item_wool_tshirt",
amount: 100,
code: "PROMO_PERCENTAGE_3",
},
])
})
it("should not compute actions when applicable total is 0", async () => {
await createDefaultPromotion(service, {
rules: [
@@ -3937,7 +4166,7 @@ moduleIntegrationTestRunner({
},
},
],
})
} as any)
expect(JSON.parse(JSON.stringify(result))).toEqual([
{