|
1
|
import { RemoteLink } from "@medusajs/modules-sdk" |
|
2
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { ICartModuleService, IPromotionModuleService } from "@medusajs/types" |
|
4
|
import { |
|
5
|
ContainerRegistrationKeys, |
|
6
|
Modules, |
|
7
|
PromotionStatus, |
|
8
|
PromotionType, |
|
9
|
} from "@medusajs/utils" |
|
10
|
import { |
|
11
|
adminHeaders, |
|
12
|
createAdminUser, |
|
13
|
generatePublishableKey, |
|
14
|
generateStoreHeaders, |
|
15
|
} from "../../../../helpers/create-admin-user" |
|
16
|
|
|
17
|
jest.setTimeout(50000) |
|
18
|
|
|
19
|
const env = {} |
|
20
|
|
|
21
|
medusaIntegrationTestRunner({ |
|
22
|
env, |
|
23
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
24
|
describe("Store Carts API: Remove promotions from cart", () => { |
|
25
|
let appContainer |
|
26
|
let cartModuleService: ICartModuleService |
|
27
|
let promotionModuleService: IPromotionModuleService |
|
28
|
let remoteLinkService: RemoteLink |
|
29
|
let storeHeaders |
|
30
|
|
|
31
|
beforeAll(async () => { |
|
32
|
appContainer = getContainer() |
|
33
|
cartModuleService = appContainer.resolve(Modules.CART) |
|
34
|
remoteLinkService = appContainer.resolve( |
|
35
|
ContainerRegistrationKeys.REMOTE_LINK |
|
36
|
) |
|
37
|
promotionModuleService = appContainer.resolve(Modules.PROMOTION) |
|
38
|
}) |
|
39
|
|
|
40
|
beforeAll(async () => { |
|
41
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
42
|
const publishableKey = await generatePublishableKey(appContainer) |
|
43
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
44
|
|
|
45
|
await dbUtils.snapshot() |
|
46
|
}) |
|
47
|
|
|
48
|
describe("DELETE /store/carts/:id/promotions", () => { |
|
49
|
it("should remove line item adjustments from a cart based on promotions", async () => { |
|
50
|
const appliedPromotion = |
|
51
|
await promotionModuleService.createPromotions({ |
|
52
|
code: "PROMOTION_APPLIED", |
|
53
|
type: PromotionType.STANDARD, |
|
54
|
status: PromotionStatus.ACTIVE, |
|
55
|
application_method: { |
|
56
|
type: "fixed", |
|
57
|
target_type: "items", |
|
58
|
allocation: "each", |
|
59
|
value: 300, |
|
60
|
apply_to_quantity: 1, |
|
61
|
currency_code: "usd", |
|
62
|
max_quantity: 1, |
|
63
|
target_rules: [ |
|
64
|
{ |
|
65
|
attribute: "items.product_id", |
|
66
|
operator: "eq", |
|
67
|
values: "prod_tshirt", |
|
68
|
}, |
|
69
|
], |
|
70
|
}, |
|
71
|
}) |
|
72
|
|
|
73
|
const appliedPromotionToRemove = |
|
74
|
await promotionModuleService.createPromotions({ |
|
75
|
code: "PROMOTION_APPLIED_TO_REMOVE", |
|
76
|
type: PromotionType.STANDARD, |
|
77
|
status: PromotionStatus.ACTIVE, |
|
78
|
application_method: { |
|
79
|
type: "fixed", |
|
80
|
target_type: "items", |
|
81
|
allocation: "each", |
|
82
|
value: 300, |
|
83
|
apply_to_quantity: 1, |
|
84
|
currency_code: "usd", |
|
85
|
max_quantity: 1, |
|
86
|
target_rules: [ |
|
87
|
{ |
|
88
|
attribute: "items.product_id", |
|
89
|
operator: "eq", |
|
90
|
values: "prod_tshirt", |
|
91
|
}, |
|
92
|
], |
|
93
|
}, |
|
94
|
}) |
|
95
|
|
|
96
|
const cart = await cartModuleService.createCarts({ |
|
97
|
currency_code: "usd", |
|
98
|
items: [ |
|
99
|
{ |
|
100
|
id: "item-1", |
|
101
|
unit_price: 2000, |
|
102
|
quantity: 1, |
|
103
|
title: "Test item", |
|
104
|
product_id: "prod_mat", |
|
105
|
} as any, |
|
106
|
{ |
|
107
|
id: "item-2", |
|
108
|
unit_price: 1000, |
|
109
|
quantity: 1, |
|
110
|
title: "Test item", |
|
111
|
product_id: "prod_tshirt", |
|
112
|
} as any, |
|
113
|
], |
|
114
|
}) |
|
115
|
|
|
116
|
const [adjustment1, adjustment2] = |
|
117
|
await cartModuleService.addLineItemAdjustments([ |
|
118
|
{ |
|
119
|
code: appliedPromotion.code!, |
|
120
|
amount: 300, |
|
121
|
item_id: "item-2", |
|
122
|
promotion_id: appliedPromotionToRemove.id, |
|
123
|
}, |
|
124
|
{ |
|
125
|
code: appliedPromotionToRemove.code!, |
|
126
|
amount: 150, |
|
127
|
item_id: "item-1", |
|
128
|
promotion_id: appliedPromotionToRemove.id, |
|
129
|
}, |
|
130
|
{ |
|
131
|
code: appliedPromotionToRemove.code!, |
|
132
|
amount: 150, |
|
133
|
item_id: "item-2", |
|
134
|
promotion_id: appliedPromotionToRemove.id, |
|
135
|
}, |
|
136
|
]) |
|
137
|
|
|
138
|
await remoteLinkService.create([ |
|
139
|
{ |
|
140
|
[Modules.CART]: { cart_id: cart.id }, |
|
141
|
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id }, |
|
142
|
}, |
|
143
|
{ |
|
144
|
[Modules.CART]: { cart_id: cart.id }, |
|
145
|
[Modules.PROMOTION]: { |
|
146
|
promotion_id: appliedPromotionToRemove.id, |
|
147
|
}, |
|
148
|
}, |
|
149
|
]) |
|
150
|
|
|
151
|
const response = await api.delete( |
|
152
|
`/store/carts/${cart.id}/promotions`, |
|
153
|
{ |
|
154
|
data: { |
|
155
|
promo_codes: [appliedPromotionToRemove.code], |
|
156
|
}, |
|
157
|
...storeHeaders, |
|
158
|
} |
|
159
|
) |
|
160
|
|
|
161
|
expect(response.status).toEqual(200) |
|
162
|
expect(response.data.cart).toEqual( |
|
163
|
expect.objectContaining({ |
|
164
|
id: expect.any(String), |
|
165
|
items: expect.arrayContaining([ |
|
166
|
expect.objectContaining({ |
|
167
|
id: "item-1", |
|
168
|
adjustments: [], |
|
169
|
}), |
|
170
|
expect.objectContaining({ |
|
171
|
id: "item-2", |
|
172
|
adjustments: [ |
|
173
|
expect.objectContaining({ |
|
174
|
amount: 300, |
|
175
|
code: appliedPromotion.code, |
|
176
|
}), |
|
177
|
], |
|
178
|
}), |
|
179
|
]), |
|
180
|
}) |
|
181
|
) |
|
182
|
}) |
|
183
|
|
|
184
|
it("should add shipping method adjustments to a cart based on promotions", async () => { |
|
185
|
const appliedPromotion = |
|
186
|
await promotionModuleService.createPromotions({ |
|
187
|
code: "PROMOTION_APPLIED", |
|
188
|
status: PromotionStatus.ACTIVE, |
|
189
|
type: PromotionType.STANDARD, |
|
190
|
rules: [ |
|
191
|
{ |
|
192
|
attribute: "customer_id", |
|
193
|
operator: "in", |
|
194
|
values: ["cus_test"], |
|
195
|
}, |
|
196
|
], |
|
197
|
application_method: { |
|
198
|
type: "fixed", |
|
199
|
target_type: "shipping_methods", |
|
200
|
allocation: "each", |
|
201
|
value: 100, |
|
202
|
max_quantity: 1, |
|
203
|
currency_code: "usd", |
|
204
|
target_rules: [ |
|
205
|
{ |
|
206
|
attribute: "name", |
|
207
|
operator: "in", |
|
208
|
values: ["express"], |
|
209
|
}, |
|
210
|
], |
|
211
|
}, |
|
212
|
}) |
|
213
|
|
|
214
|
const appliedPromotionToRemove = |
|
215
|
await promotionModuleService.createPromotions({ |
|
216
|
code: "PROMOTION_APPLIED_TO_REMOVE", |
|
217
|
type: PromotionType.STANDARD, |
|
218
|
status: PromotionStatus.ACTIVE, |
|
219
|
rules: [ |
|
220
|
{ |
|
221
|
attribute: "customer_id", |
|
222
|
operator: "in", |
|
223
|
values: ["cus_test"], |
|
224
|
}, |
|
225
|
], |
|
226
|
application_method: { |
|
227
|
type: "fixed", |
|
228
|
target_type: "shipping_methods", |
|
229
|
allocation: "each", |
|
230
|
value: 100, |
|
231
|
max_quantity: 1, |
|
232
|
currency_code: "usd", |
|
233
|
target_rules: [ |
|
234
|
{ |
|
235
|
attribute: "name", |
|
236
|
operator: "in", |
|
237
|
values: ["express"], |
|
238
|
}, |
|
239
|
], |
|
240
|
}, |
|
241
|
}) |
|
242
|
|
|
243
|
const cart = await cartModuleService.createCarts({ |
|
244
|
currency_code: "usd", |
|
245
|
customer_id: "cus_test", |
|
246
|
items: [ |
|
247
|
{ |
|
248
|
unit_price: 2000, |
|
249
|
quantity: 1, |
|
250
|
title: "Test item", |
|
251
|
product_id: "prod_mat", |
|
252
|
}, |
|
253
|
], |
|
254
|
}) |
|
255
|
|
|
256
|
const [express, standard] = |
|
257
|
await cartModuleService.addShippingMethods(cart.id, [ |
|
258
|
{ |
|
259
|
amount: 500, |
|
260
|
name: "express", |
|
261
|
}, |
|
262
|
{ |
|
263
|
amount: 500, |
|
264
|
name: "standard", |
|
265
|
}, |
|
266
|
]) |
|
267
|
|
|
268
|
await cartModuleService.addShippingMethodAdjustments(cart.id, [ |
|
269
|
{ |
|
270
|
shipping_method_id: express.id, |
|
271
|
amount: 100, |
|
272
|
code: appliedPromotion.code!, |
|
273
|
}, |
|
274
|
{ |
|
275
|
shipping_method_id: express.id, |
|
276
|
amount: 100, |
|
277
|
code: appliedPromotionToRemove.code!, |
|
278
|
}, |
|
279
|
]) |
|
280
|
|
|
281
|
await remoteLinkService.create([ |
|
282
|
{ |
|
283
|
[Modules.CART]: { cart_id: cart.id }, |
|
284
|
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id }, |
|
285
|
}, |
|
286
|
{ |
|
287
|
[Modules.CART]: { cart_id: cart.id }, |
|
288
|
[Modules.PROMOTION]: { |
|
289
|
promotion_id: appliedPromotionToRemove.id, |
|
290
|
}, |
|
291
|
}, |
|
292
|
]) |
|
293
|
|
|
294
|
const response = await api.delete( |
|
295
|
`/store/carts/${cart.id}/promotions`, |
|
296
|
{ |
|
297
|
data: { promo_codes: [appliedPromotionToRemove.code] }, |
|
298
|
...storeHeaders, |
|
299
|
} |
|
300
|
) |
|
301
|
|
|
302
|
expect(response.status).toEqual(200) |
|
303
|
expect(response.data.cart).toEqual( |
|
304
|
expect.objectContaining({ |
|
305
|
id: expect.any(String), |
|
306
|
items: expect.arrayContaining([ |
|
307
|
expect.objectContaining({ |
|
308
|
id: expect.any(String), |
|
309
|
}), |
|
310
|
]), |
|
311
|
shipping_methods: expect.arrayContaining([ |
|
312
|
expect.objectContaining({ |
|
313
|
id: express.id, |
|
314
|
adjustments: [ |
|
315
|
expect.objectContaining({ |
|
316
|
amount: 100, |
|
317
|
code: appliedPromotion.code, |
|
318
|
}), |
|
319
|
], |
|
320
|
}), |
|
321
|
expect.objectContaining({ |
|
322
|
id: standard.id, |
|
323
|
adjustments: [], |
|
324
|
}), |
|
325
|
]), |
|
326
|
}) |
|
327
|
) |
|
328
|
}) |
|
329
|
}) |
|
330
|
}) |
|
331
|
}, |
|
332
|
}) |