feat(pricing, types): add price rule operators to price calculations (#10350)

what:

- adds price rule operators when doing price calculations
- rules now accepts a key where the value can be an array of objects `({ operator: string, value: number })`
  - validation for available types of operator and value to be a number
```
await service.createPriceSets({
  prices: [
    {
      amount: 50,
      currency_code: "usd",
      rules: {
        region_id: "de",
        cart_total: [
          { operator: "gte", value: 400 },
          { operator: "lte", value: 500 },
        ]
      },
    },
  ]
})
```
- price calculations will now account for the operators - lte, gte, lt, gt when the price context is a number

RESOLVES CMRC-747
This commit is contained in:
Riqwan Thamir
2024-11-28 20:48:00 +00:00
committed by GitHub
parent 805fe4b1db
commit 324b4ab438
8 changed files with 462 additions and 33 deletions
@@ -5,6 +5,7 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { defaultPriceRuleData } from "./data"
export * from "./data"
export * from "./operators"
export async function createPriceRules(
manager: SqlEntityManager,
@@ -0,0 +1,39 @@
import { RuleWithOperator } from "@medusajs/types"
export const withOperator = (
border,
min = 400,
max = 800
): RuleWithOperator[] => {
if (border === "betweenEquals") {
return [
{ operator: "gte", value: min },
{ operator: "lte", value: max },
]
} else if (border === "between") {
return [
{ operator: "gt", value: min },
{ operator: "lt", value: max },
]
} else if (border === "excludingMin") {
return [
{ operator: "gt", value: min },
{ operator: "lte", value: max },
]
} else if (border === "excludingMax") {
return [
{ operator: "gte", value: min },
{ operator: "lt", value: max },
]
} else if (border === "gt") {
return [{ operator: "gt", value: min }]
} else if (border === "lt") {
return [{ operator: "lt", value: min }]
} else if (border === "lte") {
return [{ operator: "lte", value: min }]
} else if (border === "gte") {
return [{ operator: "gte", value: min }]
} else {
return []
}
}