2
2
// @ts-check
3
3
4
4
import {
5
+ DiscountClass,
5
6
OrderDiscountSelectionStrategy,
6
7
ProductDiscountSelectionStrategy,
7
8
} from '../generated/api';
@@ -20,63 +21,85 @@ export function generateCartRun(input) {
20
21
if (!input.cart.lines.length) {
21
22
throw new Error('No cart lines found');
22
23
}
24
+
25
+ const hasOrderDiscountClass = input.discount.discountClasses.includes(
26
+ DiscountClass.Order
27
+ );
28
+
29
+ const hasProductDiscountClass = input.discount.discountClasses.includes(
30
+ DiscountClass.Product
31
+ );
32
+
33
+ if (!hasOrderDiscountClass && !hasProductDiscountClass) {
34
+ return { operations: [] };
35
+ }
36
+
23
37
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
24
38
if (line.cost.subtotalAmount.amount > maxLine.cost.subtotalAmount.amount) {
25
39
return line;
26
40
}
27
41
return maxLine;
28
42
}, input.cart.lines[0]);
29
- return {
30
- operations: [
31
- {
32
- orderDiscountsAdd: {
33
- candidates: [
34
- {
35
- message: '10% OFF ORDER',
36
- targets: [
37
- {
38
- orderSubtotal: {
39
- excludedCartLineIds: [],
40
- },
41
- },
42
- ],
43
- value: {
44
- percentage: {
45
- value: 10,
43
+
44
+ const operations = [];
45
+
46
+ if(hasOrderDiscountClass) {
47
+ operations.push({
48
+ orderDiscountsAdd: {
49
+ candidates: [
50
+ {
51
+ message: '10% OFF ORDER',
52
+ targets: [
53
+ {
54
+ orderSubtotal: {
55
+ excludedCartLineIds: [],
46
56
},
47
57
},
58
+ ],
59
+ value: {
60
+ percentage: {
61
+ value: 10,
62
+ },
48
63
},
49
- ] ,
50
- selectionStrategy: OrderDiscountSelectionStrategy.First ,
51
- } ,
64
+ } ,
65
+ ] ,
66
+ selectionStrategy: OrderDiscountSelectionStrategy.First ,
52
67
},
53
- {
54
- productDiscountsAdd: {
55
- candidates: [
56
- {
57
- message: '20% OFF PRODUCT',
58
- targets: [
59
- {
60
- cartLine: {
61
- id: maxCartLine.id,
62
- },
63
- },
64
- ],
65
- value: {
66
- percentage: {
67
- value: 20,
68
+ });
69
+ }
70
+
71
+ if (hasProductDiscountClass) {
72
+ operations.push({
73
+ productDiscountsAdd: {
74
+ candidates: [
75
+ {
76
+ message: '20% OFF PRODUCT',
77
+ targets: [
78
+ {
79
+ cartLine: {
80
+ id: maxCartLine.id,
68
81
},
69
82
},
83
+ ],
84
+ value: {
85
+ percentage: {
86
+ value: 20,
87
+ },
70
88
},
71
- ] ,
72
- selectionStrategy: ProductDiscountSelectionStrategy.First ,
73
- } ,
89
+ } ,
90
+ ] ,
91
+ selectionStrategy: ProductDiscountSelectionStrategy.First ,
74
92
},
75
- ],
93
+ });
94
+ }
95
+
96
+ return {
97
+ operations,
76
98
};
77
99
}
78
100
{%- elsif flavor contains "typescript" -%}
79
101
import {
102
+ DiscountClass,
80
103
OrderDiscountSelectionStrategy,
81
104
ProductDiscountSelectionStrategy,
82
105
CartInput,
@@ -88,60 +111,80 @@ export function generateCartRun(input: CartInput): CartLinesDiscountsGenerateRun
88
111
throw new Error('No cart lines found');
89
112
}
90
113
114
+ const hasOrderDiscountClass = input.discount.discountClasses.includes(
115
+ DiscountClass.Order
116
+ );
117
+
118
+ const hasProductDiscountClass = input.discount.discountClasses.includes(
119
+ DiscountClass.Product
120
+ );
121
+
122
+ if (!hasOrderDiscountClass && !hasProductDiscountClass) {
123
+ return { operations: [] };
124
+ }
125
+
91
126
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
92
127
if (line.cost.subtotalAmount.amount > maxLine.cost.subtotalAmount.amount) {
93
128
return line;
94
129
}
95
130
return maxLine;
96
131
}, input.cart.lines[0]);
97
132
98
- return {
99
- operations: [
100
- {
101
- orderDiscountsAdd: {
102
- candidates: [
103
- {
104
- message: '10% OFF ORDER',
105
- targets: [
106
- {
107
- orderSubtotal: {
108
- excludedCartLineIds: [],
109
- },
110
- },
111
- ],
112
- value: {
113
- percentage: {
114
- value: 10,
133
+ const operations: CartLinesDiscountsGenerateRunResult['operations'] = [];
134
+
135
+ if (hasOrderDiscountClass) {
136
+ operations.push({
137
+ orderDiscountsAdd: {
138
+ candidates: [
139
+ {
140
+ message: '10% OFF ORDER',
141
+ targets: [
142
+ {
143
+ orderSubtotal: {
144
+ excludedCartLineIds: [],
115
145
},
116
146
},
147
+ ],
148
+ value: {
149
+ percentage: {
150
+ value: 10,
151
+ },
117
152
},
118
- ],
119
- selectionStrategy: OrderDiscountSelectionStrategy.First,
120
- },
121
- },
122
- {
123
- productDiscountsAdd: {
124
- candidates: [
125
- {
126
- message: '20% OFF PRODUCT',
127
- targets: [
128
- {
129
- cartLine: {
130
- id: maxCartLine.id,
131
- },
132
- },
133
- ],
134
- value: {
135
- percentage: {
136
- value: 20,
153
+ },
154
+ ],
155
+ selectionStrategy: OrderDiscountSelectionStrategy.First,
156
+ }
157
+ });
158
+ }
159
+
160
+ if (hasProductDiscountClass) {
161
+ operations.push({
162
+ productDiscountsAdd: {
163
+ candidates: [
164
+ {
165
+ message: '20% OFF PRODUCT',
166
+ targets: [
167
+ {
168
+ cartLine: {
169
+ id: maxCartLine.id,
137
170
},
138
171
},
172
+ ],
173
+ value: {
174
+ percentage: {
175
+ value: 20,
176
+ },
139
177
},
140
- ] ,
141
- selectionStrategy: ProductDiscountSelectionStrategy.First ,
142
- } ,
178
+ } ,
179
+ ] ,
180
+ selectionStrategy: ProductDiscountSelectionStrategy.First ,
143
181
},
144
- ],
182
+ });
183
+ };
184
+
185
+ return {
186
+ operations,
145
187
};
146
188
}
189
+
147
190
{%- endif -%}
0 commit comments