Skip to content

Commit 93d7a93

Browse files
Made in operator work as In insted of equal logic (medusajs#13078)
Changed the In operator to actually work like an In instead of being same logic as Equals. This means that in promotions you can add a rule to apply when a product is in a category or multiple different. Before the logic had to match all the products categories to apply, which doesnt really make sense when you have nested category structure on products. The logic also applies to tags where you can make a rule apply based on a tag before it also had to match all tags on a product to apply. Issue: medusajs#12669 Co-authored-by: Carlos R. L. Rodrigues <[email protected]>
1 parent 02dd83f commit 93d7a93

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

.changeset/khaki-buses-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/promotion": patch
3+
---
4+
5+
chore(promotion): in operator work as In insted of equal logic

packages/modules/promotion/integration-tests/__tests__/services/promotion-module/evaluate-rule-value-condition.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ moduleIntegrationTestRunner({
1818
})
1919
})
2020

21+
describe("in", () => {
22+
const operator = "in"
23+
24+
it("should evaluate conditions accurately", async () => {
25+
expect(testFunc(["1", "2"], operator, [2])).toEqual(true)
26+
expect(testFunc(["2"], operator, ["2"])).toEqual(true)
27+
expect(testFunc(["2"], operator, ["22"])).toEqual(false)
28+
})
29+
})
30+
2131
describe("ne", () => {
2232
const operator = "ne"
2333

packages/modules/promotion/src/utils/validations/promotion-rule.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ export function evaluateRuleValueCondition(
117117
}
118118

119119
switch (operator) {
120-
case "in":
121120
case "eq": {
122121
const ruleValueSet = new Set(ruleValues)
123122
return valuesToCheck.every((val) => ruleValueSet.has(`${val}`))
124123
}
124+
case "in": {
125+
const ruleValueSet = new Set(ruleValues)
126+
return valuesToCheck.some((val) => ruleValueSet.has(`${val}`))
127+
}
125128
case "ne": {
126129
const ruleValueSet = new Set(ruleValues)
127130
return valuesToCheck.every((val) => !ruleValueSet.has(`${val}`))

0 commit comments

Comments
 (0)