|
1
|
import { |
|
2
|
ApplicationMethodAllocation, |
|
3
|
ApplicationMethodTargetType, |
|
4
|
ApplicationMethodType, |
|
5
|
BigNumber, |
|
6
|
isDefined, |
|
7
|
isPresent, |
|
8
|
MathBN, |
|
9
|
MedusaError, |
|
10
|
PromotionType, |
|
11
|
} from "@medusajs/framework/utils" |
|
12
|
import { InferEntityType } from "@medusajs/types" |
|
13
|
import { Promotion } from "@models" |
|
14
|
import { CreateApplicationMethodDTO, UpdateApplicationMethodDTO } from "@types" |
|
15
|
|
|
16
|
export const allowedAllocationTargetTypes: string[] = [ |
|
17
|
ApplicationMethodTargetType.SHIPPING_METHODS, |
|
18
|
ApplicationMethodTargetType.ITEMS, |
|
19
|
] |
|
20
|
|
|
21
|
export const allowedAllocationTypes: string[] = [ |
|
22
|
ApplicationMethodAllocation.ACROSS, |
|
23
|
ApplicationMethodAllocation.EACH, |
|
24
|
ApplicationMethodAllocation.ONCE, |
|
25
|
] |
|
26
|
|
|
27
|
export const allowedAllocationForQuantity: string[] = [ |
|
28
|
ApplicationMethodAllocation.EACH, |
|
29
|
ApplicationMethodAllocation.ONCE, |
|
30
|
] |
|
31
|
|
|
32
|
export function validateApplicationMethodAttributes( |
|
33
|
data: UpdateApplicationMethodDTO | CreateApplicationMethodDTO, |
|
34
|
promotion: InferEntityType<typeof Promotion> |
|
35
|
) { |
|
36
|
const applicationMethod = promotion?.application_method || {} |
|
37
|
const buyRulesMinQuantity = |
|
38
|
data.buy_rules_min_quantity || applicationMethod?.buy_rules_min_quantity |
|
39
|
const applyToQuantity = |
|
40
|
data.apply_to_quantity || applicationMethod?.apply_to_quantity |
|
41
|
const targetType = data.target_type || applicationMethod?.target_type |
|
42
|
const type = data.type || applicationMethod?.type |
|
43
|
const applicationMethodType = data.type || applicationMethod?.type |
|
44
|
const value = new BigNumber(data.value ?? applicationMethod.value ?? 0) |
|
45
|
const maxQuantity = data.max_quantity || applicationMethod.max_quantity |
|
46
|
const allocation = data.allocation || applicationMethod.allocation |
|
47
|
const allTargetTypes: string[] = Object.values(ApplicationMethodTargetType) |
|
48
|
|
|
49
|
if ( |
|
50
|
type === ApplicationMethodType.PERCENTAGE && |
|
51
|
(MathBN.lte(value, 0) || MathBN.gt(value, 100)) |
|
52
|
) { |
|
53
|
throw new MedusaError( |
|
54
|
MedusaError.Types.INVALID_DATA, |
|
55
|
`Application Method value should be a percentage number between 0 and 100` |
|
56
|
) |
|
57
|
} |
|
58
|
|
|
59
|
if (promotion?.type === PromotionType.BUYGET) { |
|
60
|
if (!isPresent(applyToQuantity)) { |
|
61
|
throw new MedusaError( |
|
62
|
MedusaError.Types.INVALID_DATA, |
|
63
|
`apply_to_quantity is a required field for Promotion type of ${PromotionType.BUYGET}` |
|
64
|
) |
|
65
|
} |
|
66
|
|
|
67
|
if (!isPresent(buyRulesMinQuantity)) { |
|
68
|
throw new MedusaError( |
|
69
|
MedusaError.Types.INVALID_DATA, |
|
70
|
`buy_rules_min_quantity is a required field for Promotion type of ${PromotionType.BUYGET}` |
|
71
|
) |
|
72
|
} |
|
73
|
|
|
74
|
if (!isPresent(maxQuantity)) { |
|
75
|
throw new MedusaError( |
|
76
|
MedusaError.Types.INVALID_DATA, |
|
77
|
`application_method.max_quantity is a required field for Promotion type of ${PromotionType.BUYGET}` |
|
78
|
) |
|
79
|
} |
|
80
|
|
|
81
|
if (!isPresent(applyToQuantity)) { |
|
82
|
throw new MedusaError( |
|
83
|
MedusaError.Types.INVALID_DATA, |
|
84
|
`application_method.apply_to_quantity is a required field for Promotion type of ${PromotionType.BUYGET}` |
|
85
|
) |
|
86
|
} |
|
87
|
|
|
88
|
if (MathBN.lt(maxQuantity!, applyToQuantity!)) { |
|
89
|
throw new MedusaError( |
|
90
|
MedusaError.Types.INVALID_DATA, |
|
91
|
`max_quantity (${maxQuantity}) must be greater than or equal to apply_to_quantity (${applyToQuantity}) for BUYGET promotions.` |
|
92
|
) |
|
93
|
} |
|
94
|
} |
|
95
|
|
|
96
|
if ( |
|
97
|
allocation === ApplicationMethodAllocation.ACROSS && |
|
98
|
isPresent(maxQuantity) |
|
99
|
) { |
|
100
|
throw new MedusaError( |
|
101
|
MedusaError.Types.INVALID_DATA, |
|
102
|
`application_method.max_quantity is not allowed to be set for allocation (${ApplicationMethodAllocation.ACROSS})` |
|
103
|
) |
|
104
|
} |
|
105
|
|
|
106
|
if (!allTargetTypes.includes(targetType)) { |
|
107
|
throw new MedusaError( |
|
108
|
MedusaError.Types.INVALID_DATA, |
|
109
|
`application_method.target_type should be one of ${allTargetTypes.join( |
|
110
|
", " |
|
111
|
)}` |
|
112
|
) |
|
113
|
} |
|
114
|
|
|
115
|
const allTypes: string[] = Object.values(ApplicationMethodType) |
|
116
|
|
|
117
|
if (!allTypes.includes(applicationMethodType)) { |
|
118
|
throw new MedusaError( |
|
119
|
MedusaError.Types.INVALID_DATA, |
|
120
|
`application_method.type should be one of ${allTypes.join(", ")}` |
|
121
|
) |
|
122
|
} |
|
123
|
|
|
124
|
if ( |
|
125
|
allowedAllocationTargetTypes.includes(targetType) && |
|
126
|
!allowedAllocationTypes.includes(allocation || "") |
|
127
|
) { |
|
128
|
throw new MedusaError( |
|
129
|
MedusaError.Types.INVALID_DATA, |
|
130
|
`application_method.allocation should be either '${allowedAllocationTypes.join( |
|
131
|
" OR " |
|
132
|
)}' when application_method.target_type is either '${allowedAllocationTargetTypes.join( |
|
133
|
" OR " |
|
134
|
)}'` |
|
135
|
) |
|
136
|
} |
|
137
|
|
|
138
|
const allAllocationTypes: string[] = Object.values( |
|
139
|
ApplicationMethodAllocation |
|
140
|
) |
|
141
|
|
|
142
|
if (allocation && !allAllocationTypes.includes(allocation)) { |
|
143
|
throw new MedusaError( |
|
144
|
MedusaError.Types.INVALID_DATA, |
|
145
|
`application_method.allocation should be one of ${allAllocationTypes.join( |
|
146
|
", " |
|
147
|
)}` |
|
148
|
) |
|
149
|
} |
|
150
|
|
|
151
|
if ( |
|
152
|
allocation && |
|
153
|
allowedAllocationForQuantity.includes(allocation) && |
|
154
|
!isDefined(maxQuantity) |
|
155
|
) { |
|
156
|
throw new MedusaError( |
|
157
|
MedusaError.Types.INVALID_DATA, |
|
158
|
`application_method.max_quantity is required when application_method.allocation is '${allowedAllocationForQuantity.join( |
|
159
|
" OR " |
|
160
|
)}'` |
|
161
|
) |
|
162
|
} |
|
163
|
|
|
164
|
if ( |
|
165
|
allocation === ApplicationMethodAllocation.ONCE && |
|
166
|
targetType === ApplicationMethodTargetType.ORDER |
|
167
|
) { |
|
168
|
throw new MedusaError( |
|
169
|
MedusaError.Types.INVALID_DATA, |
|
170
|
`application_method.allocation 'once' is not compatible with target_type 'order'` |
|
171
|
) |
|
172
|
} |
|
173
|
} |