From f7ffa3540f6e293507a1a96c0993b784a58ea507 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Mon, 6 Jan 2025 09:31:58 +0100 Subject: [PATCH] fix(promotion): don't evaluate rule condition if conditions to evaluate is empty (#10795) --- .changeset/selfish-coats-drop.md | 5 +++ .../promotion-module/compute-actions.spec.ts | 34 +++++++++++++++++++ .../src/utils/validations/promotion-rule.ts | 4 +++ 3 files changed, 43 insertions(+) create mode 100644 .changeset/selfish-coats-drop.md diff --git a/.changeset/selfish-coats-drop.md b/.changeset/selfish-coats-drop.md new file mode 100644 index 0000000000..c40b2e64f4 --- /dev/null +++ b/.changeset/selfish-coats-drop.md @@ -0,0 +1,5 @@ +--- +"@medusajs/promotion": patch +--- + +fix(promotion): don't evaluate rule condition if conditions to evaluate is empty diff --git a/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts b/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts index d839968794..0e50bf8712 100644 --- a/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts +++ b/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts @@ -132,6 +132,40 @@ moduleIntegrationTestRunner({ code: "PROMOTION_TEST", }, ]) + + const resultWithoutCustomer = await service.computeActions( + ["PROMOTION_TEST"], + { + items: [ + { + id: "item_cotton_tshirt", + quantity: 1, + subtotal: 100, + product_category: { + id: "catg_cotton", + }, + product: { + id: "prod_tshirt", + }, + }, + { + id: "item_cotton_sweater", + quantity: 5, + subtotal: 750, + product_category: { + id: "catg_cotton", + }, + product: { + id: "prod_sweater", + }, + }, + ], + } + ) + + expect(JSON.parse(JSON.stringify(resultWithoutCustomer))).toEqual( + [] + ) }) it("should compute the correct item amendments when there are multiple promotions to apply", async () => { diff --git a/packages/modules/promotion/src/utils/validations/promotion-rule.ts b/packages/modules/promotion/src/utils/validations/promotion-rule.ts index c9e4732596..c6295ec351 100644 --- a/packages/modules/promotion/src/utils/validations/promotion-rule.ts +++ b/packages/modules/promotion/src/utils/validations/promotion-rule.ts @@ -115,6 +115,10 @@ export function evaluateRuleValueCondition( ruleValuesToCheck = [ruleValuesToCheck] } + if (!ruleValuesToCheck.length) { + return false + } + return ruleValuesToCheck.every((ruleValueToCheck: string) => { if (operator === "in" || operator === "eq") { return ruleValues.some((ruleValue) => ruleValue === ruleValueToCheck)