|
1
|
import { castNumber } from "../../../../lib/cast-number" |
|
2
|
import { ITEM_TOTAL_ATTRIBUTE } from "../constants" |
|
3
|
|
|
4
|
const createPriceRule = ( |
|
5
|
attribute: string, |
|
6
|
operator: string, |
|
7
|
value: string | number |
|
8
|
) => { |
|
9
|
const rule = { |
|
10
|
attribute, |
|
11
|
operator, |
|
12
|
value: castNumber(value), |
|
13
|
} |
|
14
|
|
|
15
|
return rule |
|
16
|
} |
|
17
|
|
|
18
|
export const buildShippingOptionPriceRules = (rule: { |
|
19
|
gte?: string | number | null |
|
20
|
lte?: string | number | null |
|
21
|
gt?: string | number | null |
|
22
|
lt?: string | number | null |
|
23
|
eq?: string | number | null |
|
24
|
}) => { |
|
25
|
const conditions = [ |
|
26
|
{ value: rule.gte, operator: "gte" }, |
|
27
|
{ value: rule.lte, operator: "lte" }, |
|
28
|
{ value: rule.gt, operator: "gt" }, |
|
29
|
{ value: rule.lt, operator: "lt" }, |
|
30
|
{ value: rule.eq, operator: "eq" }, |
|
31
|
] |
|
32
|
|
|
33
|
const conditionsWithValues = conditions.filter( |
|
34
|
({ value }) => value !== undefined && value !== null && value !== "" |
|
35
|
) as { |
|
36
|
value: string | number |
|
37
|
operator: string |
|
38
|
}[] |
|
39
|
|
|
40
|
return conditionsWithValues.map(({ operator, value }) => |
|
41
|
createPriceRule(ITEM_TOTAL_ATTRIBUTE, operator, value) |
|
42
|
) |
|
43
|
} |