fix(promotion): eval conditions for rules are made accurate (#10915)
what: - fixes eval conditions for promotion rules RESOLVES CMRC-851
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import { evaluateRuleValueCondition } from "../../../../src/utils/validations/promotion-rule"
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.PROMOTION,
|
||||
testSuite: () => {
|
||||
describe("evaluateRuleValueCondition", () => {
|
||||
const testFunc = evaluateRuleValueCondition
|
||||
|
||||
describe("eq", () => {
|
||||
const operator = "eq"
|
||||
|
||||
it("should evaluate conditions accurately", async () => {
|
||||
expect(testFunc(["2"], operator, [2])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, ["2"])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, ["22"])).toEqual(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("ne", () => {
|
||||
const operator = "ne"
|
||||
|
||||
it("should evaluate conditions accurately", async () => {
|
||||
expect(testFunc(["2"], operator, [2])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["22"])).toEqual(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("gt", () => {
|
||||
const operator = "gt"
|
||||
|
||||
it("should evaluate conditions accurately", async () => {
|
||||
expect(testFunc(["2"], operator, [1])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["1"])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, [2])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["22"])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, [22])).toEqual(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("gte", () => {
|
||||
const operator = "gte"
|
||||
|
||||
it("should evaluate conditions accurately", async () => {
|
||||
expect(testFunc(["2"], operator, [1])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["1"])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, [2])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, ["2"])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, ["22"])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, [22])).toEqual(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("lt", () => {
|
||||
const operator = "lt"
|
||||
|
||||
it("should evaluate conditions accurately", async () => {
|
||||
expect(testFunc([1], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc(["1"], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc([2], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc(["2"], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc(["22"], operator, ["2"])).toEqual(true)
|
||||
expect(testFunc([22], operator, ["2"])).toEqual(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("lte", () => {
|
||||
const operator = "lte"
|
||||
|
||||
it("should evaluate conditions accurately", async () => {
|
||||
expect(testFunc([1], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc(["1"], operator, ["2"])).toEqual(false)
|
||||
expect(testFunc([2], operator, ["2"])).toEqual(true)
|
||||
expect(testFunc(["2"], operator, ["2"])).toEqual(true)
|
||||
expect(testFunc(["22"], operator, ["2"])).toEqual(true)
|
||||
expect(testFunc([22], operator, ["2"])).toEqual(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
ApplicationMethodTargetType,
|
||||
MathBN,
|
||||
MedusaError,
|
||||
PromotionRuleOperator,
|
||||
isPresent,
|
||||
@@ -109,7 +110,7 @@ function fetchRuleAttributeForContext(
|
||||
export function evaluateRuleValueCondition(
|
||||
ruleValues: string[],
|
||||
operator: string,
|
||||
ruleValuesToCheck: string[] | string
|
||||
ruleValuesToCheck: (string | number)[] | (string | number)
|
||||
) {
|
||||
if (!Array.isArray(ruleValuesToCheck)) {
|
||||
ruleValuesToCheck = [ruleValuesToCheck]
|
||||
@@ -119,29 +120,37 @@ export function evaluateRuleValueCondition(
|
||||
return false
|
||||
}
|
||||
|
||||
return ruleValuesToCheck.every((ruleValueToCheck: string) => {
|
||||
return ruleValuesToCheck.every((ruleValueToCheck: string | number) => {
|
||||
if (operator === "in" || operator === "eq") {
|
||||
return ruleValues.some((ruleValue) => ruleValue === ruleValueToCheck)
|
||||
return ruleValues.some((ruleValue) => ruleValue === `${ruleValueToCheck}`)
|
||||
}
|
||||
|
||||
if (operator === "ne") {
|
||||
return ruleValues.some((ruleValue) => ruleValue !== ruleValueToCheck)
|
||||
return ruleValues.some((ruleValue) => ruleValue !== `${ruleValueToCheck}`)
|
||||
}
|
||||
|
||||
if (operator === "gt") {
|
||||
return ruleValues.some((ruleValue) => ruleValue > ruleValueToCheck)
|
||||
return ruleValues.some((ruleValue) =>
|
||||
MathBN.convert(ruleValueToCheck).gt(MathBN.convert(ruleValue))
|
||||
)
|
||||
}
|
||||
|
||||
if (operator === "gte") {
|
||||
return ruleValues.some((ruleValue) => ruleValue >= ruleValueToCheck)
|
||||
return ruleValues.some((ruleValue) =>
|
||||
MathBN.convert(ruleValueToCheck).gte(MathBN.convert(ruleValue))
|
||||
)
|
||||
}
|
||||
|
||||
if (operator === "lt") {
|
||||
return ruleValues.some((ruleValue) => ruleValue < ruleValueToCheck)
|
||||
return ruleValues.some((ruleValue) =>
|
||||
MathBN.convert(ruleValueToCheck).lt(MathBN.convert(ruleValue))
|
||||
)
|
||||
}
|
||||
|
||||
if (operator === "lte") {
|
||||
return ruleValues.some((ruleValue) => ruleValue <= ruleValueToCheck)
|
||||
return ruleValues.some((ruleValue) =>
|
||||
MathBN.convert(ruleValueToCheck).lte(MathBN.convert(ruleValue))
|
||||
)
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user