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: Add promotions to 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 promotionModuleService = appContainer.resolve(Modules.PROMOTION)
35 remoteLinkService = appContainer.resolve(
36 ContainerRegistrationKeys.REMOTE_LINK
37 )
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("POST /store/carts/:id/promotions", () => {
49 it("should add line item adjustments to 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 createdPromotion =
74 await promotionModuleService.createPromotions({
75 code: "PROMOTION_TEST",
76 type: PromotionType.STANDARD,
77 status: PromotionStatus.ACTIVE,
78 application_method: {
79 type: "fixed",
80 target_type: "items",
81 allocation: "across",
82 value: 1000,
83 apply_to_quantity: 1,
84 currency_code: "usd",
85 target_rules: [
86 {
87 attribute: "items.product_id",
88 operator: "eq",
89 values: "prod_mat",
90 },
91 ],
92 },
93 })
94
95 const cart = await cartModuleService.createCarts({
96 currency_code: "usd",
97 items: [
98 // Adjustment to add
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 // This adjustment will be removed and recreated
107 {
108 id: "item-2",
109 unit_price: 1000,
110 quantity: 1,
111 title: "Test item",
112 product_id: "prod_tshirt",
113 } as any,
114 ],
115 })
116
117 // Adjustment to keep
118 const [lineItemAdjustment] =
119 await cartModuleService.addLineItemAdjustments([
120 {
121 code: appliedPromotion.code!,
122 amount: 300,
123 item_id: "item-2",
124 promotion_id: appliedPromotion.id,
125 },
126 ])
127
128 await remoteLinkService.create({
129 [Modules.CART]: { cart_id: cart.id },
130 [Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
131 })
132
133 const created = await api.post(
134 `/store/carts/${cart.id}/promotions`,
135 { promo_codes: [createdPromotion.code] },
136 storeHeaders
137 )
138
139 expect(created.status).toEqual(200)
140 expect(created.data.cart).toEqual(
141 expect.objectContaining({
142 id: expect.any(String),
143 items: expect.arrayContaining([
144 expect.objectContaining({
145 id: "item-1",
146 adjustments: expect.arrayContaining([
147 expect.objectContaining({
148 code: createdPromotion.code,
149 amount: 1000,
150 }),
151 ]),
152 }),
153 expect.objectContaining({
154 adjustments: expect.arrayContaining([
155 expect.objectContaining({
156 id: expect.not.stringContaining(lineItemAdjustment.id),
157 code: appliedPromotion.code,
158 amount: 300,
159 }),
160 ]),
161 }),
162 ]),
163 })
164 )
165 })
166
167 it("should add line item adjustments only for discountable items", async () => {
168 const createdPromotion =
169 await promotionModuleService.createPromotions({
170 code: "PROMOTION_TEST",
171 type: PromotionType.STANDARD,
172 status: PromotionStatus.ACTIVE,
173 application_method: {
174 type: "fixed",
175 target_type: "items",
176 allocation: "across",
177 value: 1000,
178 apply_to_quantity: 1,
179 currency_code: "usd",
180 },
181 })
182
183 const cart = await cartModuleService.createCarts({
184 currency_code: "usd",
185 items: [
186 {
187 id: "item-1",
188 unit_price: 2000,
189 quantity: 1,
190 title: "Test item",
191 product_id: "prod_mat",
192 } as any,
193 {
194 id: "item-2",
195 unit_price: 1000,
196 quantity: 1,
197 title: "Test item",
198 product_id: "prod_tshirt",
199 is_discountable: false,
200 } as any,
201 ],
202 })
203
204 const created = await api.post(
205 `/store/carts/${cart.id}/promotions`,
206 { promo_codes: [createdPromotion.code] },
207 storeHeaders
208 )
209
210 expect(created.status).toEqual(200)
211 expect(created.data.cart).toEqual(
212 expect.objectContaining({
213 id: expect.any(String),
214 items: expect.arrayContaining([
215 expect.objectContaining({
216 id: "item-1",
217 adjustments: expect.arrayContaining([
218 expect.objectContaining({
219 code: createdPromotion.code,
220 amount: 1000,
221 }),
222 ]),
223 }),
224 expect.objectContaining({
225 adjustments: [],
226 }),
227 ]),
228 })
229 )
230 })
231
232 it("should add shipping method adjustments to a cart based on promotions", async () => {
233 const [appliedPromotion] =
234 await promotionModuleService.createPromotions([
235 {
236 code: "PROMOTION_APPLIED",
237 type: PromotionType.STANDARD,
238 status: PromotionStatus.ACTIVE,
239 rules: [
240 {
241 attribute: "customer_id",
242 operator: "in",
243 values: ["cus_test"],
244 },
245 ],
246 application_method: {
247 type: "fixed",
248 target_type: "shipping_methods",
249 allocation: "each",
250 value: 100,
251 max_quantity: 1,
252 currency_code: "usd",
253 target_rules: [
254 {
255 attribute: "name",
256 operator: "in",
257 values: ["express"],
258 },
259 ],
260 },
261 },
262 ])
263
264 const [newPromotion] = await promotionModuleService.createPromotions([
265 {
266 code: "PROMOTION_NEW",
267 type: PromotionType.STANDARD,
268 status: PromotionStatus.ACTIVE,
269 rules: [
270 {
271 attribute: "customer_id",
272 operator: "in",
273 values: ["cus_test"],
274 },
275 ],
276 application_method: {
277 type: "fixed",
278 target_type: "shipping_methods",
279 allocation: "each",
280 value: 200,
281 max_quantity: 1,
282 currency_code: "usd",
283 target_rules: [
284 {
285 attribute: "name",
286 operator: "in",
287 values: ["express", "standard"],
288 },
289 ],
290 },
291 },
292 ])
293
294 const cart = await cartModuleService.createCarts({
295 currency_code: "usd",
296 customer_id: "cus_test",
297 items: [
298 {
299 unit_price: 2000,
300 quantity: 1,
301 title: "Test item",
302 product_id: "prod_mat",
303 } as any,
304 ],
305 })
306
307 const [express, standard] =
308 await cartModuleService.addShippingMethods(cart.id, [
309 {
310 amount: 500,
311 name: "express",
312 },
313 {
314 amount: 500,
315 name: "standard",
316 },
317 ])
318
319 await remoteLinkService.create({
320 [Modules.CART]: { cart_id: cart.id },
321 [Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
322 })
323
324 const [adjustment] =
325 await cartModuleService.addShippingMethodAdjustments(cart.id, [
326 {
327 shipping_method_id: express.id,
328 amount: 100,
329 code: appliedPromotion.code!,
330 },
331 ])
332
333 const created = await api.post(
334 `/store/carts/${cart.id}/promotions`,
335 { promo_codes: [newPromotion.code] },
336 storeHeaders
337 )
338
339 expect(created.status).toEqual(200)
340 expect(created.data.cart).toEqual(
341 expect.objectContaining({
342 id: expect.any(String),
343 items: expect.arrayContaining([
344 expect.objectContaining({
345 id: expect.any(String),
346 }),
347 ]),
348 shipping_methods: expect.arrayContaining([
349 expect.objectContaining({
350 id: express.id,
351 adjustments: expect.arrayContaining([
352 expect.objectContaining({
353 id: expect.any(String),
354 amount: 200,
355 code: newPromotion.code,
356 }),
357 expect.objectContaining({
358 id: expect.not.stringContaining(adjustment.id),
359 amount: 100,
360 code: appliedPromotion.code,
361 }),
362 ]),
363 }),
364 expect.objectContaining({
365 id: standard.id,
366 adjustments: [
367 expect.objectContaining({
368 id: expect.any(String),
369 amount: 200,
370 code: newPromotion.code,
371 }),
372 ],
373 }),
374 ]),
375 })
376 )
377 })
378 })
379 })
380 },
381 })