|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { Modules, PromotionStatus, PromotionType } from "@medusajs/utils" |
|
3
|
import { |
|
4
|
createAdminUser, |
|
5
|
generatePublishableKey, |
|
6
|
generateStoreHeaders, |
|
7
|
} from "../../../../helpers/create-admin-user" |
|
8
|
import { setupTaxStructure } from "../../../../modules/__tests__/fixtures/tax" |
|
9
|
import { medusaTshirtProduct } from "../../../__fixtures__/product" |
|
10
|
|
|
11
|
jest.setTimeout(500000) |
|
12
|
|
|
13
|
const adminHeaders = { |
|
14
|
headers: { "x-medusa-access-token": "test_token" }, |
|
15
|
} |
|
16
|
|
|
17
|
medusaIntegrationTestRunner({ |
|
18
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
19
|
describe("Admin Promotions API - Promotion Limits", () => { |
|
20
|
let appContainer |
|
21
|
let promotion |
|
22
|
let product |
|
23
|
let region |
|
24
|
let salesChannel |
|
25
|
let storeHeaders |
|
26
|
let shippingProfile |
|
27
|
let stockLocation |
|
28
|
let fulfillmentSet |
|
29
|
let shippingOption |
|
30
|
|
|
31
|
beforeAll(async () => { |
|
32
|
appContainer = getContainer() |
|
33
|
}) |
|
34
|
|
|
35
|
beforeAll(async () => { |
|
36
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
37
|
|
|
38
|
await setupTaxStructure(appContainer.resolve(Modules.TAX)) |
|
39
|
|
|
40
|
shippingProfile = ( |
|
41
|
await api.post( |
|
42
|
`/admin/shipping-profiles`, |
|
43
|
{ name: "default", type: "default" }, |
|
44
|
adminHeaders |
|
45
|
) |
|
46
|
).data.shipping_profile |
|
47
|
|
|
48
|
stockLocation = ( |
|
49
|
await api.post( |
|
50
|
`/admin/stock-locations`, |
|
51
|
{ name: "test location" }, |
|
52
|
adminHeaders |
|
53
|
) |
|
54
|
).data.stock_location |
|
55
|
|
|
56
|
const fulfillmentSets = ( |
|
57
|
await api.post( |
|
58
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
59
|
{ |
|
60
|
name: `Test-inventory`, |
|
61
|
type: "test-type", |
|
62
|
}, |
|
63
|
adminHeaders |
|
64
|
) |
|
65
|
).data.stock_location.fulfillment_sets |
|
66
|
|
|
67
|
fulfillmentSet = ( |
|
68
|
await api.post( |
|
69
|
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
|
70
|
{ |
|
71
|
name: `Test-inventory`, |
|
72
|
geo_zones: [{ type: "country", country_code: "US" }], |
|
73
|
}, |
|
74
|
adminHeaders |
|
75
|
) |
|
76
|
).data.fulfillment_set |
|
77
|
|
|
78
|
await api.post( |
|
79
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
80
|
{ add: ["manual_test-provider"] }, |
|
81
|
adminHeaders |
|
82
|
) |
|
83
|
|
|
84
|
shippingOption = ( |
|
85
|
await api.post( |
|
86
|
`/admin/shipping-options`, |
|
87
|
{ |
|
88
|
name: `Test shipping option ${fulfillmentSet.id}`, |
|
89
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
90
|
shipping_profile_id: shippingProfile.id, |
|
91
|
provider_id: "manual_test-provider", |
|
92
|
price_type: "flat", |
|
93
|
type: { |
|
94
|
label: "Test type", |
|
95
|
description: "Test description", |
|
96
|
code: "test-code", |
|
97
|
}, |
|
98
|
prices: [{ currency_code: "usd", amount: 1000 }], |
|
99
|
rules: [], |
|
100
|
}, |
|
101
|
adminHeaders |
|
102
|
) |
|
103
|
).data.shipping_option |
|
104
|
|
|
105
|
product = ( |
|
106
|
await api.post( |
|
107
|
`/admin/products`, |
|
108
|
{ ...medusaTshirtProduct, shipping_profile_id: shippingProfile.id }, |
|
109
|
adminHeaders |
|
110
|
) |
|
111
|
).data.product |
|
112
|
|
|
113
|
region = ( |
|
114
|
await api.post( |
|
115
|
`/admin/regions`, |
|
116
|
{ |
|
117
|
payment_providers: ["pp_system_default"], |
|
118
|
name: "Test Region", |
|
119
|
currency_code: "usd", |
|
120
|
countries: ["us"], |
|
121
|
}, |
|
122
|
adminHeaders |
|
123
|
) |
|
124
|
).data.region |
|
125
|
|
|
126
|
salesChannel = ( |
|
127
|
await api.post( |
|
128
|
`/admin/sales-channels`, |
|
129
|
{ name: "Test Sales Channel" }, |
|
130
|
adminHeaders |
|
131
|
) |
|
132
|
).data.sales_channel |
|
133
|
|
|
134
|
await api.post( |
|
135
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
136
|
{ add: [salesChannel.id] }, |
|
137
|
adminHeaders |
|
138
|
) |
|
139
|
|
|
140
|
const publishableKey = await generatePublishableKey(appContainer) |
|
141
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
142
|
|
|
143
|
await dbUtils.snapshot() |
|
144
|
}) |
|
145
|
|
|
146
|
describe("Create promotion with limit", () => { |
|
147
|
it("should create a promotion with a usage limit", async () => { |
|
148
|
const response = await api.post( |
|
149
|
`/admin/promotions`, |
|
150
|
{ |
|
151
|
code: "LIMITED_PROMO", |
|
152
|
type: PromotionType.STANDARD, |
|
153
|
status: PromotionStatus.ACTIVE, |
|
154
|
limit: 5, |
|
155
|
application_method: { |
|
156
|
type: "fixed", |
|
157
|
target_type: "items", |
|
158
|
allocation: "across", |
|
159
|
value: 100, |
|
160
|
currency_code: "usd", |
|
161
|
}, |
|
162
|
}, |
|
163
|
adminHeaders |
|
164
|
) |
|
165
|
|
|
166
|
expect(response.data.promotion).toEqual( |
|
167
|
expect.objectContaining({ |
|
168
|
code: "LIMITED_PROMO", |
|
169
|
limit: 5, |
|
170
|
used: 0, |
|
171
|
}) |
|
172
|
) |
|
173
|
}) |
|
174
|
|
|
175
|
it("should create a promotion without a limit (unlimited)", async () => { |
|
176
|
const response = await api.post( |
|
177
|
`/admin/promotions`, |
|
178
|
{ |
|
179
|
code: "UNLIMITED_PROMO", |
|
180
|
type: PromotionType.STANDARD, |
|
181
|
status: PromotionStatus.ACTIVE, |
|
182
|
application_method: { |
|
183
|
type: "fixed", |
|
184
|
target_type: "items", |
|
185
|
allocation: "across", |
|
186
|
value: 100, |
|
187
|
currency_code: "usd", |
|
188
|
}, |
|
189
|
}, |
|
190
|
adminHeaders |
|
191
|
) |
|
192
|
|
|
193
|
expect(response.data.promotion).toEqual( |
|
194
|
expect.objectContaining({ |
|
195
|
code: "UNLIMITED_PROMO", |
|
196
|
limit: null, |
|
197
|
used: 0, |
|
198
|
}) |
|
199
|
) |
|
200
|
}) |
|
201
|
|
|
202
|
it("should prevent creating automatic promotion with limit", async () => { |
|
203
|
const response = await api |
|
204
|
.post( |
|
205
|
`/admin/promotions`, |
|
206
|
{ |
|
207
|
code: "AUTO_PROMO", |
|
208
|
type: PromotionType.STANDARD, |
|
209
|
status: PromotionStatus.ACTIVE, |
|
210
|
is_automatic: true, |
|
211
|
limit: 5, |
|
212
|
application_method: { |
|
213
|
type: "fixed", |
|
214
|
target_type: "items", |
|
215
|
allocation: "across", |
|
216
|
value: 100, |
|
217
|
currency_code: "usd", |
|
218
|
}, |
|
219
|
}, |
|
220
|
adminHeaders |
|
221
|
) |
|
222
|
.catch((err) => { |
|
223
|
return err.response |
|
224
|
}) |
|
225
|
|
|
226
|
expect(response.status).toBe(400) |
|
227
|
expect(response.data.message).toContain( |
|
228
|
"Automatic promotions cannot have a usage limit" |
|
229
|
) |
|
230
|
}) |
|
231
|
}) |
|
232
|
|
|
233
|
describe("Complete order increments usage", () => { |
|
234
|
beforeEach(async () => { |
|
235
|
promotion = ( |
|
236
|
await api.post( |
|
237
|
`/admin/promotions`, |
|
238
|
{ |
|
239
|
code: "TEST_LIMIT", |
|
240
|
type: PromotionType.STANDARD, |
|
241
|
status: PromotionStatus.ACTIVE, |
|
242
|
limit: 3, |
|
243
|
application_method: { |
|
244
|
type: "fixed", |
|
245
|
target_type: "items", |
|
246
|
allocation: "across", |
|
247
|
value: 100, |
|
248
|
currency_code: "usd", |
|
249
|
}, |
|
250
|
}, |
|
251
|
adminHeaders |
|
252
|
) |
|
253
|
).data.promotion |
|
254
|
}) |
|
255
|
|
|
256
|
it("should increment used count when order is completed", async () => { |
|
257
|
const cart = ( |
|
258
|
await api.post( |
|
259
|
`/store/carts`, |
|
260
|
{ |
|
261
|
currency_code: "usd", |
|
262
|
sales_channel_id: salesChannel.id, |
|
263
|
region_id: region.id, |
|
264
|
shipping_address: { |
|
265
|
address_1: "test address 1", |
|
266
|
address_2: "test address 2", |
|
267
|
city: "SF", |
|
268
|
country_code: "US", |
|
269
|
province: "CA", |
|
270
|
postal_code: "94016", |
|
271
|
}, |
|
272
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
273
|
promo_codes: [promotion.code], |
|
274
|
}, |
|
275
|
storeHeaders |
|
276
|
) |
|
277
|
).data.cart |
|
278
|
|
|
279
|
expect(cart.promotions).toHaveLength(1) |
|
280
|
expect(cart.promotions[0].code).toBe(promotion.code) |
|
281
|
|
|
282
|
await api.post( |
|
283
|
`/store/carts/${cart.id}/shipping-methods`, |
|
284
|
{ option_id: shippingOption.id }, |
|
285
|
storeHeaders |
|
286
|
) |
|
287
|
|
|
288
|
const paymentCollection = ( |
|
289
|
await api.post( |
|
290
|
`/store/payment-collections`, |
|
291
|
{ cart_id: cart.id }, |
|
292
|
storeHeaders |
|
293
|
) |
|
294
|
).data.payment_collection |
|
295
|
|
|
296
|
await api.post( |
|
297
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
298
|
{ provider_id: "pp_system_default" }, |
|
299
|
storeHeaders |
|
300
|
) |
|
301
|
|
|
302
|
const order = ( |
|
303
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
304
|
).data.order |
|
305
|
|
|
306
|
expect(order).toBeDefined() |
|
307
|
|
|
308
|
const updatedPromotion = ( |
|
309
|
await api.get(`/admin/promotions/${promotion.id}`, adminHeaders) |
|
310
|
).data.promotion |
|
311
|
|
|
312
|
expect(updatedPromotion.used).toBe(1) |
|
313
|
}) |
|
314
|
|
|
315
|
it("should not increment used count when promotion is only added to cart", async () => { |
|
316
|
// Create cart with promotion but don't complete |
|
317
|
const cart = ( |
|
318
|
await api.post( |
|
319
|
`/store/carts`, |
|
320
|
{ |
|
321
|
currency_code: "usd", |
|
322
|
sales_channel_id: salesChannel.id, |
|
323
|
region_id: region.id, |
|
324
|
shipping_address: { |
|
325
|
address_1: "test address 1", |
|
326
|
address_2: "test address 2", |
|
327
|
city: "SF", |
|
328
|
country_code: "US", |
|
329
|
province: "CA", |
|
330
|
postal_code: "94016", |
|
331
|
}, |
|
332
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
333
|
promo_codes: [promotion.code], |
|
334
|
}, |
|
335
|
storeHeaders |
|
336
|
) |
|
337
|
).data.cart |
|
338
|
|
|
339
|
expect(cart.promotions).toHaveLength(1) |
|
340
|
|
|
341
|
// Check promotion usage was NOT incremented |
|
342
|
const updatedPromotion = ( |
|
343
|
await api.get(`/admin/promotions/${promotion.id}`, adminHeaders) |
|
344
|
).data.promotion |
|
345
|
|
|
346
|
expect(updatedPromotion.used).toBe(0) |
|
347
|
}) |
|
348
|
}) |
|
349
|
|
|
350
|
describe("Limit enforcement on cart completion", () => { |
|
351
|
beforeEach(async () => { |
|
352
|
promotion = ( |
|
353
|
await api.post( |
|
354
|
`/admin/promotions`, |
|
355
|
{ |
|
356
|
code: "LIMIT_2", |
|
357
|
type: PromotionType.STANDARD, |
|
358
|
status: PromotionStatus.ACTIVE, |
|
359
|
limit: 2, |
|
360
|
application_method: { |
|
361
|
type: "fixed", |
|
362
|
target_type: "items", |
|
363
|
allocation: "across", |
|
364
|
value: 100, |
|
365
|
currency_code: "usd", |
|
366
|
}, |
|
367
|
}, |
|
368
|
adminHeaders |
|
369
|
) |
|
370
|
).data.promotion |
|
371
|
}) |
|
372
|
|
|
373
|
it("should allow completing 2 orders successfully", async () => { |
|
374
|
// Complete first cart |
|
375
|
const cart1 = ( |
|
376
|
await api.post( |
|
377
|
`/store/carts`, |
|
378
|
{ |
|
379
|
currency_code: "usd", |
|
380
|
sales_channel_id: salesChannel.id, |
|
381
|
region_id: region.id, |
|
382
|
shipping_address: { |
|
383
|
address_1: "test address 1", |
|
384
|
address_2: "test address 2", |
|
385
|
city: "SF", |
|
386
|
country_code: "US", |
|
387
|
province: "CA", |
|
388
|
postal_code: "94016", |
|
389
|
}, |
|
390
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
391
|
promo_codes: [promotion.code], |
|
392
|
}, |
|
393
|
storeHeaders |
|
394
|
) |
|
395
|
).data.cart |
|
396
|
|
|
397
|
// Setup first cart |
|
398
|
await api.post( |
|
399
|
`/store/carts/${cart1.id}/shipping-methods`, |
|
400
|
{ option_id: shippingOption.id }, |
|
401
|
storeHeaders |
|
402
|
) |
|
403
|
|
|
404
|
const paymentCollection1 = ( |
|
405
|
await api.post( |
|
406
|
`/store/payment-collections`, |
|
407
|
{ cart_id: cart1.id }, |
|
408
|
storeHeaders |
|
409
|
) |
|
410
|
).data.payment_collection |
|
411
|
|
|
412
|
await api.post( |
|
413
|
`/store/payment-collections/${paymentCollection1.id}/payment-sessions`, |
|
414
|
{ provider_id: "pp_system_default" }, |
|
415
|
storeHeaders |
|
416
|
) |
|
417
|
|
|
418
|
await api.post(`/store/carts/${cart1.id}/complete`, {}, storeHeaders) |
|
419
|
|
|
420
|
// Complete second cart |
|
421
|
const cart2 = ( |
|
422
|
await api.post( |
|
423
|
`/store/carts`, |
|
424
|
{ |
|
425
|
currency_code: "usd", |
|
426
|
sales_channel_id: salesChannel.id, |
|
427
|
region_id: region.id, |
|
428
|
shipping_address: { |
|
429
|
address_1: "test address 1", |
|
430
|
address_2: "test address 2", |
|
431
|
city: "SF", |
|
432
|
country_code: "US", |
|
433
|
province: "CA", |
|
434
|
postal_code: "94016", |
|
435
|
}, |
|
436
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
437
|
promo_codes: [promotion.code], |
|
438
|
}, |
|
439
|
storeHeaders |
|
440
|
) |
|
441
|
).data.cart |
|
442
|
|
|
443
|
// Setup second cart |
|
444
|
await api.post( |
|
445
|
`/store/carts/${cart2.id}/shipping-methods`, |
|
446
|
{ option_id: shippingOption.id }, |
|
447
|
storeHeaders |
|
448
|
) |
|
449
|
|
|
450
|
const paymentCollection2 = ( |
|
451
|
await api.post( |
|
452
|
`/store/payment-collections`, |
|
453
|
{ cart_id: cart2.id }, |
|
454
|
storeHeaders |
|
455
|
) |
|
456
|
).data.payment_collection |
|
457
|
|
|
458
|
await api.post( |
|
459
|
`/store/payment-collections/${paymentCollection2.id}/payment-sessions`, |
|
460
|
{ provider_id: "pp_system_default" }, |
|
461
|
storeHeaders |
|
462
|
) |
|
463
|
|
|
464
|
await api.post(`/store/carts/${cart2.id}/complete`, {}, storeHeaders) |
|
465
|
|
|
466
|
const updatedPromotion = ( |
|
467
|
await api.get(`/admin/promotions/${promotion.id}`, adminHeaders) |
|
468
|
).data.promotion |
|
469
|
|
|
470
|
expect(updatedPromotion.used).toBe(2) |
|
471
|
}) |
|
472
|
|
|
473
|
it("should not add promotion to the third cart when limit is exceeded", async () => { |
|
474
|
// Complete first two orders |
|
475
|
for (let i = 0; i < 2; i++) { |
|
476
|
const cart = ( |
|
477
|
await api.post( |
|
478
|
`/store/carts`, |
|
479
|
{ |
|
480
|
currency_code: "usd", |
|
481
|
sales_channel_id: salesChannel.id, |
|
482
|
region_id: region.id, |
|
483
|
shipping_address: { |
|
484
|
address_1: "test address 1", |
|
485
|
address_2: "test address 2", |
|
486
|
city: "SF", |
|
487
|
country_code: "US", |
|
488
|
province: "CA", |
|
489
|
postal_code: "94016", |
|
490
|
}, |
|
491
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
492
|
promo_codes: [promotion.code], |
|
493
|
}, |
|
494
|
storeHeaders |
|
495
|
) |
|
496
|
).data.cart |
|
497
|
|
|
498
|
await api.post( |
|
499
|
`/store/carts/${cart.id}/shipping-methods`, |
|
500
|
{ option_id: shippingOption.id }, |
|
501
|
storeHeaders |
|
502
|
) |
|
503
|
|
|
504
|
const paymentCollection = ( |
|
505
|
await api.post( |
|
506
|
`/store/payment-collections`, |
|
507
|
{ cart_id: cart.id }, |
|
508
|
storeHeaders |
|
509
|
) |
|
510
|
).data.payment_collection |
|
511
|
|
|
512
|
await api.post( |
|
513
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
514
|
{ provider_id: "pp_system_default" }, |
|
515
|
storeHeaders |
|
516
|
) |
|
517
|
|
|
518
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
519
|
} |
|
520
|
|
|
521
|
// Third cart should fail |
|
522
|
const cart3 = ( |
|
523
|
await api.post( |
|
524
|
`/store/carts?fields=*promotions.*`, |
|
525
|
{ |
|
526
|
currency_code: "usd", |
|
527
|
sales_channel_id: salesChannel.id, |
|
528
|
region_id: region.id, |
|
529
|
shipping_address: { |
|
530
|
address_1: "test address 1", |
|
531
|
address_2: "test address 2", |
|
532
|
city: "SF", |
|
533
|
country_code: "US", |
|
534
|
province: "CA", |
|
535
|
postal_code: "94016", |
|
536
|
}, |
|
537
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
538
|
promo_codes: [promotion.code], |
|
539
|
}, |
|
540
|
storeHeaders |
|
541
|
) |
|
542
|
).data.cart |
|
543
|
|
|
544
|
expect(cart3.promotions).toHaveLength(0) // promotion cannot be appleied since action "PROMOTION EXCEEDED LIMIT" is returned |
|
545
|
}) |
|
546
|
|
|
547
|
it("should fail third cart completion with limit exceeded", async () => { |
|
548
|
const carts = [] as any[] |
|
549
|
// Complete first two orders |
|
550
|
for (let i = 0; i < 3; i++) { |
|
551
|
const cart = ( |
|
552
|
await api.post( |
|
553
|
`/store/carts`, |
|
554
|
{ |
|
555
|
currency_code: "usd", |
|
556
|
sales_channel_id: salesChannel.id, |
|
557
|
region_id: region.id, |
|
558
|
shipping_address: { |
|
559
|
address_1: "test address 1", |
|
560
|
address_2: "test address 2", |
|
561
|
city: "SF", |
|
562
|
country_code: "US", |
|
563
|
province: "CA", |
|
564
|
postal_code: "94016", |
|
565
|
}, |
|
566
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
567
|
promo_codes: [promotion.code], |
|
568
|
}, |
|
569
|
storeHeaders |
|
570
|
) |
|
571
|
).data.cart |
|
572
|
|
|
573
|
await api.post( |
|
574
|
`/store/carts/${cart.id}/shipping-methods`, |
|
575
|
{ option_id: shippingOption.id }, |
|
576
|
storeHeaders |
|
577
|
) |
|
578
|
|
|
579
|
const paymentCollection = ( |
|
580
|
await api.post( |
|
581
|
`/store/payment-collections`, |
|
582
|
{ cart_id: cart.id }, |
|
583
|
storeHeaders |
|
584
|
) |
|
585
|
).data.payment_collection |
|
586
|
|
|
587
|
await api.post( |
|
588
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
589
|
{ provider_id: "pp_system_default" }, |
|
590
|
storeHeaders |
|
591
|
) |
|
592
|
|
|
593
|
carts.push(cart) |
|
594
|
} |
|
595
|
|
|
596
|
// complete first 2 carts |
|
597
|
for (let i = 0; i < 2; i++) { |
|
598
|
await api.post( |
|
599
|
`/store/carts/${carts[i].id}/complete`, |
|
600
|
{}, |
|
601
|
storeHeaders |
|
602
|
) |
|
603
|
} |
|
604
|
|
|
605
|
// Third cart should fail |
|
606
|
const cart3 = carts[2] |
|
607
|
|
|
608
|
const response = await api |
|
609
|
.post(`/store/carts/${cart3.id}/complete`, {}, storeHeaders) |
|
610
|
.catch((err) => { |
|
611
|
return err.response |
|
612
|
}) |
|
613
|
|
|
614
|
expect(response.status).toBe(400) |
|
615
|
expect(response.data.message).toContain( |
|
616
|
"Promotion usage exceeds the limit" |
|
617
|
) |
|
618
|
}) |
|
619
|
}) |
|
620
|
|
|
621
|
describe("Update limit validation", () => { |
|
622
|
beforeEach(async () => { |
|
623
|
promotion = ( |
|
624
|
await api.post( |
|
625
|
`/admin/promotions`, |
|
626
|
{ |
|
627
|
code: "UPDATE_LIMIT", |
|
628
|
type: PromotionType.STANDARD, |
|
629
|
status: PromotionStatus.ACTIVE, |
|
630
|
limit: 10, |
|
631
|
application_method: { |
|
632
|
type: "fixed", |
|
633
|
target_type: "items", |
|
634
|
allocation: "across", |
|
635
|
value: 100, |
|
636
|
currency_code: "usd", |
|
637
|
}, |
|
638
|
}, |
|
639
|
adminHeaders |
|
640
|
) |
|
641
|
).data.promotion |
|
642
|
}) |
|
643
|
|
|
644
|
it("should allow updating limit to higher value", async () => { |
|
645
|
const response = await api.post( |
|
646
|
`/admin/promotions/${promotion.id}`, |
|
647
|
{ |
|
648
|
limit: 20, |
|
649
|
}, |
|
650
|
adminHeaders |
|
651
|
) |
|
652
|
|
|
653
|
expect(response.data.promotion.limit).toBe(20) |
|
654
|
}) |
|
655
|
|
|
656
|
it("should prevent updating limit to less than current usage", async () => { |
|
657
|
// Complete two order to set used = 2 |
|
658
|
for (let i = 0; i < 2; i++) { |
|
659
|
const cart = ( |
|
660
|
await api.post( |
|
661
|
`/store/carts`, |
|
662
|
{ |
|
663
|
currency_code: "usd", |
|
664
|
sales_channel_id: salesChannel.id, |
|
665
|
region_id: region.id, |
|
666
|
shipping_address: { |
|
667
|
address_1: "test address 1", |
|
668
|
address_2: "test address 2", |
|
669
|
city: "SF", |
|
670
|
country_code: "US", |
|
671
|
province: "CA", |
|
672
|
postal_code: "94016", |
|
673
|
}, |
|
674
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
675
|
promo_codes: [promotion.code], |
|
676
|
}, |
|
677
|
storeHeaders |
|
678
|
) |
|
679
|
).data.cart |
|
680
|
|
|
681
|
await api.post( |
|
682
|
`/store/carts/${cart.id}/shipping-methods`, |
|
683
|
{ option_id: shippingOption.id }, |
|
684
|
storeHeaders |
|
685
|
) |
|
686
|
|
|
687
|
const paymentCollection = ( |
|
688
|
await api.post( |
|
689
|
`/store/payment-collections`, |
|
690
|
{ cart_id: cart.id }, |
|
691
|
storeHeaders |
|
692
|
) |
|
693
|
).data.payment_collection |
|
694
|
|
|
695
|
await api.post( |
|
696
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
697
|
{ provider_id: "pp_system_default" }, |
|
698
|
storeHeaders |
|
699
|
) |
|
700
|
|
|
701
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
702
|
} |
|
703
|
|
|
704
|
// Try to update limit to 0 (less than used = 2) |
|
705
|
const response = await api |
|
706
|
.post( |
|
707
|
`/admin/promotions/${promotion.id}`, |
|
708
|
{ |
|
709
|
limit: 1, |
|
710
|
}, |
|
711
|
adminHeaders |
|
712
|
) |
|
713
|
.catch((err) => { |
|
714
|
return err.response |
|
715
|
}) |
|
716
|
|
|
717
|
expect(response.status).toBe(400) |
|
718
|
expect(response.data.message).toContain( |
|
719
|
"cannot be less than current usage" |
|
720
|
) |
|
721
|
}) |
|
722
|
|
|
723
|
it("should allow updating limit to 2 when used is 1", async () => { |
|
724
|
// Complete one order to set used = 1 |
|
725
|
const cart = ( |
|
726
|
await api.post( |
|
727
|
`/store/carts`, |
|
728
|
{ |
|
729
|
currency_code: "usd", |
|
730
|
sales_channel_id: salesChannel.id, |
|
731
|
region_id: region.id, |
|
732
|
shipping_address: { |
|
733
|
address_1: "test address 1", |
|
734
|
address_2: "test address 2", |
|
735
|
city: "SF", |
|
736
|
country_code: "US", |
|
737
|
province: "CA", |
|
738
|
postal_code: "94016", |
|
739
|
}, |
|
740
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
741
|
promo_codes: [promotion.code], |
|
742
|
}, |
|
743
|
storeHeaders |
|
744
|
) |
|
745
|
).data.cart |
|
746
|
|
|
747
|
await api.post( |
|
748
|
`/store/carts/${cart.id}/shipping-methods`, |
|
749
|
{ option_id: shippingOption.id }, |
|
750
|
storeHeaders |
|
751
|
) |
|
752
|
|
|
753
|
const paymentCollection = ( |
|
754
|
await api.post( |
|
755
|
`/store/payment-collections`, |
|
756
|
{ cart_id: cart.id }, |
|
757
|
storeHeaders |
|
758
|
) |
|
759
|
).data.payment_collection |
|
760
|
|
|
761
|
await api.post( |
|
762
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
763
|
{ provider_id: "pp_system_default" }, |
|
764
|
storeHeaders |
|
765
|
) |
|
766
|
|
|
767
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
768
|
|
|
769
|
// Update limit to 2 |
|
770
|
const response = await api.post( |
|
771
|
`/admin/promotions/${promotion.id}`, |
|
772
|
{ |
|
773
|
limit: 2, |
|
774
|
}, |
|
775
|
adminHeaders |
|
776
|
) |
|
777
|
|
|
778
|
expect(response.data.promotion.limit).toBe(2) |
|
779
|
expect(response.data.promotion.used).toBe(1) |
|
780
|
}) |
|
781
|
}) |
|
782
|
|
|
783
|
describe("Both campaign and promotion limits", () => { |
|
784
|
let campaign |
|
785
|
let campaignPromotion |
|
786
|
|
|
787
|
beforeEach(async () => { |
|
788
|
// Create campaign with budget limit of 3 |
|
789
|
campaign = ( |
|
790
|
await api.post( |
|
791
|
`/admin/campaigns`, |
|
792
|
{ |
|
793
|
name: "Test Campaign", |
|
794
|
campaign_identifier: "test-campaign", |
|
795
|
budget: { |
|
796
|
type: "usage", |
|
797
|
limit: 3, |
|
798
|
}, |
|
799
|
}, |
|
800
|
adminHeaders |
|
801
|
) |
|
802
|
).data.campaign |
|
803
|
|
|
804
|
// Create promotion with limit of 2 |
|
805
|
campaignPromotion = ( |
|
806
|
await api.post( |
|
807
|
`/admin/promotions`, |
|
808
|
{ |
|
809
|
code: "CAMPAIGN_LIMIT", |
|
810
|
type: PromotionType.STANDARD, |
|
811
|
status: PromotionStatus.ACTIVE, |
|
812
|
limit: 2, |
|
813
|
campaign_id: campaign.id, |
|
814
|
application_method: { |
|
815
|
type: "fixed", |
|
816
|
target_type: "items", |
|
817
|
allocation: "across", |
|
818
|
value: 100, |
|
819
|
currency_code: "usd", |
|
820
|
}, |
|
821
|
}, |
|
822
|
adminHeaders |
|
823
|
) |
|
824
|
).data.promotion |
|
825
|
}) |
|
826
|
|
|
827
|
it("should hit promotion limit first ", async () => { |
|
828
|
// Complete 2 orders - should hit promotion limit |
|
829
|
for (let i = 0; i < 2; i++) { |
|
830
|
const cart = ( |
|
831
|
await api.post( |
|
832
|
`/store/carts`, |
|
833
|
{ |
|
834
|
currency_code: "usd", |
|
835
|
sales_channel_id: salesChannel.id, |
|
836
|
region_id: region.id, |
|
837
|
shipping_address: { |
|
838
|
address_1: "test address 1", |
|
839
|
address_2: "test address 2", |
|
840
|
city: "SF", |
|
841
|
country_code: "US", |
|
842
|
province: "CA", |
|
843
|
postal_code: "94016", |
|
844
|
}, |
|
845
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
846
|
promo_codes: [campaignPromotion.code], |
|
847
|
}, |
|
848
|
storeHeaders |
|
849
|
) |
|
850
|
).data.cart |
|
851
|
|
|
852
|
await api.post( |
|
853
|
`/store/carts/${cart.id}/shipping-methods`, |
|
854
|
{ option_id: shippingOption.id }, |
|
855
|
storeHeaders |
|
856
|
) |
|
857
|
|
|
858
|
const paymentCollection = ( |
|
859
|
await api.post( |
|
860
|
`/store/payment-collections`, |
|
861
|
{ cart_id: cart.id }, |
|
862
|
storeHeaders |
|
863
|
) |
|
864
|
).data.payment_collection |
|
865
|
|
|
866
|
await api.post( |
|
867
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
868
|
{ provider_id: "pp_system_default" }, |
|
869
|
storeHeaders |
|
870
|
) |
|
871
|
|
|
872
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
873
|
} |
|
874
|
|
|
875
|
// Third order should fail with promotion limit exceeded |
|
876
|
const cart3 = ( |
|
877
|
await api.post( |
|
878
|
`/store/carts`, |
|
879
|
{ |
|
880
|
currency_code: "usd", |
|
881
|
sales_channel_id: salesChannel.id, |
|
882
|
region_id: region.id, |
|
883
|
shipping_address: { |
|
884
|
address_1: "test address 1", |
|
885
|
address_2: "test address 2", |
|
886
|
city: "SF", |
|
887
|
country_code: "US", |
|
888
|
province: "CA", |
|
889
|
postal_code: "94016", |
|
890
|
}, |
|
891
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
892
|
promo_codes: [campaignPromotion.code], |
|
893
|
}, |
|
894
|
storeHeaders |
|
895
|
) |
|
896
|
).data.cart |
|
897
|
|
|
898
|
expect(cart3.promotions).toHaveLength(0) |
|
899
|
}) |
|
900
|
|
|
901
|
it("should hit campaign limit first", async () => { |
|
902
|
await api.post( |
|
903
|
`/admin/promotions/${campaignPromotion.id}`, |
|
904
|
{ |
|
905
|
limit: 5, |
|
906
|
}, |
|
907
|
adminHeaders |
|
908
|
) |
|
909
|
// Complete 3 orders - should hit campaign limit |
|
910
|
for (let i = 0; i < 3; i++) { |
|
911
|
const cart = ( |
|
912
|
await api.post( |
|
913
|
`/store/carts`, |
|
914
|
{ |
|
915
|
currency_code: "usd", |
|
916
|
sales_channel_id: salesChannel.id, |
|
917
|
region_id: region.id, |
|
918
|
shipping_address: { |
|
919
|
address_1: "test address 1", |
|
920
|
address_2: "test address 2", |
|
921
|
city: "SF", |
|
922
|
country_code: "US", |
|
923
|
province: "CA", |
|
924
|
postal_code: "94016", |
|
925
|
}, |
|
926
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
927
|
promo_codes: [campaignPromotion.code], |
|
928
|
}, |
|
929
|
storeHeaders |
|
930
|
) |
|
931
|
).data.cart |
|
932
|
|
|
933
|
await api.post( |
|
934
|
`/store/carts/${cart.id}/shipping-methods`, |
|
935
|
{ option_id: shippingOption.id }, |
|
936
|
storeHeaders |
|
937
|
) |
|
938
|
|
|
939
|
const paymentCollection = ( |
|
940
|
await api.post( |
|
941
|
`/store/payment-collections`, |
|
942
|
{ cart_id: cart.id }, |
|
943
|
storeHeaders |
|
944
|
) |
|
945
|
).data.payment_collection |
|
946
|
|
|
947
|
await api.post( |
|
948
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
949
|
{ provider_id: "pp_system_default" }, |
|
950
|
storeHeaders |
|
951
|
) |
|
952
|
|
|
953
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
954
|
} |
|
955
|
|
|
956
|
const cart4 = ( |
|
957
|
await api.post( |
|
958
|
`/store/carts`, |
|
959
|
{ |
|
960
|
currency_code: "usd", |
|
961
|
sales_channel_id: salesChannel.id, |
|
962
|
region_id: region.id, |
|
963
|
shipping_address: { |
|
964
|
address_1: "test address 1", |
|
965
|
address_2: "test address 2", |
|
966
|
city: "SF", |
|
967
|
country_code: "US", |
|
968
|
province: "CA", |
|
969
|
postal_code: "94016", |
|
970
|
}, |
|
971
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
972
|
promo_codes: [campaignPromotion.code], |
|
973
|
}, |
|
974
|
storeHeaders |
|
975
|
) |
|
976
|
).data.cart |
|
977
|
|
|
978
|
expect(cart4.promotions).toHaveLength(0) |
|
979
|
}) |
|
980
|
}) |
|
981
|
}) |
|
982
|
}, |
|
983
|
}) |