1 import {
2 ApplicationMethodTargetTypeValues,
3 InferEntityType,
4 PromotionRuleDTO,
5 PromotionRuleOperatorValues,
6 } from "@medusajs/framework/types"
7 import {
8 ApplicationMethodTargetType,
9 MathBN,
10 MedusaError,
11 PromotionRuleOperator,
12 isPresent,
13 isString,
14 pickValueFromObject,
15 } from "@medusajs/framework/utils"
16 import { PromotionRule } from "@models"
17 import { CreatePromotionRuleDTO } from "@types"
18
19 export function validatePromotionRuleAttributes(
20 promotionRulesData: CreatePromotionRuleDTO[]
21 ) {
22 const errors: string[] = []
23
24 for (const promotionRuleData of promotionRulesData) {
25 if (!isPresent(promotionRuleData.attribute)) {
26 errors.push("rules[].attribute is a required field")
27 }
28
29 if (!isPresent(promotionRuleData.operator)) {
30 errors.push("rules[].operator is a required field")
31 }
32
33 if (isPresent(promotionRuleData.operator)) {
34 const allowedOperators: PromotionRuleOperatorValues[] = Object.values(
35 PromotionRuleOperator
36 )
37
38 if (!allowedOperators.includes(promotionRuleData.operator)) {
39 errors.push(
40 `rules[].operator (${
41 promotionRuleData.operator
42 }) is invalid. It should be one of ${allowedOperators.join(", ")}`
43 )
44 }
45 } else {
46 errors.push("rules[].operator is a required field")
47 }
48 }
49
50 if (!errors.length) return
51
52 throw new MedusaError(MedusaError.Types.INVALID_DATA, errors.join(", "))
53 }
54
55 export function areRulesValidForContext(
56 rules: PromotionRuleDTO[] | InferEntityType<typeof PromotionRule>[],
57 context: Record<string, any>,
58 contextScope: ApplicationMethodTargetTypeValues
59 ): boolean {
60 if (!rules?.length) {
61 return true
62 }
63
64 const isItemScope = contextScope === ApplicationMethodTargetType.ITEMS
65 const isShippingScope =
66 contextScope === ApplicationMethodTargetType.SHIPPING_METHODS
67
68 return ("initialized" in rules ? [...rules] : rules).every((rule) => {
69 if (!rule.attribute || !rule.values?.length) {
70 return false
71 }
72
73 const validRuleValues: string[] = []
74 for (const value of rule.values) {
75 if (isString(value.value)) {
76 validRuleValues.push(value.value as string)
77 }
78 }
79
80 if (!validRuleValues.length) {
81 return false
82 }
83
84 let ruleAttribute = rule.attribute
85 if (isItemScope) {
86 ruleAttribute = ruleAttribute.replace(
87 `${ApplicationMethodTargetType.ITEMS}.`,
88 ""
89 )
90 } else if (isShippingScope) {
91 ruleAttribute = ruleAttribute.replace(
92 `${ApplicationMethodTargetType.SHIPPING_METHODS}.`,
93 ""
94 )
95 }
96
97 const valuesToCheck = pickValueFromObject(ruleAttribute, context)
98
99 return evaluateRuleValueCondition(
100 validRuleValues,
101 rule.operator!,
102 valuesToCheck
103 )
104 })
105 }
106
107 /*
108 Optimized evaluateRuleValueCondition by using early returns and cleaner approach
109 for evaluating rule conditions.
110 */
111 export function evaluateRuleValueCondition(
112 ruleValues: string[],
113 operator: string,
114 ruleValuesToCheck: (string | number)[] | (string | number)
115 ): boolean {
116 const valuesToCheck = Array.isArray(ruleValuesToCheck)
117 ? ruleValuesToCheck
118 : [ruleValuesToCheck]
119
120 if (!valuesToCheck.length) {
121 switch (operator) {
122 case "ne":
123 return true
124 default:
125 return false
126 }
127 }
128
129 switch (operator) {
130 case "eq": {
131 const ruleValueSet = new Set(ruleValues)
132 return valuesToCheck.every((val) => ruleValueSet.has(`${val}`))
133 }
134 case "in": {
135 const ruleValueSet = new Set(ruleValues)
136 return valuesToCheck.some((val) => ruleValueSet.has(`${val}`))
137 }
138 case "ne": {
139 const ruleValueSet = new Set(ruleValues)
140 return valuesToCheck.every((val) => !ruleValueSet.has(`${val}`))
141 }
142 case "gt":
143 return valuesToCheck.every((val) =>
144 ruleValues.some((ruleVal) => MathBN.gt(val, ruleVal))
145 )
146 case "gte":
147 return valuesToCheck.every((val) =>
148 ruleValues.some((ruleVal) => MathBN.gte(val, ruleVal))
149 )
150 case "lt":
151 return valuesToCheck.every((val) =>
152 ruleValues.some((ruleVal) => MathBN.lt(val, ruleVal))
153 )
154 case "lte":
155 return valuesToCheck.every((val) =>
156 ruleValues.some((ruleVal) => MathBN.lte(val, ruleVal))
157 )
158 default:
159 return false
160 }
161 }