1 import {
2 CreatePromotionDTO,
3 IPromotionModuleService,
4 } from "@medusajs/framework/types"
5 import {
6 ApplicationMethodType,
7 CampaignBudgetType,
8 MathBN,
9 Modules,
10 PromotionStatus,
11 PromotionType,
12 } from "@medusajs/framework/utils"
13 import { moduleIntegrationTestRunner, SuiteOptions } from "@medusajs/test-utils"
14 import { createCampaigns } from "../../../__fixtures__/campaigns"
15 import { createDefaultPromotion } from "../../../__fixtures__/promotion"
16
17 jest.setTimeout(30000)
18
19 moduleIntegrationTestRunner({
20 moduleName: Modules.PROMOTION,
21 testSuite: ({
22 MikroOrmWrapper,
23 service,
24 }: SuiteOptions<IPromotionModuleService>) => {
25 describe("Promotion Service: computeActions", () => {
26 beforeEach(async () => {
27 await createCampaigns(MikroOrmWrapper.forkManager())
28 })
29
30 // A non-tax-inclusive promotion records its adjustment excl-tax, but a
31 // later tax-inclusive promotion measures the remaining applicable amount
32 // against the incl-tax `original_total`. Without reconciling the two tax
33 // bases, the stacked discounts can exceed the item value and drive the
34 // taxable base negative.
35 describe("when mixing tax-inclusive and non-tax-inclusive promotions", () => {
36 // Stacks a non-tax-inclusive then a tax-inclusive promotion on a taxed
37 // line item and returns the resulting taxable base (excl-tax).
38 const computeStackedTaxableBase = async (
39 allocation: "each" | "across"
40 ) => {
41 // 20% VAT: subtotal 3.50 (excl), original_total 4.20 (incl)
42 const item = {
43 id: "item_taxed",
44 quantity: 1,
45 subtotal: 3.5,
46 original_total: 4.2,
47 is_discountable: true,
48 product: { id: "prod_taxed" },
49 }
50
51 // max_quantity is required for "each" but forbidden for "across".
52 const quantityField = allocation === "each" ? { max_quantity: 1 } : {}
53
54 // Higher application_method.value => applied first. 10% of 3.50 = 0.35 excl-tax.
55 await createDefaultPromotion(service, {
56 code: "PROMO_NON_TAX_INCL",
57 is_tax_inclusive: false,
58 application_method: {
59 type: "percentage",
60 target_type: "items",
61 allocation,
62 value: 10,
63 ...quantityField,
64 } as any,
65 })
66
67 // Applied second. Tax-inclusive fixed promotion.
68 await createDefaultPromotion(service, {
69 code: "PROMO_TAX_INCL",
70 is_tax_inclusive: true,
71 application_method: {
72 type: "fixed",
73 target_type: "items",
74 allocation,
75 value: 5,
76 ...quantityField,
77 } as any,
78 })
79
80 const result = await service.computeActions(
81 ["PROMO_NON_TAX_INCL", "PROMO_TAX_INCL"],
82 {
83 currency_code: "usd",
84 items: [item],
85 }
86 )
87
88 const actions = JSON.parse(JSON.stringify(result)) as any[]
89
90 // Normalize every adjustment to its tax-exclusive equivalent.
91 const taxRatio = MathBN.div(item.original_total, item.subtotal)
92 const totalDiscountExclTax = actions
93 .filter(
94 (a) => a.action === "addItemAdjustment" && a.item_id === item.id
95 )
96 .reduce(
97 (acc, a) =>
98 MathBN.add(
99 acc,
100 a.is_tax_inclusive
101 ? MathBN.div(a.amount, taxRatio)
102 : a.amount
103 ),
104 MathBN.convert(0)
105 )
106
107 return MathBN.sub(item.subtotal, totalDiscountExclTax)
108 }
109
110 it("should not drive the taxable base negative when stacking (each allocation)", async () => {
111 const taxableBase = await computeStackedTaxableBase("each")
112 expect(MathBN.gte(taxableBase, 0)).toBe(true)
113 })
114
115 it("should not drive the taxable base negative when stacking (across allocation)", async () => {
116 const taxableBase = await computeStackedTaxableBase("across")
117 expect(MathBN.gte(taxableBase, 0)).toBe(true)
118 })
119 })
120
121 it("edge case: should only return promotions with no rules when context has no attributes", async () => {
122 // Create promotions with different rule configurations
123 const promotionsToCreate: CreatePromotionDTO[] = [
124 // Promotion with no rules - should be returned
125 {
126 code: "NO_RULES_PROMO",
127 is_automatic: true,
128 rules: [], // No global rules
129 application_method: {
130 type: "fixed",
131 target_type: "items",
132 allocation: "each",
133 max_quantity: 100000,
134 value: 100,
135 target_rules: [], // No target rules
136 },
137 type: "standard",
138 status: PromotionStatus.ACTIVE,
139 campaign_id: "campaign-id-1",
140 },
141 // Promotion with global rules - should NOT be returned
142 {
143 code: "WITH_GLOBAL_RULES",
144 is_automatic: true,
145 rules: [
146 {
147 attribute: "customer.id",
148 operator: "eq",
149 values: ["some_customer"],
150 },
151 ],
152 application_method: {
153 type: "fixed",
154 target_type: "items",
155 allocation: "each",
156 max_quantity: 100000,
157 value: 100,
158 target_rules: [],
159 },
160 type: "standard",
161 status: PromotionStatus.ACTIVE,
162 campaign_id: "campaign-id-1",
163 },
164 // Promotion with target rules - should NOT be returned
165 {
166 code: "WITH_TARGET_RULES",
167 is_automatic: true,
168 rules: [],
169 application_method: {
170 type: "fixed",
171 target_type: "items",
172 allocation: "each",
173 max_quantity: 100000,
174 value: 100,
175 target_rules: [
176 {
177 attribute: "items.product.id",
178 operator: "eq",
179 values: ["some_product"],
180 },
181 ],
182 },
183 type: "standard",
184 status: PromotionStatus.ACTIVE,
185 campaign_id: "campaign-id-1",
186 },
187 ]
188
189 const promotions = await service.createPromotions(promotionsToCreate)
190 const noRulePromotion = promotions.find(
191 (p) => p.code === "NO_RULES_PROMO"
192 )!
193
194 // Spy on the internal promotion service to verify prefiltering
195 let prefilterCallCount = 0
196 let prefilteredPromotions: any[] = []
197 const originalPromotionServiceList = (service as any).promotionService_
198 .list
199
200 ;(service as any).promotionService_.list = async (...args: any[]) => {
201 const result = await originalPromotionServiceList.bind(
202 (service as any).promotionService_
203 )(...args)
204
205 if (prefilterCallCount === 0) {
206 prefilteredPromotions = result
207 }
208 prefilterCallCount++
209
210 // Return nothing to not trigger future context checks
211 return []
212 }
213
214 // Empty context - no attributes at all, should trigger noRulesSubquery
215 const emptyContext = {}
216
217 const actions = await service.computeActions([], emptyContext as any)
218
219 ;(service as any).promotionService_.list = originalPromotionServiceList
220
221 // Should only return the promotion with no rules
222 expect(prefilteredPromotions).toHaveLength(1)
223 expect(prefilteredPromotions[0].id).toBe(noRulePromotion.id)
224
225 expect(actions).toHaveLength(0) // No items to apply to
226 })
227
228 it("should prefilter promotions by applicable rules", async () => {
229 // 1. Promotion with NO rules (should always apply if automatic)
230 await createDefaultPromotion(service, {
231 code: "NO_RULES_PROMO",
232 is_automatic: true,
233 rules: [], // No global rules - always applicable
234 application_method: {
235 type: "fixed",
236 target_type: "items",
237 allocation: "each",
238 max_quantity: 100000,
239 value: 100,
240 target_rules: [
241 {
242 attribute: "items.product.id",
243 operator: "eq",
244 values: ["prod_tshirt0"], // Only applies to product 0
245 },
246 ],
247 },
248 })
249
250 // 2. Promotion matching customer group VIP1
251 await createDefaultPromotion(service, {
252 code: "CUSTOMER_GROUP_PROMO",
253 is_automatic: true,
254 rules: [
255 {
256 attribute: "customer.customer_group.id",
257 operator: "in",
258 values: ["VIP1"], // Matches our test customer
259 },
260 ],
261 application_method: {
262 type: "fixed",
263 target_type: "items",
264 allocation: "each",
265 max_quantity: 100000,
266 value: 100,
267 target_rules: [
268 {
269 attribute: "items.product.id",
270 operator: "eq",
271 values: ["prod_tshirt1"], // Only applies to product 1
272 },
273 ],
274 },
275 })
276
277 // 3. Promotion with subtotal rule (should match items with subtotal > 50)
278 await createDefaultPromotion(service, {
279 code: "SUBTOTAL_PROMO",
280 is_automatic: true,
281 rules: [
282 {
283 attribute: "items.subtotal",
284 operator: "gt",
285 values: ["50"], // All our items have subtotal > 50
286 },
287 ],
288 application_method: {
289 type: "fixed",
290 target_type: "items",
291 allocation: "each",
292 max_quantity: 100000,
293 value: 100,
294 target_rules: [], // No target rules - applies to all items
295 },
296 })
297
298 // 4. Promotion matching customer.id
299 await createDefaultPromotion(service, {
300 code: "CUSTOMER_ID_PROMO",
301 is_automatic: true,
302 rules: [
303 {
304 attribute: "customer.id",
305 operator: "in",
306 values: ["customer"], // Matches our test customer
307 },
308 ],
309 application_method: {
310 type: "fixed",
311 target_type: "items",
312 allocation: "each",
313 max_quantity: 100000,
314 value: 250, // Different value to distinguish
315 target_rules: [
316 {
317 attribute: "items.product.id",
318 operator: "eq",
319 values: ["prod_tshirt9"], // Only applies to product 9
320 },
321 ],
322 },
323 })
324
325 // 5. Promotion that should NOT match (different customer group)
326 await createDefaultPromotion(service, {
327 code: "NO_MATCH_PROMO",
328 is_automatic: true,
329 rules: [
330 {
331 attribute: "customer.customer_group.id",
332 operator: "in",
333 values: ["VIP99"], // Different customer group - won't match
334 },
335 ],
336 application_method: {
337 type: "fixed",
338 target_type: "items",
339 allocation: "each",
340 max_quantity: 100000,
341 value: 100,
342 target_rules: [
343 {
344 attribute: "items.product.id",
345 operator: "eq",
346 values: ["prod_tshirt0"],
347 },
348 ],
349 },
350 })
351
352 // 6. Non-automatic promotion (should be excluded from automatic processing)
353 await createDefaultPromotion(service, {
354 code: "NON_AUTO_PROMO",
355 is_automatic: false, // Not automatic
356 rules: [
357 {
358 attribute: "customer.customer_group.id",
359 operator: "in",
360 values: ["VIP1"], // Would match but not automatic
361 },
362 ],
363 application_method: {
364 type: "fixed",
365 target_type: "items",
366 allocation: "each",
367 max_quantity: 100000,
368 value: 100,
369 target_rules: [
370 {
371 attribute: "items.product.id",
372 operator: "eq",
373 values: ["prod_tshirt0"],
374 },
375 ],
376 },
377 })
378
379 // 6. Non-automatic promotion that do not match any rules (should be excluded from automatic processing and internal pre filtering)
380 await createDefaultPromotion(service, {
381 code: "NON_AUTO_PROMO_2",
382 is_automatic: false, // Not automatic
383 rules: [
384 {
385 attribute: "customer.customer_group.id",
386 operator: "in",
387 values: ["VIP99"], // Would not match our customer group
388 },
389 ],
390 application_method: {
391 type: "fixed",
392 target_type: "items",
393 allocation: "each",
394 max_quantity: 100000,
395 value: 100,
396 target_rules: [
397 {
398 attribute: "items.product.id",
399 operator: "eq",
400 values: ["prod_tshirt0"],
401 },
402 ],
403 },
404 })
405
406 // Spy on the internal promotion service to verify prefiltering
407 let prefilterCallCount = 0
408 let prefilteredPromotions: any[] = []
409 const originalPromotionServiceList = (service as any).promotionService_
410 .list
411
412 ;(service as any).promotionService_.list = async (...args: any[]) => {
413 const result = await originalPromotionServiceList.bind(
414 (service as any).promotionService_
415 )(...args)
416
417 if (prefilterCallCount === 0) {
418 prefilteredPromotions = result
419 }
420 prefilterCallCount++
421
422 return result
423 }
424
425 // Test Context: Customer with specific attributes
426 const testContext = {
427 currency_code: "usd",
428 customer: {
429 id: "customer", // Matches CUSTOMER_ID_PROMO
430 customer_group: {
431 id: "VIP1", // Matches CUSTOMER_GROUP_PROMO
432 },
433 },
434 region_id: "region_1",
435 items: [
436 {
437 id: "item_tshirt0",
438 quantity: 1,
439 subtotal: 100, // > 50, matches SUBTOTAL_PROMO
440 product: { id: "prod_tshirt0" }, // Matches NO_RULES_PROMO target
441 },
442 {
443 id: "item_tshirt1",
444 quantity: 1,
445 subtotal: 100, // > 50, matches SUBTOTAL_PROMO
446 product: { id: "prod_tshirt1" }, // Matches CUSTOMER_GROUP_PROMO target
447 },
448 {
449 id: "item_tshirt9",
450 quantity: 5,
451 subtotal: 750, // > 50, matches SUBTOTAL_PROMO
452 product: { id: "prod_tshirt9" }, // Matches CUSTOMER_ID_PROMO target
453 },
454 {
455 id: "item_unknown",
456 quantity: 1,
457 subtotal: 110, // > 50, matches SUBTOTAL_PROMO
458 product: { id: "prod_unknown" }, // No specific target rules match
459 },
460 ] as any,
461 }
462
463 const actions = await service.computeActions([], testContext)
464
465 ;(service as any).promotionService_.list = originalPromotionServiceList
466
467 // 1. Verify prefiltering worked - should include matching promotions
468 expect(prefilteredPromotions).toHaveLength(4)
469 const prefilteredCodes = prefilteredPromotions.map((p) => p.code)
470 expect(prefilteredCodes).toEqual(
471 expect.arrayContaining([
472 "NO_RULES_PROMO", // No rules - always included
473 "CUSTOMER_GROUP_PROMO", // customer.customer_group.id = VIP1
474 "SUBTOTAL_PROMO", // items.subtotal > 50
475 "CUSTOMER_ID_PROMO", // customer.id = customer
476 ])
477 )
478
479 expect(actions).toHaveLength(4)
480
481 const actionsByCode = JSON.parse(JSON.stringify(actions)).reduce(
482 (acc, action) => {
483 if (!acc[action.code]) acc[action.code] = []
484 acc[action.code].push(action)
485 return acc
486 },
487 {} as Record<string, any[]>
488 )
489
490 // NO_RULES_PROMO: Applies to item_tshirt0 (product.id = prod_tshirt0)
491 expect(actionsByCode["NO_RULES_PROMO"]).toEqual([
492 expect.objectContaining({
493 action: "addItemAdjustment",
494 item_id: "item_tshirt0",
495 amount: 100,
496 code: "NO_RULES_PROMO",
497 }),
498 ])
499
500 // CUSTOMER_GROUP_PROMO: Applies to item_tshirt1 (product.id = prod_tshirt1)
501 expect(actionsByCode["CUSTOMER_GROUP_PROMO"]).toEqual([
502 expect.objectContaining({
503 action: "addItemAdjustment",
504 item_id: "item_tshirt1",
505 amount: 100,
506 code: "CUSTOMER_GROUP_PROMO",
507 }),
508 ])
509
510 // SUBTOTAL_PROMO: (no target rules)
511 expect(actionsByCode["SUBTOTAL_PROMO"]).toHaveLength(1)
512 expect(actionsByCode["SUBTOTAL_PROMO"]).toEqual(
513 expect.arrayContaining([
514 expect.objectContaining({ item_id: "item_unknown", amount: 100 }),
515 ])
516 )
517
518 // CUSTOMER_ID_PROMO: Applies to item_tshirt9 (product.id = prod_tshirt9)
519 expect(actionsByCode["CUSTOMER_ID_PROMO"]).toEqual([
520 expect.objectContaining({
521 action: "addItemAdjustment",
522 item_id: "item_tshirt9",
523 amount: 750,
524 code: "CUSTOMER_ID_PROMO",
525 }),
526 ])
527 })
528
529 it("should exclude promotions with rules for attributes not in context", async () => {
530 // Create promotion with mixed attributes - some in context, some not
531 await createDefaultPromotion(service, {
532 code: "MIXED_ATTRIBUTES_PROMO",
533 is_automatic: true,
534 rules: [
535 {
536 attribute: "customer.id",
537 operator: "eq",
538 values: ["customer1"], // This matches context
539 },
540 {
541 attribute: "special_flag", // This attribute NOT in context
542 operator: "eq",
543 values: ["premium"],
544 },
545 ],
546 application_method: {
547 type: "fixed",
548 target_type: "items",
549 allocation: "each",
550 max_quantity: 100000,
551 value: 100,
552 },
553 })
554
555 // Create promotion with only context attributes - should be included
556 await createDefaultPromotion(service, {
557 code: "CONTEXT_ONLY_PROMO",
558 is_automatic: true,
559 rules: [
560 {
561 attribute: "customer.id",
562 operator: "eq",
563 values: ["customer1"],
564 },
565 ],
566 application_method: {
567 type: "fixed",
568 target_type: "items",
569 allocation: "each",
570 max_quantity: 100000,
571 value: 50,
572 },
573 })
574
575 // Spy on promotion service to capture prefiltered results
576 let prefilteredPromotions: any[] = []
577 const originalPromotionServiceList = (service as any).promotionService_
578 .list
579
580 ;(service as any).promotionService_.list = async (...args: any[]) => {
581 const result = await originalPromotionServiceList.bind(
582 (service as any).promotionService_
583 )(...args)
584 prefilteredPromotions = result
585 return result
586 }
587
588 // Context with limited attributes
589 const testContext = {
590 customer: { id: "customer1" },
591 items: [
592 {
593 id: "item1",
594 quantity: 1,
595 subtotal: 100,
596 product: { id: "prod1" },
597 },
598 ],
599 }
600
601 await service.computeActions([], testContext as any)
602
603 // Restore original method
604 ;(service as any).promotionService_.list = originalPromotionServiceList
605
606 // Should only include promotion with context-only attributes
607 expect(prefilteredPromotions).toHaveLength(1)
608 expect(prefilteredPromotions[0].code).toBe("CONTEXT_ONLY_PROMO")
609 })
610
611 it("should handle prefiltering of many automatic promotions targetting customers and regions with only one that is relevant", async () => {
612 const promotionToCreate: CreatePromotionDTO[] = []
613 // I ve also tested with 20k and the compute actions takes 200/300ms
614 for (let i = 0; i < 100; i++) {
615 promotionToCreate.push({
616 code: "CUSTOMER_PROMO_" + i,
617 is_automatic: true,
618 rules: [
619 {
620 attribute: "customer.id",
621 operator: "eq",
622 values: ["customer" + i], // Matches our test customer1
623 },
624 {
625 attribute: "region_id",
626 operator: "eq",
627 values: ["region_1"], // matches our region
628 },
629 ],
630 application_method: {
631 type: "fixed",
632 target_type: "items",
633 allocation: "each",
634 max_quantity: 100000,
635 value: 100,
636 target_rules: [
637 {
638 attribute: "items.product.id",
639 operator: "eq",
640 values: ["prod_tshirt0"], // Only applies to product 0
641 },
642 ],
643 },
644 type: "standard",
645 status: PromotionStatus.ACTIVE,
646 campaign_id: "campaign-id-1",
647 })
648 }
649
650 await service.createPromotions(promotionToCreate)
651
652 // Spy on the internal promotion service to verify prefiltering
653 let prefilterCallCount = 0
654 let prefilteredPromotions: any[] = []
655 const originalPromotionServiceList = (service as any).promotionService_
656 .list
657
658 ;(service as any).promotionService_.list = async (...args: any[]) => {
659 const result = await originalPromotionServiceList.bind(
660 (service as any).promotionService_
661 )(...args)
662
663 if (prefilterCallCount === 0) {
664 prefilteredPromotions = result
665 }
666 prefilterCallCount++
667
668 return result
669 }
670
671 // Test Context: Customer with specific attributes
672 const testContext = {
673 currency_code: "usd",
674 customer: {
675 id: "customer1", // Matches CUSTOMER_PROMO_1
676 },
677 region_id: "region_1",
678 items: [
679 {
680 id: "item_tshirt0",
681 quantity: 1,
682 subtotal: 100,
683 product: { id: "prod_tshirt0" }, // Matches CUSTOMER_PROMO_1 target
684 },
685 {
686 id: "item_tshirt1",
687 quantity: 1,
688 subtotal: 100,
689 product: { id: "prod_tshirt1" },
690 },
691 {
692 id: "item_tshirt9",
693 quantity: 5,
694 subtotal: 750,
695 product: { id: "prod_tshirt9" },
696 },
697 {
698 id: "item_unknown",
699 quantity: 1,
700 subtotal: 110,
701 product: { id: "prod_unknown" },
702 },
703 ] as any,
704 }
705
706 const actions = await service.computeActions([], testContext)
707
708 ;(service as any).promotionService_.list = originalPromotionServiceList
709
710 // 1. Verify prefiltering worked - should include matching promotion
711 // We expect the prefilter to have return a single promotion that is being satisfied by the
712 // context with the given customer id and region id
713 expect(prefilteredPromotions).toHaveLength(1)
714 const prefilteredCodes = prefilteredPromotions.map((p) => p.code)
715 expect(prefilteredCodes).toEqual(
716 expect.arrayContaining([
717 "CUSTOMER_PROMO_1", // customer.id = customer1
718 ])
719 )
720
721 expect(actions).toHaveLength(1)
722
723 const actionsByCode = JSON.parse(JSON.stringify(actions)).reduce(
724 (acc, action) => {
725 if (!acc[action.code]) acc[action.code] = []
726 acc[action.code].push(action)
727 return acc
728 },
729 {} as Record<string, any[]>
730 )
731
732 // CUSTOMER_PROMO_1: Applies to item_tshirt0 (product.id = prod_tshirt0)
733 expect(actionsByCode["CUSTOMER_PROMO_1"]).toEqual([
734 expect.objectContaining({
735 action: "addItemAdjustment",
736 item_id: "item_tshirt0",
737 amount: 100,
738 code: "CUSTOMER_PROMO_1",
739 }),
740 ])
741 })
742
743 it("should handle prefiltering of many automatic promotions targetting customers and regions while no context attribute can satisfies any of those rules", async () => {
744 const promotionToCreate: CreatePromotionDTO[] = []
745 for (let i = 0; i < 100; i++) {
746 promotionToCreate.push({
747 code: "CUSTOMER_PROMO_" + i,
748 is_automatic: true,
749 rules: [
750 {
751 attribute: "customer.id",
752 operator: "eq",
753 values: ["customer" + i], // Matches our test customer1
754 },
755 {
756 attribute: "region_id",
757 operator: "eq",
758 values: ["region_1"], // matches our region
759 },
760 ],
761 application_method: {
762 type: "fixed",
763 target_type: "items",
764 allocation: "each",
765 max_quantity: 100000,
766 value: 100,
767 target_rules: [
768 {
769 attribute: "items.product.id",
770 operator: "eq",
771 values: ["prod_tshirt0"], // Only applies to product 0
772 },
773 ],
774 },
775 type: "standard",
776 status: PromotionStatus.ACTIVE,
777 campaign_id: "campaign-id-1",
778 })
779 }
780
781 await service.createPromotions(promotionToCreate)
782
783 // Spy on the internal promotion service to verify prefiltering
784 let prefilterCallCount = 0
785 let prefilteredPromotions: any[] = []
786 const originalPromotionServiceList = (service as any).promotionService_
787 .list
788
789 ;(service as any).promotionService_.list = async (...args: any[]) => {
790 const result = await originalPromotionServiceList.bind(
791 (service as any).promotionService_
792 )(...args)
793
794 if (prefilterCallCount === 0) {
795 prefilteredPromotions = result
796 }
797 prefilterCallCount++
798
799 return result
800 }
801
802 // Test Context
803 const testContext = {
804 currency_code: "usd",
805 items: [
806 {
807 id: "item_tshirt0",
808 quantity: 1,
809 subtotal: 100,
810 product: { id: "prod_tshirt0" }, // Matches CUSTOMER_PROMO_1 target
811 },
812 {
813 id: "item_tshirt1",
814 quantity: 1,
815 subtotal: 100,
816 product: { id: "prod_tshirt1" },
817 },
818 {
819 id: "item_tshirt9",
820 quantity: 5,
821 subtotal: 750,
822 product: { id: "prod_tshirt9" },
823 },
824 {
825 id: "item_unknown",
826 quantity: 1,
827 subtotal: 110,
828 product: { id: "prod_unknown" },
829 },
830 ] as any,
831 }
832
833 const actions = await service.computeActions([], testContext)
834
835 ;(service as any).promotionService_.list = originalPromotionServiceList
836
837 // 1. Verify prefiltering worked - should include matching promotion
838 // We expect the prefilter to have return 0 promotion that is being satisfied by the
839 expect(prefilteredPromotions).toHaveLength(0)
840
841 expect(actions).toHaveLength(0)
842 })
843
844 it("should return empty array when promotion is not active (draft or inactive)", async () => {
845 const promotion = await createDefaultPromotion(service, {
846 status: PromotionStatus.DRAFT,
847 rules: [
848 {
849 attribute: "customer.customer_group.id",
850 operator: "in",
851 values: ["VIP", "top100"],
852 },
853 ],
854 application_method: {
855 type: "fixed",
856 target_type: "items",
857 allocation: "each",
858 value: 200,
859 max_quantity: 1,
860 target_rules: [
861 {
862 attribute: "items.product_category.id",
863 operator: "eq",
864 values: ["catg_cotton"],
865 },
866 ],
867 },
868 })
869
870 const result = await service.computeActions([promotion.code!], {
871 currency_code: "usd",
872 customer: {
873 customer_group: {
874 id: "VIP",
875 },
876 },
877 items: [
878 {
879 id: "item_cotton_tshirt",
880 quantity: 1,
881 subtotal: 100,
882 original_total: 100,
883 is_discountable: true,
884 product_category: {
885 id: "catg_cotton",
886 },
887 product: {
888 id: "prod_tshirt",
889 },
890 },
891 {
892 id: "item_cotton_sweater",
893 quantity: 5,
894 subtotal: 750,
895 original_total: 750,
896 is_discountable: true,
897 product_category: {
898 id: "catg_cotton",
899 },
900 product: {
901 id: "prod_sweater",
902 },
903 },
904 ],
905 })
906
907 expect(result).toEqual([])
908 })
909
910 describe("when code is not present in database", () => {
911 it("should return empty array when promotion does not exist", async () => {
912 const response = await service.computeActions(["DOES_NOT_EXIST"], {
913 currency_code: "usd",
914 customer: {
915 customer_group: {
916 id: "VIP",
917 },
918 },
919 items: [
920 {
921 id: "item_cotton_tshirt",
922 quantity: 1,
923 subtotal: 100,
924 original_total: 100,
925 is_discountable: true,
926 product_category: {
927 id: "catg_cotton",
928 },
929 product: {
930 id: "prod_tshirt",
931 },
932 },
933 {
934 id: "item_cotton_sweater",
935 quantity: 5,
936 subtotal: 750,
937 original_total: 750,
938 is_discountable: true,
939 product_category: {
940 id: "catg_cotton",
941 },
942 product: {
943 id: "prod_sweater",
944 },
945 },
946 ],
947 })
948
949 expect(response).toEqual([])
950 })
951 })
952
953 describe("when promotion is for items and allocation is each", () => {
954 describe("when application type is fixed", () => {
955 it("should compute the correct item amendments", async () => {
956 await createDefaultPromotion(service, {
957 rules: [
958 {
959 attribute: "customer.customer_group.id",
960 operator: "in",
961 values: ["VIP", "top100"],
962 },
963 ],
964 application_method: {
965 type: "fixed",
966 target_type: "items",
967 allocation: "each",
968 value: 200,
969 max_quantity: 1,
970 target_rules: [
971 {
972 attribute: "items.product_category.id",
973 operator: "eq",
974 values: ["catg_cotton"],
975 },
976 ],
977 } as any,
978 })
979
980 const result = await service.computeActions(["PROMOTION_TEST"], {
981 currency_code: "usd",
982 customer: {
983 customer_group: {
984 id: "VIP",
985 },
986 },
987 items: [
988 {
989 id: "item_cotton_tshirt",
990 quantity: 1,
991 subtotal: 100,
992 original_total: 100,
993 is_discountable: true,
994 product_category: {
995 id: "catg_cotton",
996 },
997 product: {
998 id: "prod_tshirt",
999 },
1000 },
1001 {
1002 id: "item_cotton_sweater",
1003 quantity: 5,
1004 subtotal: 750,
1005 original_total: 750,
1006 is_discountable: true,
1007 product_category: {
1008 id: "catg_cotton",
1009 },
1010 product: {
1011 id: "prod_sweater",
1012 },
1013 },
1014 ],
1015 })
1016
1017 expect(JSON.parse(JSON.stringify(result))).toEqual([
1018 {
1019 action: "addItemAdjustment",
1020 item_id: "item_cotton_tshirt",
1021 amount: 100,
1022 code: "PROMOTION_TEST",
1023 is_tax_inclusive: false,
1024 },
1025 {
1026 action: "addItemAdjustment",
1027 item_id: "item_cotton_sweater",
1028 amount: 150,
1029 code: "PROMOTION_TEST",
1030 is_tax_inclusive: false,
1031 },
1032 ])
1033
1034 const resultWithoutCustomer = await service.computeActions(
1035 ["PROMOTION_TEST"],
1036 {
1037 currency_code: "usd",
1038 items: [
1039 {
1040 id: "item_cotton_tshirt",
1041 quantity: 1,
1042 subtotal: 100,
1043 original_total: 100,
1044 is_discountable: true,
1045 product_category: {
1046 id: "catg_cotton",
1047 },
1048 product: {
1049 id: "prod_tshirt",
1050 },
1051 },
1052 {
1053 id: "item_cotton_sweater",
1054 quantity: 5,
1055 subtotal: 750,
1056 original_total: 750,
1057 is_discountable: true,
1058 product_category: {
1059 id: "catg_cotton",
1060 },
1061 product: {
1062 id: "prod_sweater",
1063 },
1064 },
1065 ],
1066 }
1067 )
1068
1069 expect(JSON.parse(JSON.stringify(resultWithoutCustomer))).toEqual(
1070 []
1071 )
1072 })
1073
1074 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
1075 await createDefaultPromotion(service, {
1076 rules: [
1077 {
1078 attribute: "customer.customer_group.id",
1079 operator: "in",
1080 values: ["VIP", "top100"],
1081 },
1082 ],
1083 application_method: {
1084 type: "fixed",
1085 target_type: "items",
1086 allocation: "each",
1087 value: 30,
1088 max_quantity: 2,
1089 target_rules: [
1090 {
1091 attribute: "items.product_category.id",
1092 operator: "eq",
1093 values: ["catg_cotton"],
1094 },
1095 ],
1096 } as any,
1097 })
1098
1099 await createDefaultPromotion(service, {
1100 code: "PROMOTION_TEST_2",
1101 rules: [
1102 {
1103 attribute: "customer.customer_group.id",
1104 operator: "in",
1105 values: ["VIP", "top100"],
1106 },
1107 ],
1108 application_method: {
1109 type: "fixed",
1110 target_type: "items",
1111 allocation: "each",
1112 value: 50,
1113 max_quantity: 1,
1114 target_rules: [
1115 {
1116 attribute: "items.product_category.id",
1117 operator: "eq",
1118 values: ["catg_cotton"],
1119 },
1120 ],
1121 } as any,
1122 })
1123
1124 const result = await service.computeActions(
1125 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
1126 {
1127 currency_code: "usd",
1128 customer: {
1129 customer_group: {
1130 id: "VIP",
1131 },
1132 },
1133 items: [
1134 {
1135 id: "item_cotton_tshirt",
1136 quantity: 1,
1137 subtotal: 50,
1138 original_total: 50,
1139 is_discountable: true,
1140 product_category: {
1141 id: "catg_cotton",
1142 },
1143 product: {
1144 id: "prod_tshirt",
1145 },
1146 },
1147 {
1148 id: "item_cotton_sweater",
1149 quantity: 1,
1150 subtotal: 150,
1151 original_total: 150,
1152 is_discountable: true,
1153 product_category: {
1154 id: "catg_cotton",
1155 },
1156 product: {
1157 id: "prod_sweater",
1158 },
1159 },
1160 ],
1161 }
1162 )
1163
1164 expect(JSON.parse(JSON.stringify(result))).toEqual([
1165 {
1166 action: "addItemAdjustment",
1167 item_id: "item_cotton_tshirt",
1168 amount: 50,
1169 code: "PROMOTION_TEST_2",
1170 is_tax_inclusive: false,
1171 },
1172 {
1173 action: "addItemAdjustment",
1174 item_id: "item_cotton_sweater",
1175 amount: 50,
1176 code: "PROMOTION_TEST_2",
1177 is_tax_inclusive: false,
1178 },
1179 {
1180 action: "addItemAdjustment",
1181 item_id: "item_cotton_sweater",
1182 amount: 30,
1183 code: "PROMOTION_TEST",
1184 is_tax_inclusive: false,
1185 },
1186 ])
1187 })
1188
1189 it("should not compute actions when applicable total is 0", async () => {
1190 await createDefaultPromotion(service, {
1191 rules: [
1192 {
1193 attribute: "customer.customer_group.id",
1194 operator: "in",
1195 values: ["VIP", "top100"],
1196 },
1197 ],
1198 application_method: {
1199 type: "fixed",
1200 target_type: "items",
1201 allocation: "each",
1202 value: 500,
1203 max_quantity: 2,
1204 target_rules: [
1205 {
1206 attribute: "items.product_category.id",
1207 operator: "eq",
1208 values: ["catg_cotton"],
1209 },
1210 ],
1211 } as any,
1212 })
1213
1214 await createDefaultPromotion(service, {
1215 code: "PROMOTION_TEST_2",
1216 rules: [
1217 {
1218 attribute: "customer.customer_group.id",
1219 operator: "in",
1220 values: ["VIP", "top100"],
1221 },
1222 ],
1223 application_method: {
1224 type: "fixed",
1225 target_type: "items",
1226 allocation: "each",
1227 value: 50,
1228 max_quantity: 1,
1229 target_rules: [
1230 {
1231 attribute: "items.product_category.id",
1232 operator: "eq",
1233 values: ["catg_cotton"],
1234 },
1235 ],
1236 } as any,
1237 })
1238
1239 const result = await service.computeActions(
1240 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
1241 {
1242 currency_code: "usd",
1243 customer: {
1244 customer_group: {
1245 id: "VIP",
1246 },
1247 },
1248 items: [
1249 {
1250 id: "item_cotton_tshirt",
1251 quantity: 1,
1252 subtotal: 50,
1253 original_total: 50,
1254 is_discountable: true,
1255 product_category: {
1256 id: "catg_cotton",
1257 },
1258 product: {
1259 id: "prod_tshirt",
1260 },
1261 },
1262 {
1263 id: "item_cotton_sweater",
1264 quantity: 1,
1265 subtotal: 150,
1266 original_total: 150,
1267 is_discountable: true,
1268 product_category: {
1269 id: "catg_cotton",
1270 },
1271 product: {
1272 id: "prod_sweater",
1273 },
1274 },
1275 ],
1276 }
1277 )
1278
1279 expect(JSON.parse(JSON.stringify(result))).toEqual([
1280 {
1281 action: "addItemAdjustment",
1282 item_id: "item_cotton_tshirt",
1283 amount: 50,
1284 code: "PROMOTION_TEST",
1285 is_tax_inclusive: false,
1286 },
1287 {
1288 action: "addItemAdjustment",
1289 item_id: "item_cotton_sweater",
1290 amount: 150,
1291 code: "PROMOTION_TEST",
1292 is_tax_inclusive: false,
1293 },
1294 ])
1295 })
1296
1297 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
1298 await createDefaultPromotion(service, {
1299 rules: [
1300 {
1301 attribute: "customer.customer_group.id",
1302 operator: "in",
1303 values: ["VIP", "top100"],
1304 },
1305 ],
1306 application_method: {
1307 type: "fixed",
1308 target_type: "items",
1309 allocation: "each",
1310 value: 100,
1311 max_quantity: 5,
1312 target_rules: [
1313 {
1314 attribute: "items.product_category.id",
1315 operator: "eq",
1316 values: ["catg_cotton"],
1317 },
1318 ],
1319 } as any,
1320 })
1321
1322 const result = await service.computeActions(["PROMOTION_TEST"], {
1323 currency_code: "usd",
1324 customer: {
1325 customer_group: {
1326 id: "VIP",
1327 },
1328 },
1329 items: [
1330 {
1331 id: "item_cotton_tshirt",
1332 quantity: 5,
1333 subtotal: 5000,
1334 original_total: 5000,
1335 is_discountable: true,
1336 product_category: {
1337 id: "catg_cotton",
1338 },
1339 product: {
1340 id: "prod_tshirt",
1341 },
1342 },
1343 ],
1344 })
1345
1346 expect(JSON.parse(JSON.stringify(result))).toEqual([
1347 {
1348 action: "addItemAdjustment",
1349 item_id: "item_cotton_tshirt",
1350 amount: 500,
1351 code: "PROMOTION_TEST",
1352 is_tax_inclusive: false,
1353 },
1354 ])
1355 })
1356
1357 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
1358 await createDefaultPromotion(service, {
1359 rules: [
1360 {
1361 attribute: "customer.customer_group.id",
1362 operator: "in",
1363 values: ["VIP", "top100"],
1364 },
1365 ],
1366 campaign_id: "campaign-id-2",
1367 application_method: {
1368 type: "fixed",
1369 target_type: "items",
1370 allocation: "each",
1371 value: 500,
1372 max_quantity: 5,
1373 target_rules: [
1374 {
1375 attribute: "items.product_category.id",
1376 operator: "eq",
1377 values: ["catg_cotton"],
1378 },
1379 ],
1380 } as any,
1381 })
1382 await service.updateCampaigns({
1383 id: "campaign-id-2",
1384 budget: { used: 1000 },
1385 })
1386
1387 const result = await service.computeActions(["PROMOTION_TEST"], {
1388 currency_code: "usd",
1389 customer: {
1390 customer_group: {
1391 id: "VIP",
1392 },
1393 },
1394 items: [
1395 {
1396 id: "item_cotton_tshirt",
1397 quantity: 5,
1398 subtotal: 5000,
1399 original_total: 5000,
1400 is_discountable: true,
1401 product_category: {
1402 id: "catg_cotton",
1403 },
1404 product: {
1405 id: "prod_tshirt",
1406 },
1407 },
1408 ],
1409 })
1410
1411 expect(JSON.parse(JSON.stringify(result))).toEqual([
1412 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
1413 ])
1414 })
1415 })
1416
1417 describe("when application type is percentage", () => {
1418 it("should compute the correct item amendments", async () => {
1419 await createDefaultPromotion(service, {
1420 rules: [
1421 {
1422 attribute: "customer.customer_group.id",
1423 operator: "in",
1424 values: ["VIP", "top100"],
1425 },
1426 ],
1427 application_method: {
1428 type: ApplicationMethodType.PERCENTAGE,
1429 target_type: "items",
1430 allocation: "each",
1431 value: 10,
1432 max_quantity: 1,
1433 target_rules: [
1434 {
1435 attribute: "items.product_category.id",
1436 operator: "eq",
1437 values: ["catg_cotton"],
1438 },
1439 ],
1440 } as any,
1441 })
1442
1443 const result = await service.computeActions(["PROMOTION_TEST"], {
1444 currency_code: "usd",
1445 customer: {
1446 customer_group: {
1447 id: "VIP",
1448 },
1449 },
1450 items: [
1451 {
1452 id: "item_cotton_tshirt",
1453 quantity: 1,
1454 subtotal: 100,
1455 original_total: 100,
1456 is_discountable: true,
1457 product_category: {
1458 id: "catg_cotton",
1459 },
1460 product: {
1461 id: "prod_tshirt",
1462 },
1463 },
1464 {
1465 id: "item_cotton_sweater",
1466 quantity: 5,
1467 subtotal: 750,
1468 original_total: 750,
1469 is_discountable: true,
1470 product_category: {
1471 id: "catg_cotton",
1472 },
1473 product: {
1474 id: "prod_sweater",
1475 },
1476 },
1477 ],
1478 })
1479
1480 expect(JSON.parse(JSON.stringify(result))).toEqual([
1481 {
1482 action: "addItemAdjustment",
1483 item_id: "item_cotton_tshirt",
1484 amount: 10,
1485 code: "PROMOTION_TEST",
1486 is_tax_inclusive: false,
1487 },
1488 {
1489 action: "addItemAdjustment",
1490 item_id: "item_cotton_sweater",
1491 amount: 15,
1492 code: "PROMOTION_TEST",
1493 is_tax_inclusive: false,
1494 },
1495 ])
1496 })
1497
1498 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
1499 await createDefaultPromotion(service, {
1500 rules: [
1501 {
1502 attribute: "customer.customer_group.id",
1503 operator: "in",
1504 values: ["VIP", "top100"],
1505 },
1506 ],
1507 application_method: {
1508 type: ApplicationMethodType.PERCENTAGE,
1509 target_type: "items",
1510 allocation: "each",
1511 value: 30,
1512 max_quantity: 2,
1513 target_rules: [
1514 {
1515 attribute: "items.product_category.id",
1516 operator: "eq",
1517 values: ["catg_cotton"],
1518 },
1519 ],
1520 } as any,
1521 })
1522
1523 await createDefaultPromotion(service, {
1524 code: "PROMOTION_TEST_2",
1525 rules: [
1526 {
1527 attribute: "customer.customer_group.id",
1528 operator: "in",
1529 values: ["VIP", "top100"],
1530 },
1531 ],
1532 application_method: {
1533 type: ApplicationMethodType.PERCENTAGE,
1534 target_type: "items",
1535 allocation: "each",
1536 value: 10,
1537 max_quantity: 1,
1538 target_rules: [
1539 {
1540 attribute: "items.product_category.id",
1541 operator: "eq",
1542 values: ["catg_cotton"],
1543 },
1544 ],
1545 } as any,
1546 })
1547
1548 const result = await service.computeActions(
1549 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
1550 {
1551 currency_code: "usd",
1552 customer: {
1553 customer_group: {
1554 id: "VIP",
1555 },
1556 },
1557 items: [
1558 {
1559 id: "item_cotton_tshirt",
1560 quantity: 3,
1561 subtotal: 150,
1562 original_total: 150,
1563 is_discountable: true,
1564 product_category: {
1565 id: "catg_cotton",
1566 },
1567 product: {
1568 id: "prod_tshirt",
1569 },
1570 },
1571 {
1572 id: "item_cotton_sweater",
1573 quantity: 1,
1574 subtotal: 150,
1575 original_total: 150,
1576 is_discountable: true,
1577 product_category: {
1578 id: "catg_cotton",
1579 },
1580 product: {
1581 id: "prod_sweater",
1582 },
1583 },
1584 ],
1585 }
1586 )
1587
1588 expect(JSON.parse(JSON.stringify(result))).toEqual([
1589 {
1590 action: "addItemAdjustment",
1591 item_id: "item_cotton_tshirt",
1592 amount: 30,
1593 code: "PROMOTION_TEST",
1594 is_tax_inclusive: false,
1595 },
1596 {
1597 action: "addItemAdjustment",
1598 item_id: "item_cotton_sweater",
1599 amount: 45,
1600 code: "PROMOTION_TEST",
1601 is_tax_inclusive: false,
1602 },
1603 {
1604 action: "addItemAdjustment",
1605 item_id: "item_cotton_tshirt",
1606 amount: 5,
1607 code: "PROMOTION_TEST_2",
1608 is_tax_inclusive: false,
1609 },
1610 {
1611 action: "addItemAdjustment",
1612 item_id: "item_cotton_sweater",
1613 amount: 10.5,
1614 code: "PROMOTION_TEST_2",
1615 is_tax_inclusive: false,
1616 },
1617 ])
1618 })
1619
1620 it("should compute actions when 2 percentage promotions are applied to the same item", async () => {
1621 await createDefaultPromotion(service, {
1622 code: "PROMO_PERCENTAGE_1",
1623 rules: [],
1624 application_method: {
1625 type: ApplicationMethodType.PERCENTAGE,
1626 target_type: "items",
1627 allocation: "each",
1628 value: 100,
1629 max_quantity: 1,
1630 target_rules: [
1631 {
1632 attribute: "items.product.id",
1633 operator: "eq",
1634 values: ["prod_tshirt"],
1635 },
1636 ],
1637 },
1638 })
1639
1640 await createDefaultPromotion(service, {
1641 code: "PROMO_PERCENTAGE_2",
1642 rules: [],
1643 application_method: {
1644 type: ApplicationMethodType.PERCENTAGE,
1645 target_type: "items",
1646 allocation: "each",
1647 value: 100,
1648 max_quantity: 1,
1649 target_rules: [
1650 {
1651 attribute: "items.product.id",
1652 operator: "eq",
1653 values: ["prod_tshirt"],
1654 },
1655 ],
1656 },
1657 })
1658
1659 const result = await service.computeActions(
1660 ["PROMO_PERCENTAGE_1", "PROMO_PERCENTAGE_2"],
1661 {
1662 currency_code: "usd",
1663 items: [
1664 {
1665 id: "item_cotton_tshirt",
1666 quantity: 4,
1667 subtotal: 200,
1668 original_total: 200,
1669 is_discountable: true,
1670 product_category: {
1671 id: "catg_cotton",
1672 },
1673 product: {
1674 id: "prod_tshirt",
1675 },
1676 },
1677 ],
1678 }
1679 )
1680
1681 expect(JSON.parse(JSON.stringify(result))).toEqual([
1682 {
1683 action: "addItemAdjustment",
1684 item_id: "item_cotton_tshirt",
1685 amount: 50,
1686 code: "PROMO_PERCENTAGE_1",
1687 is_tax_inclusive: false,
1688 },
1689 {
1690 action: "addItemAdjustment",
1691 item_id: "item_cotton_tshirt",
1692 amount: 50,
1693 code: "PROMO_PERCENTAGE_2",
1694 is_tax_inclusive: false,
1695 },
1696 ])
1697 })
1698
1699 it("should not compute actions when applicable total is 0", async () => {
1700 await createDefaultPromotion(service, {
1701 rules: [
1702 {
1703 attribute: "customer.customer_group.id",
1704 operator: "in",
1705 values: ["VIP", "top100"],
1706 },
1707 ],
1708 application_method: {
1709 type: ApplicationMethodType.PERCENTAGE,
1710 target_type: "items",
1711 allocation: "each",
1712 value: 100,
1713 max_quantity: 10,
1714 target_rules: [
1715 {
1716 attribute: "items.product_category.id",
1717 operator: "eq",
1718 values: ["catg_cotton"],
1719 },
1720 ],
1721 } as any,
1722 })
1723
1724 await createDefaultPromotion(service, {
1725 code: "PROMOTION_TEST_2",
1726 rules: [
1727 {
1728 attribute: "customer.customer_group.id",
1729 operator: "in",
1730 values: ["VIP", "top100"],
1731 },
1732 ],
1733 application_method: {
1734 type: ApplicationMethodType.PERCENTAGE,
1735 target_type: "items",
1736 allocation: "each",
1737 value: 50,
1738 max_quantity: 10,
1739 target_rules: [
1740 {
1741 attribute: "items.product_category.id",
1742 operator: "eq",
1743 values: ["catg_cotton"],
1744 },
1745 ],
1746 } as any,
1747 })
1748
1749 const result = await service.computeActions(
1750 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
1751 {
1752 currency_code: "usd",
1753 customer: {
1754 customer_group: {
1755 id: "VIP",
1756 },
1757 },
1758 items: [
1759 {
1760 id: "item_cotton_tshirt",
1761 quantity: 1,
1762 subtotal: 50,
1763 original_total: 50,
1764 is_discountable: true,
1765 product_category: {
1766 id: "catg_cotton",
1767 },
1768 product: {
1769 id: "prod_tshirt",
1770 },
1771 },
1772 {
1773 id: "item_cotton_sweater",
1774 quantity: 1,
1775 subtotal: 150,
1776 original_total: 150,
1777 is_discountable: true,
1778 product_category: {
1779 id: "catg_cotton",
1780 },
1781 product: {
1782 id: "prod_sweater",
1783 },
1784 },
1785 ],
1786 }
1787 )
1788
1789 expect(JSON.parse(JSON.stringify(result))).toEqual([
1790 {
1791 action: "addItemAdjustment",
1792 item_id: "item_cotton_tshirt",
1793 amount: 50,
1794 code: "PROMOTION_TEST",
1795 is_tax_inclusive: false,
1796 },
1797 {
1798 action: "addItemAdjustment",
1799 item_id: "item_cotton_sweater",
1800 amount: 150,
1801 code: "PROMOTION_TEST",
1802 is_tax_inclusive: false,
1803 },
1804 ])
1805 })
1806
1807 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
1808 await createDefaultPromotion(service, {
1809 rules: [
1810 {
1811 attribute: "customer.customer_group.id",
1812 operator: "in",
1813 values: ["VIP", "top100"],
1814 },
1815 ],
1816 application_method: {
1817 type: ApplicationMethodType.PERCENTAGE,
1818 target_type: "items",
1819 allocation: "each",
1820 value: 100,
1821 max_quantity: 5,
1822 target_rules: [
1823 {
1824 attribute: "items.product_category.id",
1825 operator: "eq",
1826 values: ["catg_cotton"],
1827 },
1828 ],
1829 } as any,
1830 })
1831
1832 const result = await service.computeActions(["PROMOTION_TEST"], {
1833 currency_code: "usd",
1834 customer: {
1835 customer_group: {
1836 id: "VIP",
1837 },
1838 },
1839 items: [
1840 {
1841 id: "item_cotton_tshirt",
1842 quantity: 5,
1843 subtotal: 10000,
1844 original_total: 10000,
1845 is_discountable: true,
1846 product_category: {
1847 id: "catg_cotton",
1848 },
1849 product: {
1850 id: "prod_tshirt",
1851 },
1852 },
1853 ],
1854 })
1855
1856 expect(JSON.parse(JSON.stringify(result))).toEqual([
1857 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
1858 ])
1859 })
1860
1861 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
1862 await createDefaultPromotion(service, {
1863 rules: [
1864 {
1865 attribute: "customer.customer_group.id",
1866 operator: "in",
1867 values: ["VIP", "top100"],
1868 },
1869 ],
1870 campaign_id: "campaign-id-2",
1871 application_method: {
1872 type: ApplicationMethodType.PERCENTAGE,
1873 target_type: "items",
1874 allocation: "each",
1875 value: 100,
1876 max_quantity: 5,
1877 target_rules: [
1878 {
1879 attribute: "items.product_category.id",
1880 operator: "eq",
1881 values: ["catg_cotton"],
1882 },
1883 ],
1884 } as any,
1885 })
1886
1887 await service.updateCampaigns({
1888 id: "campaign-id-2",
1889 budget: { used: 1000 },
1890 })
1891
1892 const result = await service.computeActions(["PROMOTION_TEST"], {
1893 currency_code: "usd",
1894 customer: {
1895 customer_group: {
1896 id: "VIP",
1897 },
1898 },
1899 items: [
1900 {
1901 id: "item_cotton_tshirt",
1902 quantity: 5,
1903 subtotal: 5000,
1904 original_total: 5000,
1905 is_discountable: true,
1906 product_category: {
1907 id: "catg_cotton",
1908 },
1909 product: {
1910 id: "prod_tshirt",
1911 },
1912 },
1913 ],
1914 })
1915
1916 expect(JSON.parse(JSON.stringify(result))).toEqual([
1917 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
1918 ])
1919 })
1920 })
1921 })
1922
1923 describe("when promotion is for items and allocation is across", () => {
1924 describe("when application type is fixed", () => {
1925 it("should compute the correct item amendments", async () => {
1926 await createDefaultPromotion(service, {
1927 rules: [
1928 {
1929 attribute: "customer.customer_group.id",
1930 operator: "in",
1931 values: ["VIP", "top100"],
1932 },
1933 ],
1934 campaign_id: "campaign-id-2",
1935 application_method: {
1936 type: "fixed",
1937 target_type: "items",
1938 allocation: "across",
1939 value: 400,
1940 target_rules: [
1941 {
1942 attribute: "items.product_category.id",
1943 operator: "eq",
1944 values: ["catg_cotton"],
1945 },
1946 ],
1947 } as any,
1948 })
1949
1950 const result = await service.computeActions(["PROMOTION_TEST"], {
1951 currency_code: "usd",
1952 customer: {
1953 customer_group: {
1954 id: "VIP",
1955 },
1956 },
1957 items: [
1958 {
1959 id: "item_cotton_tshirt",
1960 quantity: 2,
1961 subtotal: 200,
1962 original_total: 200,
1963 is_discountable: true,
1964 product_category: {
1965 id: "catg_cotton",
1966 },
1967 product: {
1968 id: "prod_tshirt",
1969 },
1970 },
1971 {
1972 id: "item_cotton_sweater",
1973 quantity: 2,
1974 subtotal: 600,
1975 original_total: 600,
1976 is_discountable: true,
1977 product_category: {
1978 id: "catg_cotton",
1979 },
1980 product: {
1981 id: "prod_sweater",
1982 },
1983 },
1984 ],
1985 })
1986
1987 expect(JSON.parse(JSON.stringify(result))).toEqual([
1988 {
1989 action: "addItemAdjustment",
1990 item_id: "item_cotton_tshirt",
1991 amount: 100,
1992 code: "PROMOTION_TEST",
1993 is_tax_inclusive: false,
1994 },
1995 {
1996 action: "addItemAdjustment",
1997 item_id: "item_cotton_sweater",
1998 amount: 300,
1999 code: "PROMOTION_TEST",
2000 is_tax_inclusive: false,
2001 },
2002 ])
2003 })
2004
2005 it("should compute the correct item amendments when promotion is automatic", async () => {
2006 await createDefaultPromotion(service, {
2007 rules: [
2008 {
2009 attribute: "customer.customer_group.id",
2010 operator: "in",
2011 values: ["VIP", "top100"],
2012 },
2013 ],
2014 is_automatic: true,
2015 application_method: {
2016 type: "fixed",
2017 target_type: "items",
2018 allocation: "across",
2019 value: 400,
2020 target_rules: [
2021 {
2022 attribute: "items.product_category.id",
2023 operator: "eq",
2024 values: ["catg_cotton"],
2025 },
2026 ],
2027 } as any,
2028 })
2029
2030 const result = await service.computeActions([], {
2031 currency_code: "usd",
2032 customer: {
2033 customer_group: {
2034 id: "VIP",
2035 },
2036 },
2037 items: [
2038 {
2039 id: "item_cotton_tshirt",
2040 quantity: 2,
2041 subtotal: 200,
2042 original_total: 200,
2043 is_discountable: true,
2044 product_category: {
2045 id: "catg_cotton",
2046 },
2047 product: {
2048 id: "prod_tshirt",
2049 },
2050 },
2051 {
2052 id: "item_cotton_sweater",
2053 quantity: 2,
2054 subtotal: 600,
2055 original_total: 600,
2056 is_discountable: true,
2057 product_category: {
2058 id: "catg_cotton",
2059 },
2060 product: {
2061 id: "prod_sweater",
2062 },
2063 },
2064 ],
2065 })
2066
2067 expect(JSON.parse(JSON.stringify(result))).toEqual([
2068 {
2069 action: "addItemAdjustment",
2070 item_id: "item_cotton_tshirt",
2071 amount: 100,
2072 code: "PROMOTION_TEST",
2073 is_tax_inclusive: false,
2074 },
2075 {
2076 action: "addItemAdjustment",
2077 item_id: "item_cotton_sweater",
2078 amount: 300,
2079 code: "PROMOTION_TEST",
2080 is_tax_inclusive: false,
2081 },
2082 ])
2083 })
2084
2085 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
2086 await createDefaultPromotion(service, {
2087 rules: [
2088 {
2089 attribute: "customer.customer_group.id",
2090 operator: "in",
2091 values: ["VIP", "top100"],
2092 },
2093 ],
2094 application_method: {
2095 type: "fixed",
2096 target_type: "items",
2097 allocation: "across",
2098 value: 30,
2099 target_rules: [
2100 {
2101 attribute: "items.product_category.id",
2102 operator: "eq",
2103 values: ["catg_cotton"],
2104 },
2105 ],
2106 } as any,
2107 })
2108
2109 await createDefaultPromotion(service, {
2110 code: "PROMOTION_TEST_2",
2111 rules: [
2112 {
2113 attribute: "customer.customer_group.id",
2114 operator: "in",
2115 values: ["VIP", "top100"],
2116 },
2117 ],
2118 is_automatic: true,
2119 application_method: {
2120 type: "fixed",
2121 target_type: "items",
2122 allocation: "across",
2123 value: 50,
2124 target_rules: [
2125 {
2126 attribute: "items.product_category.id",
2127 operator: "eq",
2128 values: ["catg_cotton"],
2129 },
2130 ],
2131 } as any,
2132 })
2133
2134 const result = await service.computeActions(
2135 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
2136 {
2137 currency_code: "usd",
2138 customer: {
2139 customer_group: {
2140 id: "VIP",
2141 },
2142 },
2143 items: [
2144 {
2145 id: "item_cotton_tshirt",
2146 quantity: 1,
2147 subtotal: 50,
2148 original_total: 50,
2149 is_discountable: true,
2150 product_category: {
2151 id: "catg_cotton",
2152 },
2153 product: {
2154 id: "prod_tshirt",
2155 },
2156 },
2157 {
2158 id: "item_cotton_sweater",
2159 quantity: 1,
2160 subtotal: 150,
2161 original_total: 150,
2162 is_discountable: true,
2163 product_category: {
2164 id: "catg_cotton",
2165 },
2166 product: {
2167 id: "prod_sweater",
2168 },
2169 },
2170 ],
2171 }
2172 )
2173
2174 expect(JSON.parse(JSON.stringify(result))).toEqual([
2175 {
2176 action: "addItemAdjustment",
2177 item_id: "item_cotton_tshirt",
2178 amount: 12.5,
2179 code: "PROMOTION_TEST_2",
2180 is_tax_inclusive: false,
2181 },
2182 {
2183 action: "addItemAdjustment",
2184 item_id: "item_cotton_sweater",
2185 amount: 37.5,
2186 code: "PROMOTION_TEST_2",
2187 is_tax_inclusive: false,
2188 },
2189 {
2190 action: "addItemAdjustment",
2191 item_id: "item_cotton_tshirt",
2192 amount: 7.5,
2193 code: "PROMOTION_TEST",
2194 is_tax_inclusive: false,
2195 },
2196 {
2197 action: "addItemAdjustment",
2198 item_id: "item_cotton_sweater",
2199 amount: 22.5,
2200 code: "PROMOTION_TEST",
2201 is_tax_inclusive: false,
2202 },
2203 ])
2204 })
2205
2206 it("should not compute actions when applicable total is 0", async () => {
2207 await createDefaultPromotion(service, {
2208 rules: [
2209 {
2210 attribute: "customer.customer_group.id",
2211 operator: "in",
2212 values: ["VIP", "top100"],
2213 },
2214 ],
2215 application_method: {
2216 type: "fixed",
2217 target_type: "items",
2218 allocation: "across",
2219 value: 1000,
2220 target_rules: [
2221 {
2222 attribute: "items.product_category.id",
2223 operator: "eq",
2224 values: ["catg_cotton"],
2225 },
2226 ],
2227 } as any,
2228 })
2229
2230 await createDefaultPromotion(service, {
2231 code: "PROMOTION_TEST_2",
2232 rules: [
2233 {
2234 attribute: "customer.customer_group.id",
2235 operator: "in",
2236 values: ["VIP", "top100"],
2237 },
2238 ],
2239 is_automatic: true,
2240 application_method: {
2241 type: "fixed",
2242 target_type: "items",
2243 allocation: "across",
2244 value: 50,
2245 target_rules: [
2246 {
2247 attribute: "items.product_category.id",
2248 operator: "eq",
2249 values: ["catg_cotton"],
2250 },
2251 ],
2252 } as any,
2253 })
2254
2255 const result = await service.computeActions(
2256 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
2257 {
2258 currency_code: "usd",
2259 customer: {
2260 customer_group: {
2261 id: "VIP",
2262 },
2263 },
2264 items: [
2265 {
2266 id: "item_cotton_tshirt",
2267 quantity: 1,
2268 subtotal: 50,
2269 original_total: 50,
2270 is_discountable: true,
2271 product_category: {
2272 id: "catg_cotton",
2273 },
2274 product: {
2275 id: "prod_tshirt",
2276 },
2277 },
2278 {
2279 id: "item_cotton_sweater",
2280 quantity: 1,
2281 subtotal: 150,
2282 original_total: 150,
2283 is_discountable: true,
2284 product_category: {
2285 id: "catg_cotton",
2286 },
2287 product: {
2288 id: "prod_sweater",
2289 },
2290 },
2291 ],
2292 }
2293 )
2294
2295 expect(JSON.parse(JSON.stringify(result))).toEqual([
2296 {
2297 action: "addItemAdjustment",
2298 item_id: "item_cotton_tshirt",
2299 amount: 50,
2300 code: "PROMOTION_TEST",
2301 is_tax_inclusive: false,
2302 },
2303 {
2304 action: "addItemAdjustment",
2305 item_id: "item_cotton_sweater",
2306 amount: 150,
2307 code: "PROMOTION_TEST",
2308 is_tax_inclusive: false,
2309 },
2310 ])
2311 })
2312
2313 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
2314 await createDefaultPromotion(service, {
2315 rules: [
2316 {
2317 attribute: "customer.customer_group.id",
2318 operator: "in",
2319 values: ["VIP", "top100"],
2320 },
2321 ],
2322 application_method: {
2323 type: "fixed",
2324 target_type: "items",
2325 allocation: "across",
2326 value: 1500,
2327 target_rules: [
2328 {
2329 attribute: "items.product_category.id",
2330 operator: "eq",
2331 values: ["catg_cotton"],
2332 },
2333 ],
2334 } as any,
2335 })
2336
2337 const result = await service.computeActions(["PROMOTION_TEST"], {
2338 currency_code: "usd",
2339 customer: {
2340 customer_group: {
2341 id: "VIP",
2342 },
2343 },
2344 items: [
2345 {
2346 id: "item_cotton_tshirt",
2347 quantity: 5,
2348 subtotal: 5000,
2349 original_total: 5000,
2350 is_discountable: true,
2351 product_category: {
2352 id: "catg_cotton",
2353 },
2354 product: {
2355 id: "prod_tshirt",
2356 },
2357 },
2358 ],
2359 })
2360
2361 expect(JSON.parse(JSON.stringify(result))).toEqual([
2362 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
2363 ])
2364 })
2365
2366 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
2367 await createDefaultPromotion(service, {
2368 rules: [
2369 {
2370 attribute: "customer.customer_group.id",
2371 operator: "in",
2372 values: ["VIP", "top100"],
2373 },
2374 ],
2375 campaign_id: "campaign-id-2",
2376 application_method: {
2377 type: "fixed",
2378 target_type: "items",
2379 allocation: "across",
2380 value: 500,
2381 target_rules: [
2382 {
2383 attribute: "items.product_category.id",
2384 operator: "eq",
2385 values: ["catg_cotton"],
2386 },
2387 ],
2388 } as any,
2389 })
2390
2391 await service.updateCampaigns({
2392 id: "campaign-id-2",
2393 budget: { used: 1000 },
2394 })
2395
2396 const result = await service.computeActions(["PROMOTION_TEST"], {
2397 currency_code: "usd",
2398 customer: {
2399 customer_group: {
2400 id: "VIP",
2401 },
2402 },
2403 items: [
2404 {
2405 id: "item_cotton_tshirt",
2406 quantity: 5,
2407 subtotal: 5000,
2408 original_total: 5000,
2409 is_discountable: true,
2410 product_category: {
2411 id: "catg_cotton",
2412 },
2413 product: {
2414 id: "prod_tshirt",
2415 },
2416 },
2417 ],
2418 })
2419
2420 expect(JSON.parse(JSON.stringify(result))).toEqual([
2421 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
2422 ])
2423 })
2424 })
2425
2426 describe("when application type is percentage", () => {
2427 it("should compute the correct item amendments", async () => {
2428 await createDefaultPromotion(service, {
2429 rules: [
2430 {
2431 attribute: "customer.customer_group.id",
2432 operator: "in",
2433 values: ["VIP", "top100"],
2434 },
2435 ],
2436 application_method: {
2437 type: ApplicationMethodType.PERCENTAGE,
2438 target_type: "items",
2439 allocation: "across",
2440 value: 10,
2441 target_rules: [
2442 {
2443 attribute: "items.product_category.id",
2444 operator: "eq",
2445 values: ["catg_cotton"],
2446 },
2447 ],
2448 } as any,
2449 })
2450
2451 const result = await service.computeActions(["PROMOTION_TEST"], {
2452 currency_code: "usd",
2453 customer: {
2454 customer_group: {
2455 id: "VIP",
2456 },
2457 },
2458 items: [
2459 {
2460 id: "item_cotton_tshirt",
2461 quantity: 2,
2462 subtotal: 200,
2463 original_total: 200,
2464 is_discountable: true,
2465 product_category: {
2466 id: "catg_cotton",
2467 },
2468 product: {
2469 id: "prod_tshirt",
2470 },
2471 },
2472 {
2473 id: "item_cotton_sweater",
2474 quantity: 2,
2475 subtotal: 600,
2476 original_total: 600,
2477 is_discountable: true,
2478 product_category: {
2479 id: "catg_cotton",
2480 },
2481 product: {
2482 id: "prod_sweater",
2483 },
2484 },
2485 ],
2486 })
2487
2488 expect(JSON.parse(JSON.stringify(result))).toEqual([
2489 {
2490 action: "addItemAdjustment",
2491 item_id: "item_cotton_tshirt",
2492 amount: 20,
2493 code: "PROMOTION_TEST",
2494 is_tax_inclusive: false,
2495 },
2496 {
2497 action: "addItemAdjustment",
2498 item_id: "item_cotton_sweater",
2499 amount: 60,
2500 code: "PROMOTION_TEST",
2501 is_tax_inclusive: false,
2502 },
2503 ])
2504 })
2505
2506 it("should compute the correct item amendments when promotion is automatic", async () => {
2507 await createDefaultPromotion(service, {
2508 rules: [
2509 {
2510 attribute: "customer.customer_group.id",
2511 operator: "in",
2512 values: ["VIP", "top100"],
2513 },
2514 ],
2515 is_automatic: true,
2516 application_method: {
2517 type: ApplicationMethodType.PERCENTAGE,
2518 target_type: "items",
2519 allocation: "across",
2520 value: 10,
2521 target_rules: [
2522 {
2523 attribute: "items.product_category.id",
2524 operator: "eq",
2525 values: ["catg_cotton"],
2526 },
2527 ],
2528 } as any,
2529 })
2530
2531 const result = await service.computeActions([], {
2532 currency_code: "usd",
2533 customer: {
2534 customer_group: {
2535 id: "VIP",
2536 },
2537 },
2538 items: [
2539 {
2540 id: "item_cotton_tshirt",
2541 quantity: 2,
2542 subtotal: 200,
2543 original_total: 200,
2544 is_discountable: true,
2545 product_category: {
2546 id: "catg_cotton",
2547 },
2548 product: {
2549 id: "prod_tshirt",
2550 },
2551 },
2552 {
2553 id: "item_cotton_sweater",
2554 quantity: 2,
2555 subtotal: 600,
2556 original_total: 600,
2557 is_discountable: true,
2558 product_category: {
2559 id: "catg_cotton",
2560 },
2561 product: {
2562 id: "prod_sweater",
2563 },
2564 },
2565 ],
2566 })
2567
2568 expect(JSON.parse(JSON.stringify(result))).toEqual([
2569 {
2570 action: "addItemAdjustment",
2571 item_id: "item_cotton_tshirt",
2572 amount: 20,
2573 code: "PROMOTION_TEST",
2574 is_tax_inclusive: false,
2575 },
2576 {
2577 action: "addItemAdjustment",
2578 item_id: "item_cotton_sweater",
2579 amount: 60,
2580 code: "PROMOTION_TEST",
2581 is_tax_inclusive: false,
2582 },
2583 ])
2584 })
2585
2586 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
2587 await createDefaultPromotion(service, {
2588 rules: [
2589 {
2590 attribute: "customer.customer_group.id",
2591 operator: "in",
2592 values: ["VIP", "top100"],
2593 },
2594 ],
2595 application_method: {
2596 type: ApplicationMethodType.PERCENTAGE,
2597 target_type: "items",
2598 allocation: "across",
2599 value: 10,
2600 target_rules: [
2601 {
2602 attribute: "items.product_category.id",
2603 operator: "eq",
2604 values: ["catg_cotton"],
2605 },
2606 ],
2607 } as any,
2608 })
2609
2610 await createDefaultPromotion(service, {
2611 code: "PROMOTION_TEST_2",
2612 rules: [
2613 {
2614 attribute: "customer.customer_group.id",
2615 operator: "in",
2616 values: ["VIP", "top100"],
2617 },
2618 ],
2619 application_method: {
2620 type: ApplicationMethodType.PERCENTAGE,
2621 target_type: "items",
2622 allocation: "across",
2623 value: 10,
2624 target_rules: [
2625 {
2626 attribute: "items.product_category.id",
2627 operator: "eq",
2628 values: ["catg_cotton"],
2629 },
2630 ],
2631 } as any,
2632 })
2633
2634 const result = await service.computeActions(
2635 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
2636 {
2637 currency_code: "usd",
2638 customer: {
2639 customer_group: {
2640 id: "VIP",
2641 },
2642 },
2643 items: [
2644 {
2645 id: "item_cotton_tshirt",
2646 quantity: 1,
2647 subtotal: 50,
2648 original_total: 50,
2649 is_discountable: true,
2650 product_category: {
2651 id: "catg_cotton",
2652 },
2653 product: {
2654 id: "prod_tshirt",
2655 },
2656 },
2657 {
2658 id: "item_cotton_sweater",
2659 quantity: 1,
2660 subtotal: 150,
2661 original_total: 150,
2662 is_discountable: true,
2663 product_category: {
2664 id: "catg_cotton",
2665 },
2666 product: {
2667 id: "prod_sweater",
2668 },
2669 },
2670 ],
2671 }
2672 )
2673
2674 expect(JSON.parse(JSON.stringify(result))).toEqual([
2675 {
2676 action: "addItemAdjustment",
2677 item_id: "item_cotton_tshirt",
2678 amount: 5,
2679 code: "PROMOTION_TEST",
2680 is_tax_inclusive: false,
2681 },
2682 {
2683 action: "addItemAdjustment",
2684 item_id: "item_cotton_sweater",
2685 amount: 15,
2686 code: "PROMOTION_TEST",
2687 is_tax_inclusive: false,
2688 },
2689 {
2690 action: "addItemAdjustment",
2691 item_id: "item_cotton_tshirt",
2692 amount: 4.5,
2693 code: "PROMOTION_TEST_2",
2694 is_tax_inclusive: false,
2695 },
2696 {
2697 action: "addItemAdjustment",
2698 item_id: "item_cotton_sweater",
2699 amount: 13.5,
2700 code: "PROMOTION_TEST_2",
2701 is_tax_inclusive: false,
2702 },
2703 ])
2704 })
2705
2706 it("should compute actions when 2 percentage promotions are applied to the same item", async () => {
2707 await createDefaultPromotion(service, {
2708 code: "PROMO_PERCENTAGE_1",
2709 rules: [],
2710 application_method: {
2711 type: ApplicationMethodType.PERCENTAGE,
2712 target_type: "items",
2713 allocation: "across",
2714 value: 50,
2715 target_rules: [
2716 {
2717 attribute: "items.product.id",
2718 operator: "eq",
2719 values: ["prod_tshirt"],
2720 },
2721 ],
2722 },
2723 })
2724
2725 await createDefaultPromotion(service, {
2726 code: "PROMO_PERCENTAGE_2",
2727 rules: [],
2728 application_method: {
2729 type: ApplicationMethodType.PERCENTAGE,
2730 target_type: "items",
2731 allocation: "across",
2732 value: 50,
2733 target_rules: [
2734 {
2735 attribute: "items.product.id",
2736 operator: "eq",
2737 values: ["prod_tshirt"],
2738 },
2739 ],
2740 },
2741 })
2742
2743 const result = await service.computeActions(
2744 ["PROMO_PERCENTAGE_1", "PROMO_PERCENTAGE_2"],
2745 {
2746 currency_code: "usd",
2747 items: [
2748 {
2749 id: "item_cotton_tshirt",
2750 quantity: 4,
2751 subtotal: 300,
2752 original_total: 300,
2753 is_discountable: true,
2754 product: {
2755 id: "prod_tshirt",
2756 },
2757 },
2758 {
2759 id: "item_wool_tshirt",
2760 quantity: 4,
2761 subtotal: 100,
2762 original_total: 100,
2763 is_discountable: true,
2764 product: {
2765 id: "prod_tshirt",
2766 },
2767 },
2768 ],
2769 }
2770 )
2771
2772 expect(JSON.parse(JSON.stringify(result))).toEqual([
2773 {
2774 action: "addItemAdjustment",
2775 item_id: "item_cotton_tshirt",
2776 amount: 150,
2777 code: "PROMO_PERCENTAGE_1",
2778 is_tax_inclusive: false,
2779 },
2780 {
2781 action: "addItemAdjustment",
2782 item_id: "item_wool_tshirt",
2783 amount: 50,
2784 code: "PROMO_PERCENTAGE_1",
2785 is_tax_inclusive: false,
2786 },
2787 {
2788 action: "addItemAdjustment",
2789 item_id: "item_cotton_tshirt",
2790 amount: 75,
2791 code: "PROMO_PERCENTAGE_2",
2792 is_tax_inclusive: false,
2793 },
2794 {
2795 action: "addItemAdjustment",
2796 item_id: "item_wool_tshirt",
2797 amount: 25,
2798 code: "PROMO_PERCENTAGE_2",
2799 is_tax_inclusive: false,
2800 },
2801 ])
2802
2803 await createDefaultPromotion(service, {
2804 code: "PROMO_PERCENTAGE_3",
2805 rules: [],
2806 application_method: {
2807 type: ApplicationMethodType.PERCENTAGE,
2808 target_type: "items",
2809 allocation: "across",
2810 value: 100,
2811 target_rules: [
2812 {
2813 attribute: "items.product.id",
2814 operator: "eq",
2815 values: ["prod_tshirt"],
2816 },
2817 ],
2818 },
2819 })
2820
2821 const result2 = await service.computeActions(
2822 [
2823 "PROMO_PERCENTAGE_1",
2824 "PROMO_PERCENTAGE_2",
2825 "PROMO_PERCENTAGE_3",
2826 ],
2827 {
2828 currency_code: "usd",
2829 items: [
2830 {
2831 id: "item_cotton_tshirt",
2832 quantity: 4,
2833 subtotal: 300,
2834 original_total: 300,
2835 is_discountable: true,
2836 product: {
2837 id: "prod_tshirt",
2838 },
2839 },
2840 {
2841 id: "item_wool_tshirt",
2842 quantity: 4,
2843 subtotal: 100,
2844 original_total: 100,
2845 is_discountable: true,
2846 product: {
2847 id: "prod_tshirt",
2848 },
2849 },
2850 ],
2851 }
2852 )
2853
2854 // Should only apply the most valuable promotion (100% off - PROMO_PERCENTAGE_3) that covers
2855 // the entire total of the line item
2856 expect(JSON.parse(JSON.stringify(result2))).toEqual([
2857 {
2858 action: "addItemAdjustment",
2859 item_id: "item_cotton_tshirt",
2860 amount: 300,
2861 code: "PROMO_PERCENTAGE_3",
2862 is_tax_inclusive: false,
2863 },
2864 {
2865 action: "addItemAdjustment",
2866 item_id: "item_wool_tshirt",
2867 amount: 100,
2868 code: "PROMO_PERCENTAGE_3",
2869 is_tax_inclusive: false,
2870 },
2871 ])
2872 })
2873
2874 it("should not compute actions when applicable total is 0", async () => {
2875 await createDefaultPromotion(service, {
2876 rules: [
2877 {
2878 attribute: "customer.customer_group.id",
2879 operator: "in",
2880 values: ["VIP", "top100"],
2881 },
2882 ],
2883 application_method: {
2884 type: ApplicationMethodType.PERCENTAGE,
2885 target_type: "items",
2886 allocation: "across",
2887 value: 10,
2888 target_rules: [
2889 {
2890 attribute: "items.product_category.id",
2891 operator: "eq",
2892 values: ["catg_cotton"],
2893 },
2894 ],
2895 } as any,
2896 })
2897
2898 await createDefaultPromotion(service, {
2899 code: "PROMOTION_TEST_2",
2900 rules: [
2901 {
2902 attribute: "customer.customer_group.id",
2903 operator: "in",
2904 values: ["VIP", "top100"],
2905 },
2906 ],
2907 application_method: {
2908 type: ApplicationMethodType.PERCENTAGE,
2909 target_type: "items",
2910 allocation: "across",
2911 value: 10,
2912 target_rules: [
2913 {
2914 attribute: "items.product_category.id",
2915 operator: "eq",
2916 values: ["catg_cotton"],
2917 },
2918 ],
2919 } as any,
2920 })
2921
2922 const result = await service.computeActions(
2923 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
2924 {
2925 currency_code: "usd",
2926 customer: {
2927 customer_group: {
2928 id: "VIP",
2929 },
2930 },
2931 items: [
2932 {
2933 id: "item_cotton_tshirt",
2934 quantity: 1,
2935 subtotal: 50,
2936 original_total: 50,
2937 is_discountable: true,
2938 product_category: {
2939 id: "catg_cotton",
2940 },
2941 product: {
2942 id: "prod_tshirt",
2943 },
2944 },
2945 {
2946 id: "item_cotton_sweater",
2947 quantity: 1,
2948 subtotal: 150,
2949 original_total: 150,
2950 is_discountable: true,
2951 product_category: {
2952 id: "catg_cotton",
2953 },
2954 product: {
2955 id: "prod_sweater",
2956 },
2957 },
2958 ],
2959 }
2960 )
2961
2962 expect(JSON.parse(JSON.stringify(result))).toEqual([
2963 {
2964 action: "addItemAdjustment",
2965 item_id: "item_cotton_tshirt",
2966 amount: 5,
2967 code: "PROMOTION_TEST",
2968 is_tax_inclusive: false,
2969 },
2970 {
2971 action: "addItemAdjustment",
2972 item_id: "item_cotton_sweater",
2973 amount: 15,
2974 code: "PROMOTION_TEST",
2975 is_tax_inclusive: false,
2976 },
2977 {
2978 action: "addItemAdjustment",
2979 item_id: "item_cotton_tshirt",
2980 amount: 4.5,
2981 code: "PROMOTION_TEST_2",
2982 is_tax_inclusive: false,
2983 },
2984 {
2985 action: "addItemAdjustment",
2986 item_id: "item_cotton_sweater",
2987 amount: 13.5,
2988 code: "PROMOTION_TEST_2",
2989 is_tax_inclusive: false,
2990 },
2991 ])
2992 })
2993
2994 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
2995 await createDefaultPromotion(service, {
2996 rules: [
2997 {
2998 attribute: "customer.customer_group.id",
2999 operator: "in",
3000 values: ["VIP", "top100"],
3001 },
3002 ],
3003 application_method: {
3004 type: ApplicationMethodType.PERCENTAGE,
3005 target_type: "items",
3006 allocation: "across",
3007 value: 100,
3008 target_rules: [
3009 {
3010 attribute: "items.product_category.id",
3011 operator: "eq",
3012 values: ["catg_cotton"],
3013 },
3014 ],
3015 } as any,
3016 })
3017
3018 const result = await service.computeActions(["PROMOTION_TEST"], {
3019 currency_code: "usd",
3020 customer: {
3021 customer_group: {
3022 id: "VIP",
3023 },
3024 },
3025 items: [
3026 {
3027 id: "item_cotton_tshirt",
3028 quantity: 5,
3029 subtotal: 5000,
3030 original_total: 5000,
3031 is_discountable: true,
3032 product_category: {
3033 id: "catg_cotton",
3034 },
3035 product: {
3036 id: "prod_tshirt",
3037 },
3038 },
3039 ],
3040 })
3041
3042 expect(JSON.parse(JSON.stringify(result))).toEqual([
3043 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
3044 ])
3045 })
3046
3047 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
3048 await createDefaultPromotion(service, {
3049 rules: [
3050 {
3051 attribute: "customer.customer_group.id",
3052 operator: "in",
3053 values: ["VIP", "top100"],
3054 },
3055 ],
3056 campaign_id: "campaign-id-2",
3057 application_method: {
3058 type: ApplicationMethodType.PERCENTAGE,
3059 target_type: "items",
3060 allocation: "across",
3061 value: 10,
3062 target_rules: [
3063 {
3064 attribute: "items.product_category.id",
3065 operator: "eq",
3066 values: ["catg_cotton"],
3067 },
3068 ],
3069 } as any,
3070 })
3071
3072 await service.updateCampaigns({
3073 id: "campaign-id-2",
3074 budget: { used: 1000 },
3075 })
3076
3077 const result = await service.computeActions(["PROMOTION_TEST"], {
3078 currency_code: "usd",
3079 customer: {
3080 customer_group: {
3081 id: "VIP",
3082 },
3083 },
3084 items: [
3085 {
3086 id: "item_cotton_tshirt",
3087 quantity: 5,
3088 subtotal: 5000,
3089 original_total: 5000,
3090 is_discountable: true,
3091 product_category: {
3092 id: "catg_cotton",
3093 },
3094 product: {
3095 id: "prod_tshirt",
3096 },
3097 },
3098 ],
3099 })
3100
3101 expect(JSON.parse(JSON.stringify(result))).toEqual([
3102 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
3103 ])
3104 })
3105
3106 it("should compute budget exceeded action when usage by attribute exceeds campaign budget for type use_by_attribute", async () => {
3107 const testCampaign = await service.createCampaigns({
3108 name: "test",
3109 campaign_identifier: "test",
3110 budget: {
3111 type: CampaignBudgetType.USE_BY_ATTRIBUTE,
3112 attribute: "customer_email",
3113 limit: 2,
3114 },
3115 })
3116
3117 await createDefaultPromotion(service, {
3118 campaign_id: testCampaign.id,
3119 application_method: {
3120 type: ApplicationMethodType.PERCENTAGE,
3121 target_type: "items",
3122 allocation: "across",
3123 value: 10,
3124 target_rules: [
3125 {
3126 attribute: "product_category.id",
3127 operator: "eq",
3128 values: ["catg_cotton"],
3129 },
3130 ],
3131 } as any,
3132 })
3133
3134 let result = await service.computeActions(["PROMOTION_TEST"], {
3135 currency_code: "usd",
3136 email: "test@test.com",
3137 customer: {
3138 email: "test@test.com",
3139 },
3140 items: [
3141 {
3142 id: "item_cotton_tshirt",
3143 quantity: 1,
3144 subtotal: 100,
3145 original_total: 100,
3146 is_discountable: true,
3147 product_category: {
3148 id: "catg_cotton",
3149 },
3150 product: {
3151 id: "prod_tshirt",
3152 },
3153 },
3154 ],
3155 })
3156
3157 expect(JSON.parse(JSON.stringify(result))).toEqual([
3158 {
3159 action: "addItemAdjustment",
3160 code: "PROMOTION_TEST",
3161 amount: 10,
3162 is_tax_inclusive: false,
3163 item_id: "item_cotton_tshirt",
3164 },
3165 ])
3166
3167 await service.registerUsage(
3168 [{ amount: 10, code: "PROMOTION_TEST" }],
3169 {
3170 customer_id: null,
3171 customer_email: "test@test.com",
3172 }
3173 )
3174
3175 await service.registerUsage(
3176 [{ amount: 10, code: "PROMOTION_TEST" }],
3177 {
3178 customer_id: null,
3179 customer_email: "test@test.com",
3180 }
3181 )
3182
3183 result = await service.computeActions(["PROMOTION_TEST"], {
3184 currency_code: "usd",
3185 email: "test@test.com",
3186 customer: {
3187 email: "test@test.com",
3188 },
3189 items: [
3190 {
3191 id: "item_cotton_tshirt",
3192 quantity: 1,
3193 subtotal: 100,
3194 original_total: 100,
3195 is_discountable: true,
3196 product_category: {
3197 id: "catg_cotton",
3198 },
3199 product: {
3200 id: "prod_tshirt",
3201 },
3202 },
3203 ],
3204 })
3205
3206 expect(JSON.parse(JSON.stringify(result))).toEqual([
3207 {
3208 action: "campaignBudgetExceeded",
3209 code: "PROMOTION_TEST",
3210 },
3211 ])
3212
3213 result = await service.computeActions(["PROMOTION_TEST"], {
3214 currency_code: "usd",
3215 email: "another@test.com", // another email can sucessfully use the promotion
3216 customer: {
3217 email: "another@test.com",
3218 },
3219 items: [
3220 {
3221 id: "item_cotton_tshirt",
3222 quantity: 1,
3223 subtotal: 100,
3224 original_total: 100,
3225 is_discountable: true,
3226 product_category: {
3227 id: "catg_cotton",
3228 },
3229 product: {
3230 id: "prod_tshirt",
3231 },
3232 },
3233 ],
3234 })
3235
3236 expect(JSON.parse(JSON.stringify(result))).toEqual([
3237 {
3238 action: "addItemAdjustment",
3239 code: "PROMOTION_TEST",
3240 amount: 10,
3241 is_tax_inclusive: false,
3242 item_id: "item_cotton_tshirt",
3243 },
3244 ])
3245 })
3246 })
3247 })
3248
3249 describe("when promotion is for shipping_method and allocation is each", () => {
3250 describe("when application type is fixed", () => {
3251 it("should compute the correct shipping_method amendments", async () => {
3252 await createDefaultPromotion(service, {
3253 rules: [
3254 {
3255 attribute: "customer.customer_group.id",
3256 operator: "in",
3257 values: ["VIP", "top100"],
3258 },
3259 ],
3260 application_method: {
3261 type: "fixed",
3262 target_type: "shipping_methods",
3263 allocation: "each",
3264 value: 200,
3265 max_quantity: 2,
3266 target_rules: [
3267 {
3268 attribute: "shipping_methods.shipping_option.id",
3269 operator: "in",
3270 values: ["express", "standard"],
3271 },
3272 ],
3273 } as any,
3274 })
3275
3276 const result = await service.computeActions(["PROMOTION_TEST"], {
3277 currency_code: "usd",
3278 customer: {
3279 customer_group: {
3280 id: "VIP",
3281 },
3282 },
3283 shipping_methods: [
3284 {
3285 id: "shipping_method_express",
3286 subtotal: 250,
3287 original_total: 250,
3288 is_discountable: true,
3289 shipping_option: {
3290 id: "express",
3291 },
3292 },
3293 {
3294 id: "shipping_method_standard",
3295 subtotal: 150,
3296 original_total: 150,
3297 is_discountable: true,
3298 shipping_option: {
3299 id: "standard",
3300 },
3301 },
3302 {
3303 id: "shipping_method_snail",
3304 subtotal: 200,
3305 original_total: 200,
3306 is_discountable: true,
3307 shipping_option: {
3308 id: "snail",
3309 },
3310 },
3311 ],
3312 })
3313
3314 expect(JSON.parse(JSON.stringify(result))).toEqual([
3315 {
3316 action: "addShippingMethodAdjustment",
3317 shipping_method_id: "shipping_method_express",
3318 amount: 200,
3319 code: "PROMOTION_TEST",
3320 },
3321 {
3322 action: "addShippingMethodAdjustment",
3323 shipping_method_id: "shipping_method_standard",
3324 amount: 150,
3325 code: "PROMOTION_TEST",
3326 },
3327 ])
3328 })
3329
3330 it("should compute the correct shipping_method amendments when promotion is automatic", async () => {
3331 await createDefaultPromotion(service, {
3332 rules: [
3333 {
3334 attribute: "customer.customer_group.id",
3335 operator: "in",
3336 values: ["VIP", "top100"],
3337 },
3338 ],
3339 is_automatic: true,
3340 application_method: {
3341 type: "fixed",
3342 target_type: "shipping_methods",
3343 allocation: "each",
3344 value: 200,
3345 max_quantity: 2,
3346 target_rules: [
3347 {
3348 attribute: "shipping_methods.shipping_option.id",
3349 operator: "in",
3350 values: ["express", "standard"],
3351 },
3352 ],
3353 } as any,
3354 })
3355
3356 const result = await service.computeActions([], {
3357 currency_code: "usd",
3358 customer: {
3359 customer_group: {
3360 id: "VIP",
3361 },
3362 },
3363 shipping_methods: [
3364 {
3365 id: "shipping_method_express",
3366 subtotal: 250,
3367 original_total: 250,
3368 is_discountable: true,
3369 shipping_option: {
3370 id: "express",
3371 },
3372 },
3373 {
3374 id: "shipping_method_standard",
3375 subtotal: 150,
3376 original_total: 150,
3377 is_discountable: true,
3378 shipping_option: {
3379 id: "standard",
3380 },
3381 },
3382 {
3383 id: "shipping_method_snail",
3384 subtotal: 200,
3385 original_total: 200,
3386 is_discountable: true,
3387 shipping_option: {
3388 id: "snail",
3389 },
3390 },
3391 ],
3392 })
3393
3394 expect(JSON.parse(JSON.stringify(result))).toEqual([
3395 {
3396 action: "addShippingMethodAdjustment",
3397 shipping_method_id: "shipping_method_express",
3398 amount: 200,
3399 code: "PROMOTION_TEST",
3400 },
3401 {
3402 action: "addShippingMethodAdjustment",
3403 shipping_method_id: "shipping_method_standard",
3404 amount: 150,
3405 code: "PROMOTION_TEST",
3406 },
3407 ])
3408 })
3409
3410 it("should compute the correct shipping_method amendments when promotion is automatic and prevent_auto_promotions is false", async () => {
3411 await createDefaultPromotion(service, {
3412 rules: [
3413 {
3414 attribute: "customer.customer_group.id",
3415 operator: "in",
3416 values: ["VIP", "top100"],
3417 },
3418 ],
3419 is_automatic: true,
3420 application_method: {
3421 type: "fixed",
3422 target_type: "shipping_methods",
3423 allocation: "each",
3424 value: 200,
3425 max_quantity: 2,
3426 target_rules: [
3427 {
3428 attribute: "shipping_methods.shipping_option.id",
3429 operator: "in",
3430 values: ["express", "standard"],
3431 },
3432 ],
3433 } as any,
3434 })
3435
3436 const result = await service.computeActions(
3437 [],
3438 {
3439 currency_code: "usd",
3440 customer: {
3441 customer_group: {
3442 id: "VIP",
3443 },
3444 },
3445 shipping_methods: [
3446 {
3447 id: "shipping_method_express",
3448 subtotal: 250,
3449 original_total: 250,
3450 is_discountable: true,
3451 shipping_option: {
3452 id: "express",
3453 },
3454 },
3455 {
3456 id: "shipping_method_standard",
3457 subtotal: 150,
3458 original_total: 150,
3459 is_discountable: true,
3460 shipping_option: {
3461 id: "standard",
3462 },
3463 },
3464 {
3465 id: "shipping_method_snail",
3466 subtotal: 200,
3467 original_total: 200,
3468 is_discountable: true,
3469 shipping_option: {
3470 id: "snail",
3471 },
3472 },
3473 ],
3474 },
3475 { prevent_auto_promotions: true }
3476 )
3477
3478 expect(JSON.parse(JSON.stringify(result))).toEqual([])
3479 })
3480
3481 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
3482 await createDefaultPromotion(service, {
3483 rules: [
3484 {
3485 attribute: "customer.customer_group.id",
3486 operator: "in",
3487 values: ["VIP", "top100"],
3488 },
3489 ],
3490 application_method: {
3491 type: "fixed",
3492 target_type: "shipping_methods",
3493 allocation: "each",
3494 value: 200,
3495 max_quantity: 2,
3496 target_rules: [
3497 {
3498 attribute: "shipping_methods.shipping_option.id",
3499 operator: "in",
3500 values: ["express", "standard"],
3501 },
3502 ],
3503 } as any,
3504 })
3505
3506 await createDefaultPromotion(service, {
3507 code: "PROMOTION_TEST_2",
3508 rules: [
3509 {
3510 attribute: "customer.customer_group.id",
3511 operator: "in",
3512 values: ["VIP", "top100"],
3513 },
3514 ],
3515 application_method: {
3516 type: "fixed",
3517 target_type: "shipping_methods",
3518 allocation: "each",
3519 value: 200,
3520 max_quantity: 2,
3521 target_rules: [
3522 {
3523 attribute: "shipping_methods.shipping_option.id",
3524 operator: "in",
3525 values: ["express", "standard"],
3526 },
3527 ],
3528 } as any,
3529 })
3530
3531 const result = await service.computeActions(
3532 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
3533 {
3534 currency_code: "usd",
3535 customer: {
3536 customer_group: {
3537 id: "VIP",
3538 },
3539 },
3540 shipping_methods: [
3541 {
3542 id: "shipping_method_express",
3543 subtotal: 250,
3544 original_total: 250,
3545 is_discountable: true,
3546 shipping_option: {
3547 id: "express",
3548 },
3549 },
3550 {
3551 id: "shipping_method_standard",
3552 subtotal: 150,
3553 original_total: 150,
3554 is_discountable: true,
3555 shipping_option: {
3556 id: "standard",
3557 },
3558 },
3559 {
3560 id: "shipping_method_snail",
3561 subtotal: 200,
3562 original_total: 200,
3563 is_discountable: true,
3564 shipping_option: {
3565 id: "snail",
3566 },
3567 },
3568 ],
3569 }
3570 )
3571
3572 expect(JSON.parse(JSON.stringify(result))).toEqual([
3573 {
3574 action: "addShippingMethodAdjustment",
3575 shipping_method_id: "shipping_method_express",
3576 amount: 200,
3577 code: "PROMOTION_TEST",
3578 },
3579 {
3580 action: "addShippingMethodAdjustment",
3581 shipping_method_id: "shipping_method_standard",
3582 amount: 150,
3583 code: "PROMOTION_TEST",
3584 },
3585 {
3586 action: "addShippingMethodAdjustment",
3587 shipping_method_id: "shipping_method_express",
3588 amount: 50,
3589 code: "PROMOTION_TEST_2",
3590 },
3591 ])
3592 })
3593
3594 it("should not compute actions when applicable total is 0", async () => {
3595 await createDefaultPromotion(service, {
3596 rules: [
3597 {
3598 attribute: "customer.customer_group.id",
3599 operator: "in",
3600 values: ["VIP", "top100"],
3601 },
3602 ],
3603 application_method: {
3604 type: "fixed",
3605 target_type: "shipping_methods",
3606 allocation: "each",
3607 value: 500,
3608 max_quantity: 2,
3609 target_rules: [
3610 {
3611 attribute: "shipping_methods.shipping_option.id",
3612 operator: "in",
3613 values: ["express", "standard"],
3614 },
3615 ],
3616 } as any,
3617 })
3618
3619 await createDefaultPromotion(service, {
3620 code: "PROMOTION_TEST_2",
3621 rules: [
3622 {
3623 attribute: "customer.customer_group.id",
3624 operator: "in",
3625 values: ["VIP", "top100"],
3626 },
3627 ],
3628 application_method: {
3629 type: "fixed",
3630 target_type: "shipping_methods",
3631 allocation: "each",
3632 value: 200,
3633 max_quantity: 2,
3634 target_rules: [
3635 {
3636 attribute: "shipping_methods.shipping_option.id",
3637 operator: "in",
3638 values: ["express", "standard"],
3639 },
3640 ],
3641 } as any,
3642 })
3643
3644 const result = await service.computeActions(
3645 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
3646 {
3647 currency_code: "usd",
3648 customer: {
3649 customer_group: {
3650 id: "VIP",
3651 },
3652 },
3653 shipping_methods: [
3654 {
3655 id: "shipping_method_express",
3656 subtotal: 250,
3657 original_total: 250,
3658 is_discountable: true,
3659 shipping_option: {
3660 id: "express",
3661 },
3662 },
3663 {
3664 id: "shipping_method_standard",
3665 subtotal: 150,
3666 original_total: 150,
3667 is_discountable: true,
3668 shipping_option: {
3669 id: "standard",
3670 },
3671 },
3672 {
3673 id: "shipping_method_snail",
3674 subtotal: 200,
3675 original_total: 200,
3676 is_discountable: true,
3677 shipping_option: {
3678 id: "snail",
3679 },
3680 },
3681 ],
3682 }
3683 )
3684
3685 expect(JSON.parse(JSON.stringify(result))).toEqual([
3686 {
3687 action: "addShippingMethodAdjustment",
3688 shipping_method_id: "shipping_method_express",
3689 amount: 250,
3690 code: "PROMOTION_TEST",
3691 },
3692 {
3693 action: "addShippingMethodAdjustment",
3694 shipping_method_id: "shipping_method_standard",
3695 amount: 150,
3696 code: "PROMOTION_TEST",
3697 },
3698 ])
3699 })
3700
3701 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
3702 await createDefaultPromotion(service, {
3703 rules: [
3704 {
3705 attribute: "customer.customer_group.id",
3706 operator: "in",
3707 values: ["VIP", "top100"],
3708 },
3709 ],
3710 application_method: {
3711 type: "fixed",
3712 target_type: "shipping_methods",
3713 allocation: "each",
3714 value: 1200,
3715 max_quantity: 2,
3716 target_rules: [
3717 {
3718 attribute: "shipping_methods.shipping_option.id",
3719 operator: "in",
3720 values: ["express", "standard"],
3721 },
3722 ],
3723 } as any,
3724 })
3725
3726 const result = await service.computeActions(["PROMOTION_TEST"], {
3727 currency_code: "usd",
3728 customer: {
3729 customer_group: {
3730 id: "VIP",
3731 },
3732 },
3733 shipping_methods: [
3734 {
3735 id: "shipping_method_express",
3736 subtotal: 1200,
3737 original_total: 1200,
3738 is_discountable: true,
3739 shipping_option: {
3740 id: "express",
3741 },
3742 },
3743 ],
3744 })
3745
3746 expect(JSON.parse(JSON.stringify(result))).toEqual([
3747 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
3748 ])
3749 })
3750
3751 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
3752 await createDefaultPromotion(service, {
3753 rules: [
3754 {
3755 attribute: "customer.customer_group.id",
3756 operator: "in",
3757 values: ["VIP", "top100"],
3758 },
3759 ],
3760 campaign_id: "campaign-id-2",
3761 application_method: {
3762 type: "fixed",
3763 target_type: "shipping_methods",
3764 allocation: "each",
3765 value: 1200,
3766 max_quantity: 2,
3767 target_rules: [
3768 {
3769 attribute: "shipping_methods.shipping_option.id",
3770 operator: "in",
3771 values: ["express", "standard"],
3772 },
3773 ],
3774 } as any,
3775 })
3776
3777 await service.updateCampaigns({
3778 id: "campaign-id-2",
3779 budget: { used: 1000 },
3780 })
3781
3782 const result = await service.computeActions(["PROMOTION_TEST"], {
3783 currency_code: "usd",
3784 customer: {
3785 customer_group: {
3786 id: "VIP",
3787 },
3788 },
3789 shipping_methods: [
3790 {
3791 id: "shipping_method_express",
3792 subtotal: 1200,
3793 original_total: 1200,
3794 is_discountable: true,
3795 shipping_option: {
3796 id: "express",
3797 },
3798 },
3799 ],
3800 })
3801
3802 expect(JSON.parse(JSON.stringify(result))).toEqual([
3803 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
3804 ])
3805 })
3806 })
3807
3808 describe("when application type is percentage", () => {
3809 it("should compute the correct shipping_method amendments", async () => {
3810 await createDefaultPromotion(service, {
3811 rules: [
3812 {
3813 attribute: "customer.customer_group.id",
3814 operator: "in",
3815 values: ["VIP", "top100"],
3816 },
3817 ],
3818 application_method: {
3819 type: ApplicationMethodType.PERCENTAGE,
3820 target_type: "shipping_methods",
3821 allocation: "each",
3822 value: 10,
3823 max_quantity: 2,
3824 target_rules: [
3825 {
3826 attribute: "shipping_methods.shipping_option.id",
3827 operator: "in",
3828 values: ["express", "standard"],
3829 },
3830 ],
3831 } as any,
3832 })
3833
3834 const result = await service.computeActions(["PROMOTION_TEST"], {
3835 currency_code: "usd",
3836 customer: {
3837 customer_group: {
3838 id: "VIP",
3839 },
3840 },
3841 shipping_methods: [
3842 {
3843 id: "shipping_method_express",
3844 subtotal: 250,
3845 original_total: 250,
3846 is_discountable: true,
3847 shipping_option: {
3848 id: "express",
3849 },
3850 },
3851 {
3852 id: "shipping_method_standard",
3853 subtotal: 150,
3854 original_total: 150,
3855 is_discountable: true,
3856 shipping_option: {
3857 id: "standard",
3858 },
3859 },
3860 {
3861 id: "shipping_method_snail",
3862 subtotal: 200,
3863 original_total: 200,
3864 is_discountable: true,
3865 shipping_option: {
3866 id: "snail",
3867 },
3868 },
3869 ],
3870 })
3871
3872 expect(JSON.parse(JSON.stringify(result))).toEqual([
3873 {
3874 action: "addShippingMethodAdjustment",
3875 shipping_method_id: "shipping_method_express",
3876 amount: 25,
3877 code: "PROMOTION_TEST",
3878 },
3879 {
3880 action: "addShippingMethodAdjustment",
3881 shipping_method_id: "shipping_method_standard",
3882 amount: 15,
3883 code: "PROMOTION_TEST",
3884 },
3885 ])
3886 })
3887
3888 it("should compute the correct shipping_method amendments when promotion is automatic", async () => {
3889 await createDefaultPromotion(service, {
3890 rules: [
3891 {
3892 attribute: "customer.customer_group.id",
3893 operator: "in",
3894 values: ["VIP", "top100"],
3895 },
3896 ],
3897 is_automatic: true,
3898 application_method: {
3899 type: ApplicationMethodType.PERCENTAGE,
3900 target_type: "shipping_methods",
3901 allocation: "each",
3902 value: 10,
3903 max_quantity: 2,
3904 target_rules: [
3905 {
3906 attribute: "shipping_methods.shipping_option.id",
3907 operator: "in",
3908 values: ["express", "standard"],
3909 },
3910 ],
3911 } as any,
3912 })
3913
3914 const result = await service.computeActions([], {
3915 currency_code: "usd",
3916 customer: {
3917 customer_group: {
3918 id: "VIP",
3919 },
3920 },
3921 shipping_methods: [
3922 {
3923 id: "shipping_method_express",
3924 subtotal: 250,
3925 original_total: 250,
3926 is_discountable: true,
3927 shipping_option: {
3928 id: "express",
3929 },
3930 },
3931 {
3932 id: "shipping_method_standard",
3933 subtotal: 150,
3934 original_total: 150,
3935 is_discountable: true,
3936 shipping_option: {
3937 id: "standard",
3938 },
3939 },
3940 {
3941 id: "shipping_method_snail",
3942 subtotal: 200,
3943 original_total: 200,
3944 is_discountable: true,
3945 shipping_option: {
3946 id: "snail",
3947 },
3948 },
3949 ],
3950 })
3951
3952 expect(JSON.parse(JSON.stringify(result))).toEqual([
3953 {
3954 action: "addShippingMethodAdjustment",
3955 shipping_method_id: "shipping_method_express",
3956 amount: 25,
3957 code: "PROMOTION_TEST",
3958 },
3959 {
3960 action: "addShippingMethodAdjustment",
3961 shipping_method_id: "shipping_method_standard",
3962 amount: 15,
3963 code: "PROMOTION_TEST",
3964 },
3965 ])
3966 })
3967
3968 it("should compute the correct shipping_method amendments when promotion is automatic and prevent_auto_promotions is true", async () => {
3969 await createDefaultPromotion(service, {
3970 rules: [
3971 {
3972 attribute: "customer.customer_group.id",
3973 operator: "in",
3974 values: ["VIP", "top100"],
3975 },
3976 ],
3977 is_automatic: true,
3978 application_method: {
3979 type: ApplicationMethodType.PERCENTAGE,
3980 target_type: "shipping_methods",
3981 allocation: "each",
3982 value: 10,
3983 max_quantity: 2,
3984 target_rules: [
3985 {
3986 attribute: "shipping_methods.shipping_option.id",
3987 operator: "in",
3988 values: ["express", "standard"],
3989 },
3990 ],
3991 } as any,
3992 })
3993
3994 const result = await service.computeActions(
3995 [],
3996 {
3997 currency_code: "usd",
3998 customer: {
3999 customer_group: {
4000 id: "VIP",
4001 },
4002 },
4003 shipping_methods: [
4004 {
4005 id: "shipping_method_express",
4006 subtotal: 250,
4007 original_total: 250,
4008 is_discountable: true,
4009 shipping_option: {
4010 id: "express",
4011 },
4012 },
4013 {
4014 id: "shipping_method_standard",
4015 subtotal: 150,
4016 original_total: 150,
4017 is_discountable: true,
4018 shipping_option: {
4019 id: "standard",
4020 },
4021 },
4022 {
4023 id: "shipping_method_snail",
4024 subtotal: 200,
4025 original_total: 200,
4026 is_discountable: true,
4027 shipping_option: {
4028 id: "snail",
4029 },
4030 },
4031 ],
4032 },
4033 { prevent_auto_promotions: true }
4034 )
4035
4036 expect(JSON.parse(JSON.stringify(result))).toEqual([])
4037 })
4038
4039 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
4040 await createDefaultPromotion(service, {
4041 rules: [
4042 {
4043 attribute: "customer.customer_group.id",
4044 operator: "in",
4045 values: ["VIP", "top100"],
4046 },
4047 ],
4048 application_method: {
4049 type: ApplicationMethodType.PERCENTAGE,
4050 target_type: "shipping_methods",
4051 allocation: "each",
4052 value: 10,
4053 max_quantity: 2,
4054 target_rules: [
4055 {
4056 attribute: "shipping_methods.shipping_option.id",
4057 operator: "in",
4058 values: ["express", "standard"],
4059 },
4060 ],
4061 } as any,
4062 })
4063
4064 await createDefaultPromotion(service, {
4065 code: "PROMOTION_TEST_2",
4066 rules: [
4067 {
4068 attribute: "customer.customer_group.id",
4069 operator: "in",
4070 values: ["VIP", "top100"],
4071 },
4072 ],
4073 is_automatic: true,
4074 application_method: {
4075 type: ApplicationMethodType.PERCENTAGE,
4076 target_type: "shipping_methods",
4077 allocation: "each",
4078 value: 10,
4079 max_quantity: 2,
4080 target_rules: [
4081 {
4082 attribute: "shipping_methods.shipping_option.id",
4083 operator: "in",
4084 values: ["express", "standard"],
4085 },
4086 ],
4087 } as any,
4088 })
4089
4090 const result = await service.computeActions(
4091 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
4092 {
4093 currency_code: "usd",
4094 customer: {
4095 customer_group: {
4096 id: "VIP",
4097 },
4098 },
4099 shipping_methods: [
4100 {
4101 id: "shipping_method_express",
4102 subtotal: 250,
4103 original_total: 250,
4104 is_discountable: true,
4105 shipping_option: {
4106 id: "express",
4107 },
4108 },
4109 {
4110 id: "shipping_method_standard",
4111 subtotal: 150,
4112 original_total: 150,
4113 is_discountable: true,
4114 shipping_option: {
4115 id: "standard",
4116 },
4117 },
4118 {
4119 id: "shipping_method_snail",
4120 subtotal: 200,
4121 original_total: 200,
4122 is_discountable: true,
4123 shipping_option: {
4124 id: "snail",
4125 },
4126 },
4127 ],
4128 }
4129 )
4130
4131 expect(JSON.parse(JSON.stringify(result))).toEqual([
4132 {
4133 action: "addShippingMethodAdjustment",
4134 shipping_method_id: "shipping_method_express",
4135 amount: 25,
4136 code: "PROMOTION_TEST",
4137 },
4138 {
4139 action: "addShippingMethodAdjustment",
4140 shipping_method_id: "shipping_method_standard",
4141 amount: 15,
4142 code: "PROMOTION_TEST",
4143 },
4144 {
4145 action: "addShippingMethodAdjustment",
4146 shipping_method_id: "shipping_method_express",
4147 amount: 22.5,
4148 code: "PROMOTION_TEST_2",
4149 },
4150 {
4151 action: "addShippingMethodAdjustment",
4152 shipping_method_id: "shipping_method_standard",
4153 amount: 13.5,
4154 code: "PROMOTION_TEST_2",
4155 },
4156 ])
4157 })
4158
4159 it("should not compute actions when applicable total is 0", async () => {
4160 await createDefaultPromotion(service, {
4161 rules: [
4162 {
4163 attribute: "customer.customer_group.id",
4164 operator: "in",
4165 values: ["VIP", "top100"],
4166 },
4167 ],
4168 application_method: {
4169 type: ApplicationMethodType.PERCENTAGE,
4170 target_type: "shipping_methods",
4171 allocation: "each",
4172 value: 10,
4173 max_quantity: 2,
4174 target_rules: [
4175 {
4176 attribute: "shipping_methods.shipping_option.id",
4177 operator: "in",
4178 values: ["express", "standard"],
4179 },
4180 ],
4181 } as any,
4182 })
4183
4184 await createDefaultPromotion(service, {
4185 code: "PROMOTION_TEST_2",
4186 rules: [
4187 {
4188 attribute: "customer.customer_group.id",
4189 operator: "in",
4190 values: ["VIP", "top100"],
4191 },
4192 ],
4193 application_method: {
4194 type: ApplicationMethodType.PERCENTAGE,
4195 target_type: "shipping_methods",
4196 allocation: "each",
4197 value: 10,
4198 max_quantity: 2,
4199 target_rules: [
4200 {
4201 attribute: "shipping_methods.shipping_option.id",
4202 operator: "in",
4203 values: ["express", "standard"],
4204 },
4205 ],
4206 } as any,
4207 })
4208
4209 const result = await service.computeActions(
4210 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
4211 {
4212 currency_code: "usd",
4213 customer: {
4214 customer_group: {
4215 id: "VIP",
4216 },
4217 },
4218 shipping_methods: [
4219 {
4220 id: "shipping_method_express",
4221 subtotal: 250,
4222 original_total: 250,
4223 is_discountable: true,
4224 shipping_option: {
4225 id: "express",
4226 },
4227 },
4228 {
4229 id: "shipping_method_standard",
4230 subtotal: 150,
4231 original_total: 150,
4232 is_discountable: true,
4233 shipping_option: {
4234 id: "standard",
4235 },
4236 },
4237 {
4238 id: "shipping_method_snail",
4239 subtotal: 200,
4240 original_total: 200,
4241 is_discountable: true,
4242 shipping_option: {
4243 id: "snail",
4244 },
4245 },
4246 ],
4247 }
4248 )
4249
4250 expect(JSON.parse(JSON.stringify(result))).toEqual([
4251 {
4252 action: "addShippingMethodAdjustment",
4253 shipping_method_id: "shipping_method_express",
4254 amount: 25,
4255 code: "PROMOTION_TEST",
4256 },
4257 {
4258 action: "addShippingMethodAdjustment",
4259 shipping_method_id: "shipping_method_standard",
4260 amount: 15,
4261 code: "PROMOTION_TEST",
4262 },
4263 {
4264 action: "addShippingMethodAdjustment",
4265 shipping_method_id: "shipping_method_express",
4266 amount: 22.5,
4267 code: "PROMOTION_TEST_2",
4268 },
4269 {
4270 action: "addShippingMethodAdjustment",
4271 shipping_method_id: "shipping_method_standard",
4272 amount: 13.5,
4273 code: "PROMOTION_TEST_2",
4274 },
4275 ])
4276 })
4277
4278 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
4279 await createDefaultPromotion(service, {
4280 rules: [
4281 {
4282 attribute: "customer.customer_group.id",
4283 operator: "in",
4284 values: ["VIP", "top100"],
4285 },
4286 ],
4287 application_method: {
4288 type: ApplicationMethodType.PERCENTAGE,
4289 target_type: "shipping_methods",
4290 allocation: "each",
4291 value: 100,
4292 max_quantity: 2,
4293 target_rules: [
4294 {
4295 attribute: "shipping_methods.shipping_option.id",
4296 operator: "in",
4297 values: ["express", "standard"],
4298 },
4299 ],
4300 } as any,
4301 })
4302
4303 const result = await service.computeActions(["PROMOTION_TEST"], {
4304 currency_code: "usd",
4305 customer: {
4306 customer_group: {
4307 id: "VIP",
4308 },
4309 },
4310 shipping_methods: [
4311 {
4312 id: "shipping_method_express",
4313 subtotal: 1200,
4314 original_total: 1200,
4315 is_discountable: true,
4316 shipping_option: {
4317 id: "express",
4318 },
4319 },
4320 ],
4321 })
4322
4323 expect(JSON.parse(JSON.stringify(result))).toEqual([
4324 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
4325 ])
4326 })
4327
4328 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
4329 await createDefaultPromotion(service, {
4330 rules: [
4331 {
4332 attribute: "customer.customer_group.id",
4333 operator: "in",
4334 values: ["VIP", "top100"],
4335 },
4336 ],
4337 campaign_id: "campaign-id-2",
4338 application_method: {
4339 type: ApplicationMethodType.PERCENTAGE,
4340 target_type: "shipping_methods",
4341 allocation: "each",
4342 value: 10,
4343 max_quantity: 2,
4344 target_rules: [
4345 {
4346 attribute: "shipping_methods.shipping_option.id",
4347 operator: "in",
4348 values: ["express", "standard"],
4349 },
4350 ],
4351 } as any,
4352 })
4353
4354 await service.updateCampaigns({
4355 id: "campaign-id-2",
4356 budget: { used: 1000 },
4357 })
4358
4359 const result = await service.computeActions(["PROMOTION_TEST"], {
4360 currency_code: "usd",
4361 customer: {
4362 customer_group: {
4363 id: "VIP",
4364 },
4365 },
4366 shipping_methods: [
4367 {
4368 id: "shipping_method_express",
4369 subtotal: 1200,
4370 original_total: 1200,
4371 is_discountable: true,
4372 shipping_option: {
4373 id: "express",
4374 },
4375 },
4376 ],
4377 })
4378
4379 expect(JSON.parse(JSON.stringify(result))).toEqual([
4380 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
4381 ])
4382 })
4383 })
4384 })
4385
4386 describe("when promotion is for shipping_method and allocation is across", () => {
4387 describe("when application type is fixed", () => {
4388 it("should compute the correct shipping_method amendments", async () => {
4389 await createDefaultPromotion(service, {
4390 rules: [
4391 {
4392 attribute: "customer.customer_group.id",
4393 operator: "in",
4394 values: ["VIP", "top100"],
4395 },
4396 ],
4397 application_method: {
4398 type: ApplicationMethodType.FIXED,
4399 target_type: "shipping_methods",
4400 allocation: "across",
4401 value: 200,
4402 target_rules: [
4403 {
4404 attribute: "shipping_methods.shipping_option.id",
4405 operator: "in",
4406 values: ["express", "standard"],
4407 },
4408 ],
4409 } as any,
4410 })
4411
4412 const result = await service.computeActions(["PROMOTION_TEST"], {
4413 currency_code: "usd",
4414 customer: {
4415 customer_group: {
4416 id: "VIP",
4417 },
4418 },
4419 shipping_methods: [
4420 {
4421 id: "shipping_method_express",
4422 subtotal: 500,
4423 original_total: 500,
4424 is_discountable: true,
4425 shipping_option: {
4426 id: "express",
4427 },
4428 },
4429 {
4430 id: "shipping_method_standard",
4431 subtotal: 100,
4432 original_total: 100,
4433 is_discountable: true,
4434 shipping_option: {
4435 id: "standard",
4436 },
4437 },
4438 {
4439 id: "shipping_method_snail",
4440 subtotal: 200,
4441 original_total: 200,
4442 is_discountable: true,
4443 shipping_option: {
4444 id: "snail",
4445 },
4446 },
4447 ],
4448 })
4449
4450 expect(JSON.parse(JSON.stringify(result))).toEqual([
4451 {
4452 action: "addShippingMethodAdjustment",
4453 shipping_method_id: "shipping_method_express",
4454 amount: 166.66666666666666,
4455 code: "PROMOTION_TEST",
4456 },
4457 {
4458 action: "addShippingMethodAdjustment",
4459 shipping_method_id: "shipping_method_standard",
4460 amount: 33.333333333333336,
4461 code: "PROMOTION_TEST",
4462 },
4463 ])
4464 })
4465
4466 it("should compute the correct shipping_method amendments when promotion is automatic", async () => {
4467 await createDefaultPromotion(service, {
4468 rules: [
4469 {
4470 attribute: "customer.customer_group.id",
4471 operator: "in",
4472 values: ["VIP", "top100"],
4473 },
4474 ],
4475 is_automatic: true,
4476 application_method: {
4477 type: ApplicationMethodType.FIXED,
4478 target_type: "shipping_methods",
4479 allocation: "across",
4480 value: 200,
4481 target_rules: [
4482 {
4483 attribute: "shipping_methods.shipping_option.id",
4484 operator: "in",
4485 values: ["express", "standard"],
4486 },
4487 ],
4488 } as any,
4489 })
4490
4491 const result = await service.computeActions([], {
4492 currency_code: "usd",
4493 customer: {
4494 customer_group: {
4495 id: "VIP",
4496 },
4497 },
4498 shipping_methods: [
4499 {
4500 id: "shipping_method_express",
4501 subtotal: 500,
4502 original_total: 500,
4503 is_discountable: true,
4504 shipping_option: {
4505 id: "express",
4506 },
4507 },
4508 {
4509 id: "shipping_method_standard",
4510 subtotal: 100,
4511 original_total: 100,
4512 is_discountable: true,
4513 shipping_option: {
4514 id: "standard",
4515 },
4516 },
4517 {
4518 id: "shipping_method_snail",
4519 subtotal: 200,
4520 original_total: 200,
4521 is_discountable: true,
4522 shipping_option: {
4523 id: "snail",
4524 },
4525 },
4526 ],
4527 })
4528
4529 expect(JSON.parse(JSON.stringify(result))).toEqual([
4530 {
4531 action: "addShippingMethodAdjustment",
4532 shipping_method_id: "shipping_method_express",
4533 amount: 166.66666666666666,
4534 code: "PROMOTION_TEST",
4535 },
4536 {
4537 action: "addShippingMethodAdjustment",
4538 shipping_method_id: "shipping_method_standard",
4539 amount: 33.333333333333336,
4540 code: "PROMOTION_TEST",
4541 },
4542 ])
4543 })
4544
4545 it("should compute the correct item amendments when there are multiple promotions to apply", async () => {
4546 await createDefaultPromotion(service, {
4547 rules: [
4548 {
4549 attribute: "customer.customer_group.id",
4550 operator: "in",
4551 values: ["VIP", "top100"],
4552 },
4553 ],
4554 application_method: {
4555 type: ApplicationMethodType.FIXED,
4556 target_type: "shipping_methods",
4557 allocation: "across",
4558 value: 200,
4559 target_rules: [
4560 {
4561 attribute: "shipping_methods.shipping_option.id",
4562 operator: "in",
4563 values: ["express", "standard"],
4564 },
4565 ],
4566 } as any,
4567 })
4568
4569 await createDefaultPromotion(service, {
4570 code: "PROMOTION_TEST_2",
4571 rules: [
4572 {
4573 attribute: "customer.customer_group.id",
4574 operator: "in",
4575 values: ["VIP", "top100"],
4576 },
4577 ],
4578 application_method: {
4579 type: ApplicationMethodType.FIXED,
4580 target_type: "shipping_methods",
4581 allocation: "across",
4582 value: 200,
4583 target_rules: [
4584 {
4585 attribute: "shipping_methods.shipping_option.id",
4586 operator: "in",
4587 values: ["express", "standard"],
4588 },
4589 ],
4590 } as any,
4591 })
4592
4593 const result = await service.computeActions(
4594 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
4595 {
4596 currency_code: "usd",
4597 customer: {
4598 customer_group: {
4599 id: "VIP",
4600 },
4601 },
4602 shipping_methods: [
4603 {
4604 id: "shipping_method_express",
4605 subtotal: 500,
4606 original_total: 500,
4607 is_discountable: true,
4608 shipping_option: {
4609 id: "express",
4610 },
4611 },
4612 {
4613 id: "shipping_method_standard",
4614 subtotal: 100,
4615 original_total: 100,
4616 is_discountable: true,
4617 shipping_option: {
4618 id: "standard",
4619 },
4620 },
4621 {
4622 id: "shipping_method_snail",
4623 subtotal: 200,
4624 original_total: 200,
4625 is_discountable: true,
4626 shipping_option: {
4627 id: "snail",
4628 },
4629 },
4630 ],
4631 }
4632 )
4633
4634 expect(JSON.parse(JSON.stringify(result))).toEqual([
4635 {
4636 action: "addShippingMethodAdjustment",
4637 shipping_method_id: "shipping_method_express",
4638 amount: 166.66666666666666,
4639 code: "PROMOTION_TEST",
4640 },
4641 {
4642 action: "addShippingMethodAdjustment",
4643 shipping_method_id: "shipping_method_standard",
4644 amount: 33.333333333333336,
4645 code: "PROMOTION_TEST",
4646 },
4647 {
4648 action: "addShippingMethodAdjustment",
4649 shipping_method_id: "shipping_method_express",
4650 amount: 166.66666666666666,
4651 code: "PROMOTION_TEST_2",
4652 },
4653 {
4654 action: "addShippingMethodAdjustment",
4655 shipping_method_id: "shipping_method_standard",
4656 amount: 33.333333333333336,
4657 code: "PROMOTION_TEST_2",
4658 },
4659 ])
4660 })
4661
4662 it("should not compute actions when applicable total is 0", async () => {
4663 await createDefaultPromotion(service, {
4664 rules: [
4665 {
4666 attribute: "customer.customer_group.id",
4667 operator: "in",
4668 values: ["VIP", "top100"],
4669 },
4670 ],
4671 application_method: {
4672 type: ApplicationMethodType.FIXED,
4673 target_type: "shipping_methods",
4674 allocation: "across",
4675 value: 1000,
4676 target_rules: [
4677 {
4678 attribute: "shipping_methods.shipping_option.id",
4679 operator: "in",
4680 values: ["express", "standard"],
4681 },
4682 ],
4683 } as any,
4684 })
4685
4686 await createDefaultPromotion(service, {
4687 code: "PROMOTION_TEST_2",
4688 rules: [
4689 {
4690 attribute: "customer.customer_group.id",
4691 operator: "in",
4692 values: ["VIP", "top100"],
4693 },
4694 ],
4695 application_method: {
4696 type: ApplicationMethodType.FIXED,
4697 target_type: "shipping_methods",
4698 allocation: "across",
4699 value: 200,
4700 target_rules: [
4701 {
4702 attribute: "shipping_methods.shipping_option.id",
4703 operator: "in",
4704 values: ["express", "standard"],
4705 },
4706 ],
4707 } as any,
4708 })
4709
4710 const result = await service.computeActions(
4711 ["PROMOTION_TEST", "PROMOTION_TEST_2"],
4712 {
4713 currency_code: "usd",
4714 customer: {
4715 customer_group: {
4716 id: "VIP",
4717 },
4718 },
4719 shipping_methods: [
4720 {
4721 id: "shipping_method_express",
4722 subtotal: 500,
4723 original_total: 500,
4724 is_discountable: true,
4725 shipping_option: {
4726 id: "express",
4727 },
4728 },
4729 {
4730 id: "shipping_method_standard",
4731 subtotal: 100,
4732 original_total: 100,
4733 is_discountable: true,
4734 shipping_option: {
4735 id: "standard",
4736 },
4737 },
4738 {
4739 id: "shipping_method_snail",
4740 subtotal: 200,
4741 original_total: 200,
4742 is_discountable: true,
4743 shipping_option: {
4744 id: "snail",
4745 },
4746 },
4747 ],
4748 }
4749 )
4750
4751 expect(JSON.parse(JSON.stringify(result))).toEqual([
4752 {
4753 action: "addShippingMethodAdjustment",
4754 shipping_method_id: "shipping_method_express",
4755 amount: 500,
4756 code: "PROMOTION_TEST",
4757 },
4758 {
4759 action: "addShippingMethodAdjustment",
4760 shipping_method_id: "shipping_method_standard",
4761 amount: 100,
4762 code: "PROMOTION_TEST",
4763 },
4764 ])
4765 })
4766
4767 it("should compute budget exceeded action when applicable total exceeds campaign budget for type spend", async () => {
4768 await createDefaultPromotion(service, {
4769 rules: [
4770 {
4771 attribute: "customer.customer_group.id",
4772 operator: "in",
4773 values: ["VIP", "top100"],
4774 },
4775 ],
4776 application_method: {
4777 type: ApplicationMethodType.FIXED,
4778 target_type: "shipping_methods",
4779 allocation: "across",
4780 value: 1200,
4781 target_rules: [
4782 {
4783 attribute: "shipping_methods.shipping_option.id",
4784 operator: "in",
4785 values: ["express", "standard"],
4786 },
4787 ],
4788 } as any,
4789 })
4790
4791 const result = await service.computeActions(["PROMOTION_TEST"], {
4792 currency_code: "usd",
4793 customer: {
4794 customer_group: {
4795 id: "VIP",
4796 },
4797 },
4798 shipping_methods: [
4799 {
4800 id: "shipping_method_express",
4801 subtotal: 1200,
4802 original_total: 1200,
4803 is_discountable: true,
4804 shipping_option: {
4805 id: "express",
4806 },
4807 },
4808 ],
4809 })
4810
4811 expect(JSON.parse(JSON.stringify(result))).toEqual([
4812 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
4813 ])
4814 })
4815
4816 it("should compute budget exceeded action when applicable total exceeds campaign budget for type usage", async () => {
4817 await createDefaultPromotion(service, {
4818 rules: [
4819 {
4820 attribute: "customer.customer_group.id",
4821 operator: "in",
4822 values: ["VIP", "top100"],
4823 },
4824 ],
4825 campaign_id: "campaign-id-2",
4826 application_method: {
4827 type: ApplicationMethodType.FIXED,
4828 target_type: "shipping_methods",
4829 allocation: "across",
4830 value: 1200,
4831 target_rules: [
4832 {
4833 attribute: "shipping_methods.shipping_option.id",
4834 operator: "in",
4835 values: ["express", "standard"],
4836 },
4837 ],
4838 } as any,
4839 })
4840
4841 await service.updateCampaigns({
4842 id: "campaign-id-2",
4843 budget: { used: 1000 },
4844 })
4845
4846 const result = await service.computeActions(["PROMOTION_TEST"], {
4847 currency_code: "usd",
4848 customer: {
4849 customer_group: {
4850 id: "VIP",
4851 },
4852 },
4853 shipping_methods: [
4854 {
4855 id: "shipping_method_express",
4856 subtotal: 1200,
4857 original_total: 1200,
4858 is_discountable: true,
4859 shipping_option: {
4860 id: "express",
4861 },
4862 },
4863 ],
4864 })
4865
4866 expect(JSON.parse(JSON.stringify(result))).toEqual([
4867 { action: "campaignBudgetExceeded", code: "PROMOTION_TEST" },
4868 ])
4869 })
4870 })
4871
4872 describe("when application type is percentage", () => {
4873 it("should compute the correct shipping_method amendments", async () => {
4874 await createDefaultPromotion(service, {
4875 rules: [
4876 {
4877 attribute: "customer.customer_group.id",
4878 operator: "in",
4879 values: ["VIP", "top100"],
4880 },
4881 ],
4882 application_method: {
4883 type: ApplicationMethodType.PERCENTAGE,
4884 target_type: "shipping_methods",
4885 allocation: "across",
4886 value: 10,
4887 target_rules: [
4888 {
4889 attribute: "shipping_methods.shipping_option.id",
4890 operator: "in",
4891 values: ["express", "standard"],
4892 },
4893 ],
4894 } as any,
4895 })
4896
4897 const result = await service.computeActions(["PROMOTION_TEST"], {
4898 currency_code: "usd",
4899 customer: {
4900 customer_group: {
4901 id: "VIP",
4902 },
4903 },
4904 shipping_methods: [
4905 {
4906 id: "shipping_method_express",
4907 subtotal: 500,
4908 original_total: 500,
4909 is_discountable: true,
4910 shipping_option: {
4911 id: "express",
4912 },
4913 },
4914 {
4915 id: "shipping_method_standard",
4916 subtotal: 100,
4917 original_total: 100,
4918 is_discountable: true,
4919 shipping_option: {
4920 id: "standard",
4921 },
4922 },
4923 {
4924 id: "shipping_method_snail",
4925 subtotal: 200,
4926 original_total: 200,
4927 is_discountable: true,
4928 shipping_option: {
4929 id: "snail",
4930 },
4931 },
4932 ],
4933 })
4934
4935 expect(JSON.parse(JSON.stringify(result))).toEqual([
4936 {
4937 action: "addShippingMethodAdjustment",
4938 shipping_method_id: "shipping_method_express",
4939 amount: 50,
4940 code: "PROMOTION_TEST",
4941 },
4942 {
4943 action: "addShippingMethodAdjustment",
4944 shipping_method_id: "shipping_method_standard",
4945 amount: 10,
4946 code: "PROMOTION_TEST",
4947 },
4948 ])
4949 })
4950
4951 it("should compute the correct shipping_method amendments when promotion is automatic", async () => {
4952 await createDefaultPromotion(service, {
4953 rules: [
4954 {
4955 attribute: "customer.customer_group.id",
4956 operator: "in",
4957 values: ["VIP", "top100"],
4958 },
4959 ],
4960 is_automatic: true,
4961 application_method: {
4962 type: ApplicationMethodType.PERCENTAGE,
4963 target_type: "shipping_methods",
4964 allocation: "across",
4965 value: 10,
4966 target_rules: [
4967 {
4968 attribute: "shipping_methods.shipping_option.id",
4969 operator: "in",
4970 values: ["express", "standard"],
4971 },
4972 ],
4973 } as any,
4974 })
4975
4976 const result = await service.computeActions([], {
4977 currency_code: "usd",
4978 customer: {
4979 customer_group: {
4980 id: "VIP",
4981 },
4982 },
4983 shipping_methods: [
4984 {
4985 id: "shipping_method_express",
4986 subtotal: 500,
4987 original_total: 500,
4988 is_discountable: true,
4989 shipping_option: {
4990 id: "express",
4991 },
4992 },
4993 {
4994 id: "shipping_method_standard",
4995 subtotal: 100,
4996 original_total: 100,
4997 is_discountable: true,
4998 shipping_option: {
4999 id: "standard",
5000 },
Showing first 5,000 of 8,123 lines. View raw