-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathevaluate-reward-conditions.ts
More file actions
224 lines (186 loc) · 5.1 KB
/
Copy pathevaluate-reward-conditions.ts
File metadata and controls
224 lines (186 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { prepareMetadataFieldValue } from "../api/rewards/reward-condition-metadata";
import {
RewardCondition,
RewardConditions,
RewardConditionsArray,
RewardContext,
} from "../types";
import { getRewardAmount } from "./get-reward-amount";
export const evaluateRewardConditions = ({
conditions,
context,
}: {
conditions: RewardConditionsArray;
context: RewardContext;
}) => {
if (!conditions || !context) {
return null;
}
const matchingConditions: RewardConditions[] = [];
for (const conditionGroup of conditions) {
// Evaluate each condition in the group
const conditionResults = conditionGroup.conditions.map((condition) => {
const fieldValue = resolveConditionFieldValue({ condition, context });
if (fieldValue === undefined) {
return false;
}
return evaluateCondition({
condition,
fieldValue,
});
});
// Apply the operator logic to the condition results
let conditionsMet = false;
if (conditionGroup.operator === "AND") {
conditionsMet = conditionResults.every((result) => result);
} else if (conditionGroup.operator === "OR") {
conditionsMet = conditionResults.some((result) => result);
}
if (conditionsMet) {
matchingConditions.push(conditionGroup);
}
}
if (matchingConditions.length === 0) {
return null;
}
// Find the best matching condition (highest amount)
return matchingConditions.sort(
(a, b) =>
getRewardAmount({
type: b.type!,
amountInCents: b.amountInCents,
amountInPercentage: b.amountInPercentage,
}) -
getRewardAmount({
type: a.type!,
amountInCents: a.amountInCents,
amountInPercentage: a.amountInPercentage,
}),
)[0];
};
function resolveConditionFieldValue({
condition,
context,
}: {
condition: RewardCondition;
context: RewardContext;
}): string | number | string[] | number[] | undefined {
if (condition.attribute === "metadata") {
const metaKey = condition.metadataField?.trim();
if (!metaKey) {
return undefined;
}
const entityMap = {
partner: undefined,
customer: undefined,
lead: context.lead,
sale: context.sale,
} as const;
return prepareMetadataFieldValue(
entityMap[condition.entity]?.metadata?.[metaKey],
condition,
);
}
const entityMap = {
partner: context.partner,
customer: context.customer,
lead: undefined,
sale: context.sale,
} as const;
return entityMap[condition.entity]?.[condition.attribute];
}
const evaluateCondition = ({
condition,
fieldValue,
}: {
condition: RewardCondition;
fieldValue: string | number | string[] | number[];
}) => {
// Equals
if (condition.operator === "equals_to") {
return fieldValue === condition.value;
}
// Not equals
if (condition.operator === "not_equals") {
return fieldValue !== condition.value;
}
// Starts with
if (condition.operator === "starts_with") {
if (
typeof fieldValue !== "string" ||
typeof condition.value !== "string" ||
condition.value === ""
) {
return false;
}
return fieldValue.startsWith(condition.value);
}
// Ends with
if (condition.operator === "ends_with") {
if (
typeof fieldValue !== "string" ||
typeof condition.value !== "string" ||
condition.value === ""
) {
return false;
}
return fieldValue.endsWith(condition.value);
}
// Contains
if (condition.operator === "contains") {
if (typeof fieldValue !== "string" || typeof condition.value !== "string") {
return false;
}
const trimmedValue = condition.value.trim();
if (trimmedValue === "") {
return false;
}
return String(fieldValue).includes(trimmedValue);
}
// Not contains
if (condition.operator === "not_contains") {
if (typeof fieldValue !== "string" || typeof condition.value !== "string") {
return false;
}
const trimmedValue = condition.value.trim();
if (trimmedValue === "") {
return false;
}
return !String(fieldValue).includes(trimmedValue);
}
// In
if (condition.operator === "in") {
if (!Array.isArray(condition.value)) {
return false;
}
return (condition.value as (string | number)[]).includes(
fieldValue as string | number,
);
}
// Not in
if (condition.operator === "not_in") {
if (!Array.isArray(condition.value) || condition.value.length === 0) {
return false;
}
return !(condition.value as (string | number)[]).includes(
fieldValue as string | number,
);
}
// Greater than
if (condition.operator === "greater_than") {
return Number(fieldValue) > Number(condition.value);
}
// Greater than or equal
if (condition.operator === "greater_than_or_equal") {
return Number(fieldValue) >= Number(condition.value);
}
// Less than
if (condition.operator === "less_than") {
return Number(fieldValue) < Number(condition.value);
}
// Less than or equal
if (condition.operator === "less_than_or_equal") {
return Number(fieldValue) <= Number(condition.value);
}
return false;
};