1 import { IPromotionModuleService } from "@medusajs/framework/types"
2 import {
3 ApplicationMethodType,
4 CampaignBudgetType,
5 Module,
6 Modules,
7 PromotionType,
8 } from "@medusajs/framework/utils"
9 import { SuiteOptions, moduleIntegrationTestRunner } from "@medusajs/test-utils"
10 import { PromotionModuleService } from "@services"
11 import { createCampaigns } from "../../../__fixtures__/campaigns"
12 import {
13 createDefaultPromotion,
14 createDefaultPromotions,
15 createPromotions,
16 } from "../../../__fixtures__/promotion"
17
18 jest.setTimeout(30000)
19
20 moduleIntegrationTestRunner({
21 moduleName: Modules.PROMOTION,
22 testSuite: ({
23 MikroOrmWrapper,
24 service,
25 }: SuiteOptions<IPromotionModuleService>) => {
26 describe("Promotion Service", () => {
27 beforeEach(async () => {
28 await createCampaigns(MikroOrmWrapper.forkManager())
29 })
30
31 it(`should export the appropriate linkable configuration`, () => {
32 const linkable = Module(Modules.PROMOTION, {
33 service: PromotionModuleService,
34 }).linkable
35
36 expect(Object.keys(linkable)).toEqual([
37 "promotion",
38 "applicationMethod",
39 "campaign",
40 "campaignBudget",
41 "campaignBudgetUsage",
42 "promotionRule",
43 "promotionRuleValue",
44 ])
45
46 Object.keys(linkable).forEach((key) => {
47 delete linkable[key].toJSON
48 })
49
50 expect(linkable).toEqual({
51 promotion: {
52 id: {
53 linkable: "promotion_id",
54 entity: "Promotion",
55 primaryKey: "id",
56 serviceName: "promotion",
57 field: "promotion",
58 },
59 },
60 applicationMethod: {
61 id: {
62 linkable: "application_method_id",
63 entity: "ApplicationMethod",
64 primaryKey: "id",
65 serviceName: "promotion",
66 field: "applicationMethod",
67 },
68 },
69 campaign: {
70 id: {
71 linkable: "campaign_id",
72 entity: "Campaign",
73 primaryKey: "id",
74 serviceName: "promotion",
75 field: "campaign",
76 },
77 },
78 campaignBudget: {
79 id: {
80 linkable: "campaign_budget_id",
81 entity: "CampaignBudget",
82 primaryKey: "id",
83 serviceName: "promotion",
84 field: "campaignBudget",
85 },
86 },
87 campaignBudgetUsage: {
88 id: {
89 linkable: "campaign_budget_usage_id",
90 entity: "CampaignBudgetUsage",
91 primaryKey: "id",
92 serviceName: "promotion",
93 field: "campaignBudgetUsage",
94 },
95 },
96 promotionRule: {
97 id: {
98 linkable: "promotion_rule_id",
99 entity: "PromotionRule",
100 primaryKey: "id",
101 serviceName: "promotion",
102 field: "promotionRule",
103 },
104 },
105 promotionRuleValue: {
106 id: {
107 linkable: "promotion_rule_value_id",
108 entity: "PromotionRuleValue",
109 primaryKey: "id",
110 serviceName: "promotion",
111 field: "promotionRuleValue",
112 },
113 },
114 })
115 })
116
117 describe("create", () => {
118 it("should throw an error when required params are not passed", async () => {
119 const error = await createDefaultPromotion(service, {
120 code: undefined,
121 }).catch((e) => e)
122
123 expect(error.message).toContain(
124 "Value for Promotion.code is required, 'undefined' found"
125 )
126 })
127
128 it("should create a basic promotion successfully", async () => {
129 const createdPromotion = await createDefaultPromotion(service, {})
130
131 const [promotion] = await service.listPromotions({
132 id: [createdPromotion.id],
133 })
134
135 expect(promotion).toEqual(
136 expect.objectContaining({
137 code: "PROMOTION_TEST",
138 is_automatic: false,
139 type: "standard",
140 })
141 )
142 })
143
144 it("should throw error when percentage type and value is greater than 100", async () => {
145 const error = await createDefaultPromotion(service, {
146 type: PromotionType.STANDARD,
147 application_method: {
148 type: ApplicationMethodType.PERCENTAGE,
149 value: 1000,
150 } as any,
151 }).catch((e) => e)
152
153 expect(error.message).toContain(
154 "Application Method value should be a percentage number between 0 and 100"
155 )
156 })
157
158 it("should throw an error when both campaign and campaign_id are provided", async () => {
159 const startsAt = new Date("01/01/2023")
160 const endsAt = new Date("01/01/2023")
161
162 const error = await createDefaultPromotion(service, {
163 campaign_id: "campaign-id-1",
164 campaign: {
165 name: "test",
166 campaign_identifier: "test-promotion-test",
167 starts_at: startsAt,
168 ends_at: endsAt,
169 budget: {
170 type: CampaignBudgetType.SPEND,
171 used: 100,
172 limit: 100,
173 },
174 },
175 }).catch((e) => e)
176
177 expect(error.message).toContain(
178 "Provide either the 'campaign' or 'campaign_id' parameter; both cannot be used simultaneously."
179 )
180 })
181
182 it("should create a basic promotion with campaign successfully", async () => {
183 const startsAt = new Date("01/01/2023")
184 const endsAt = new Date("01/01/2023")
185
186 const createdPromotion = await createDefaultPromotion(service, {
187 code: "PROMOTION_TEST",
188 type: PromotionType.STANDARD,
189 campaign_id: undefined,
190 campaign: {
191 name: "test",
192 campaign_identifier: "test-promotion-test",
193 starts_at: startsAt,
194 ends_at: endsAt,
195 budget: {
196 type: CampaignBudgetType.SPEND,
197 currency_code: "usd",
198 used: 100,
199 limit: 100,
200 },
201 },
202 })
203
204 const [promotion] = await service.listPromotions(
205 { id: [createdPromotion.id] },
206 { relations: ["campaign.budget"] }
207 )
208
209 expect(promotion).toEqual(
210 expect.objectContaining({
211 code: "PROMOTION_TEST",
212 is_automatic: false,
213 type: "standard",
214 application_method: expect.any(Object),
215 campaign: expect.objectContaining({
216 name: "test",
217 campaign_identifier: "test-promotion-test",
218 starts_at: startsAt,
219 ends_at: endsAt,
220 budget: expect.objectContaining({
221 type: CampaignBudgetType.SPEND,
222 currency_code: "usd",
223 used: 100,
224 limit: 100,
225 }),
226 }),
227 })
228 )
229 })
230
231 it("should create a basic promotion with an existing campaign successfully", async () => {
232 const createdPromotion = await createDefaultPromotion(service, {
233 campaign_id: "campaign-id-1",
234 code: "PROMOTION_TEST",
235 })
236
237 const [promotion] = await service.listPromotions(
238 { id: [createdPromotion.id] },
239 { relations: ["campaign.budget"] }
240 )
241
242 expect(promotion).toEqual(
243 expect.objectContaining({
244 code: "PROMOTION_TEST",
245 is_automatic: false,
246 type: "standard",
247 campaign: expect.objectContaining({
248 id: "campaign-id-1",
249 budget: expect.objectContaining({
250 type: CampaignBudgetType.SPEND,
251 limit: 1000,
252 used: 0,
253 }),
254 }),
255 })
256 )
257 })
258
259 it("should throw error when creating an item application method without allocation", async () => {
260 const error = await createDefaultPromotion(service, {
261 type: PromotionType.STANDARD,
262 application_method: {
263 allocation: undefined,
264 target_type: "items",
265 } as any,
266 }).catch((e) => e)
267
268 expect(error.message).toContain(
269 "application_method.allocation should be either 'across OR each OR once' when application_method.target_type is either 'shipping_methods OR items'"
270 )
271 })
272
273 it("should throw error when creating an item application, each allocation, without max quanity", async () => {
274 const error = await createDefaultPromotion(service, {
275 application_method: {
276 allocation: "each",
277 max_quantity: undefined,
278 } as any,
279 }).catch((e) => e)
280
281 expect(error.message).toContain(
282 "application_method.max_quantity is required when application_method.allocation is 'each OR once'"
283 )
284 })
285
286 it("should throw error when creating an order application method with rules", async () => {
287 const error = await createDefaultPromotion(service, {
288 application_method: {
289 target_type: "order",
290 target_rules: [
291 {
292 attribute: "product_id",
293 operator: "eq",
294 values: ["prod_tshirt"],
295 },
296 ],
297 } as any,
298 }).catch((e) => e)
299
300 expect(error.message).toContain(
301 "Target rules for application method with target type (order) is not allowed"
302 )
303 })
304
305 it("should create a promotion with rules successfully", async () => {
306 const createdPromotion = await createDefaultPromotion(service, {
307 rules: [
308 {
309 attribute: "customer.customer_group.id",
310 operator: "in",
311 values: ["VIP", "top100"],
312 },
313 ],
314 })
315
316 const [promotion] = await service.listPromotions(
317 { id: [createdPromotion.id] },
318 { relations: ["rules", "rules.values"] }
319 )
320
321 expect(promotion).toEqual(
322 expect.objectContaining({
323 code: "PROMOTION_TEST",
324 is_automatic: false,
325 type: "standard",
326 rules: [
327 expect.objectContaining({
328 attribute: "customer.customer_group.id",
329 operator: "in",
330 values: expect.arrayContaining([
331 expect.objectContaining({
332 value: "VIP",
333 }),
334 expect.objectContaining({
335 value: "top100",
336 }),
337 ]),
338 }),
339 ],
340 })
341 )
342 })
343
344 it("should create a promotion with rules with single value successfully", async () => {
345 const createdPromotion = await createDefaultPromotion(service, {
346 rules: [
347 {
348 attribute: "customer.customer_group.id",
349 operator: "eq",
350 values: "VIP",
351 },
352 ],
353 })
354
355 const [promotion] = await service.listPromotions(
356 { id: [createdPromotion.id] },
357 { relations: ["rules", "rules.values"] }
358 )
359
360 expect(promotion).toEqual(
361 expect.objectContaining({
362 code: "PROMOTION_TEST",
363 is_automatic: false,
364 type: "standard",
365 rules: [
366 expect.objectContaining({
367 attribute: "customer.customer_group.id",
368 operator: "eq",
369 values: expect.arrayContaining([
370 expect.objectContaining({
371 value: "VIP",
372 }),
373 ]),
374 }),
375 ],
376 })
377 )
378 })
379
380 it("should throw an error when rule attribute is invalid", async () => {
381 const error = await createDefaultPromotion(service, {
382 rules: [
383 {
384 attribute: "",
385 operator: "eq",
386 values: "VIP",
387 } as any,
388 ],
389 }).catch((e) => e)
390
391 expect(error.message).toContain(
392 "rules[].attribute is a required field"
393 )
394 })
395
396 it("should throw an error when rule operator is invalid", async () => {
397 let error = await createDefaultPromotion(service, {
398 rules: [
399 {
400 attribute: "customer_group",
401 operator: "",
402 values: "VIP",
403 } as any,
404 ],
405 }).catch((e) => e)
406
407 expect(error.message).toContain(
408 "rules[].operator is a required field"
409 )
410
411 error = await createDefaultPromotion(service, {
412 rules: [
413 {
414 attribute: "customer_group",
415 operator: "doesnotexist",
416 values: "VIP",
417 } as any,
418 ],
419 }).catch((e) => e)
420
421 expect(error.message).toContain(
422 "rules[].operator (doesnotexist) is invalid. It should be one of gte, lte, gt, lt, eq, ne, in"
423 )
424 })
425
426 it("should create a basic buyget promotion successfully", async () => {
427 const createdPromotion = await createDefaultPromotion(service, {
428 type: PromotionType.BUYGET,
429 application_method: {
430 apply_to_quantity: 1,
431 max_quantity: 1,
432 allocation: "each",
433 buy_rules_min_quantity: 1,
434 buy_rules: [
435 {
436 attribute: "product_collection",
437 operator: "eq",
438 values: ["pcol_towel"],
439 },
440 ],
441 target_rules: [
442 {
443 attribute: "product_collection",
444 operator: "eq",
445 values: ["pcol_towel"],
446 },
447 ],
448 } as any,
449 })
450
451 const [promotion] = await service.listPromotions({
452 id: [createdPromotion.id],
453 })
454
455 expect(promotion).toEqual(
456 expect.objectContaining({
457 code: "PROMOTION_TEST",
458 is_automatic: false,
459 type: PromotionType.BUYGET,
460 })
461 )
462 })
463
464 it("should throw an error when target_rules are not present for buyget promotion", async () => {
465 const error = await createDefaultPromotion(service, {
466 type: PromotionType.BUYGET,
467 application_method: {
468 apply_to_quantity: 1,
469 max_quantity: 1,
470 allocation: "each",
471 buy_rules_min_quantity: 1,
472 buy_rules: [
473 {
474 attribute: "product_collection",
475 operator: "eq",
476 values: ["pcol_towel"],
477 },
478 ],
479 } as any,
480 }).catch((e) => e)
481
482 expect(error.message).toContain(
483 "Target rules are required for buyget promotion type"
484 )
485 })
486
487 it("should throw an error when buy_rules are not present for buyget promotion", async () => {
488 const error = await createDefaultPromotion(service, {
489 type: PromotionType.BUYGET,
490 application_method: {
491 apply_to_quantity: 1,
492 max_quantity: 1,
493 allocation: "each",
494 buy_rules_min_quantity: 1,
495 } as any,
496 }).catch((e) => e)
497
498 expect(error.message).toContain(
499 "Buy rules are required for buyget promotion type"
500 )
501 })
502
503 it("should throw an error when apply_to_quantity is not present for buyget promotion", async () => {
504 const error = await createDefaultPromotion(service, {
505 type: PromotionType.BUYGET,
506 application_method: {
507 max_quantity: 1,
508 allocation: "each",
509 buy_rules_min_quantity: 1,
510 buy_rules: [
511 {
512 attribute: "items.product_collection.id",
513 operator: "eq",
514 values: ["pcol_towel"],
515 },
516 ],
517 target_rules: [
518 {
519 attribute: "items.product.id",
520 operator: "eq",
521 values: ["prod_mat"],
522 },
523 ],
524 } as any,
525 }).catch((e) => e)
526
527 expect(error.message).toContain(
528 "apply_to_quantity is a required field for Promotion type of buyget"
529 )
530 })
531
532 it("should throw an error when buy_rules_min_quantity is not present for buyget promotion", async () => {
533 const error = await createDefaultPromotion(service, {
534 type: PromotionType.BUYGET,
535 application_method: {
536 apply_to_quantity: 1,
537 max_quantity: 1,
538 allocation: "each",
539 buy_rules: [
540 {
541 attribute: "items.product_collection.id",
542 operator: "eq",
543 values: ["pcol_towel"],
544 },
545 ],
546 target_rules: [
547 {
548 attribute: "items.product.id",
549 operator: "eq",
550 values: ["prod_mat"],
551 },
552 ],
553 } as any,
554 }).catch((e) => e)
555
556 expect(error.message).toContain(
557 "buy_rules_min_quantity is a required field for Promotion type of buyget"
558 )
559 })
560
561 it("should create a budget promotion with rules successfully", async () => {
562 const createdPromotion = await createDefaultPromotion(service, {
563 type: PromotionType.BUYGET,
564 application_method: {
565 apply_to_quantity: 1,
566 max_quantity: 1,
567 allocation: "each",
568 buy_rules_min_quantity: 1,
569 buy_rules: [
570 {
571 attribute: "items.product_collection.id",
572 operator: "eq",
573 values: ["pcol_towel"],
574 },
575 ],
576 target_rules: [
577 {
578 attribute: "items.product.id",
579 operator: "eq",
580 values: "prod_mat",
581 },
582 ],
583 } as any,
584 })
585
586 expect(createdPromotion).toEqual(
587 expect.objectContaining({
588 type: PromotionType.BUYGET,
589 application_method: expect.objectContaining({
590 apply_to_quantity: 1,
591 buy_rules_min_quantity: 1,
592 target_rules: [
593 expect.objectContaining({
594 attribute: "items.product.id",
595 operator: "eq",
596 values: [expect.objectContaining({ value: "prod_mat" })],
597 }),
598 ],
599 buy_rules: [
600 expect.objectContaining({
601 attribute: "items.product_collection.id",
602 operator: "eq",
603 values: [expect.objectContaining({ value: "pcol_towel" })],
604 }),
605 ],
606 }),
607 })
608 )
609 })
610 })
611
612 describe("update", () => {
613 it("should throw an error when required params are not passed", async () => {
614 const error = await service
615 .updatePromotions([
616 {
617 type: PromotionType.STANDARD,
618 } as any,
619 ])
620 .catch((e) => e)
621
622 expect(error.message).toContain('Promotion with id "" not found')
623 })
624
625 it("should update the attributes of a promotion successfully", async () => {
626 await createDefaultPromotions(service)
627
628 const [updatedPromotion] = await service.updatePromotions([
629 {
630 id: "promotion-id-1",
631 is_automatic: true,
632 code: "TEST",
633 type: PromotionType.BUYGET,
634 } as any,
635 ])
636
637 expect(updatedPromotion).toEqual(
638 expect.objectContaining({
639 is_automatic: true,
640 code: "TEST",
641 type: PromotionType.BUYGET,
642 })
643 )
644 })
645
646 it("should update the attributes of a application method successfully", async () => {
647 const createdPromotion = await createDefaultPromotion(service, {
648 application_method: { value: 100 },
649 } as any)
650 const applicationMethod = createdPromotion.application_method
651
652 const [updatedPromotion] = await service.updatePromotions([
653 {
654 id: createdPromotion.id,
655 application_method: {
656 id: applicationMethod?.id!,
657 value: 200,
658 },
659 },
660 ])
661
662 expect(updatedPromotion).toEqual(
663 expect.objectContaining({
664 application_method: expect.objectContaining({
665 value: 200,
666 }),
667 })
668 )
669 })
670
671 it("should change max_quantity to 0 when target_type is changed to order", async () => {
672 const createdPromotion = await createDefaultPromotion(service, {
673 application_method: { max_quantity: 500, allocation: "each" },
674 } as any)
675 const applicationMethod = createdPromotion.application_method
676
677 const [updatedPromotion] = await service.updatePromotions([
678 {
679 id: createdPromotion.id,
680 application_method: {
681 id: applicationMethod?.id as string,
682 target_type: "order",
683 allocation: "across",
684 },
685 },
686 ])
687
688 expect(updatedPromotion).toEqual(
689 expect.objectContaining({
690 application_method: expect.objectContaining({
691 target_type: "order",
692 allocation: "across",
693 max_quantity: null,
694 }),
695 })
696 )
697 })
698
699 it("should validate the attributes of a application method successfully", async () => {
700 const createdPromotion = await createDefaultPromotion(service, {
701 application_method: {
702 type: "fixed",
703 target_type: "order",
704 allocation: "across",
705 },
706 } as any)
707
708 const applicationMethod = createdPromotion.application_method
709
710 let error = await service
711 .updatePromotions([
712 {
713 id: createdPromotion.id,
714 application_method: {
715 id: applicationMethod?.id!,
716 target_type: "should-error",
717 } as any,
718 },
719 ])
720 .catch((e) => e)
721
722 expect(error.message).toContain(
723 `application_method.target_type should be one of order, shipping_methods, items`
724 )
725
726 error = await service
727 .updatePromotions([
728 {
729 id: createdPromotion.id,
730 application_method: {
731 id: applicationMethod?.id as string,
732 allocation: "should-error",
733 } as any,
734 },
735 ])
736 .catch((e) => e)
737
738 expect(error.message).toContain(
739 `application_method.allocation should be one of each, across`
740 )
741
742 error = await service
743 .updatePromotions([
744 {
745 id: createdPromotion.id,
746 application_method: {
747 id: applicationMethod?.id as string,
748 type: "should-error",
749 } as any,
750 },
751 ])
752 .catch((e) => e)
753
754 expect(error.message).toContain(
755 `application_method.type should be one of fixed, percentage`
756 )
757 })
758
759 it("should update campaign of the promotion", async () => {
760 const createdPromotion = await createDefaultPromotion(service, {
761 campaign_id: "campaign-id-1",
762 })
763
764 const [updatedPromotion] = await service.updatePromotions([
765 {
766 id: createdPromotion.id,
767 campaign_id: "campaign-id-2",
768 },
769 ])
770
771 expect(updatedPromotion).toEqual(
772 expect.objectContaining({
773 campaign: expect.objectContaining({
774 id: "campaign-id-2",
775 }),
776 })
777 )
778 })
779 })
780
781 describe("retrieve", () => {
782 beforeEach(async () => {
783 await createDefaultPromotions(service)
784 })
785
786 const id = "promotion-id-1"
787
788 it("should return promotion for the given id", async () => {
789 const promotion = await service.retrievePromotion(id)
790
791 expect(promotion).toEqual(
792 expect.objectContaining({
793 id,
794 })
795 )
796 })
797
798 it("should throw an error when promotion with id does not exist", async () => {
799 let error
800
801 try {
802 await service.retrievePromotion("does-not-exist")
803 } catch (e) {
804 error = e
805 }
806
807 expect(error.message).toEqual(
808 "Promotion with id: does-not-exist was not found"
809 )
810 })
811
812 it("should throw an error when a id is not provided", async () => {
813 let error
814
815 try {
816 await service.retrievePromotion(undefined as unknown as string)
817 } catch (e) {
818 error = e
819 }
820
821 expect(error.message).toEqual("promotion - id must be defined")
822 })
823
824 it("should return promotion based on config select param", async () => {
825 const promotion = await service.retrievePromotion(id, {
826 select: ["id"],
827 })
828
829 const serialized = JSON.parse(JSON.stringify(promotion))
830
831 expect(serialized).toEqual({
832 id,
833 })
834 })
835 })
836
837 describe("listAndCount", () => {
838 beforeEach(async () => {
839 await createPromotions(MikroOrmWrapper.forkManager(), [
840 {
841 id: "promotion-id-1",
842 code: "PROMOTION_1",
843 type: PromotionType.STANDARD,
844 application_method: {
845 type: ApplicationMethodType.FIXED,
846 value: "200",
847 target_type: "items",
848 currency_code: "usd",
849 },
850 } as any,
851 {
852 id: "promotion-id-2",
853 code: "PROMOTION_2",
854 type: PromotionType.STANDARD,
855 } as any,
856 ])
857 })
858
859 it("should return all promotions and count", async () => {
860 const [promotions, count] = await service.listAndCountPromotions()
861
862 expect(count).toEqual(2)
863 expect(promotions).toEqual([
864 expect.objectContaining({
865 id: "promotion-id-1",
866 code: "PROMOTION_1",
867 campaign_id: null,
868 campaign: null,
869 status: "draft",
870 is_automatic: false,
871 type: "standard",
872 application_method: expect.any(Object),
873 created_at: expect.any(Date),
874 updated_at: expect.any(Date),
875 deleted_at: null,
876 }),
877 expect.objectContaining({
878 id: "promotion-id-2",
879 code: "PROMOTION_2",
880 campaign_id: null,
881 campaign: null,
882 status: "draft",
883 is_automatic: false,
884 type: "standard",
885 application_method: null,
886 created_at: expect.any(Date),
887 updated_at: expect.any(Date),
888 deleted_at: null,
889 }),
890 ])
891 })
892
893 it("should return all promotions based on config select and relations param", async () => {
894 const [promotions, count] = await service.listAndCountPromotions(
895 {
896 id: ["promotion-id-1"],
897 },
898 {
899 relations: ["application_method"],
900 select: ["code", "application_method.type"],
901 }
902 )
903
904 expect(count).toEqual(1)
905 expect(promotions).toEqual([
906 {
907 id: "promotion-id-1",
908 code: "PROMOTION_1",
909 application_method: {
910 id: expect.any(String),
911 type: "fixed",
912 },
913 },
914 ])
915 })
916 })
917
918 describe("delete", () => {
919 it("should soft delete the promotions given an id successfully", async () => {
920 const createdPromotion = await createDefaultPromotion(service, {
921 code: "TEST",
922 type: "standard",
923 })
924
925 await service.deletePromotions([createdPromotion.id])
926
927 const promotions = await service.listPromotions(
928 {
929 id: [createdPromotion.id],
930 },
931 { withDeleted: true }
932 )
933
934 expect(promotions).toHaveLength(0)
935 })
936 })
937
938 describe("softDelete", () => {
939 it("should soft delete the promotions given an id successfully", async () => {
940 const createdPromotion = await createDefaultPromotion(service, {
941 code: "TEST",
942 type: "standard",
943 })
944
945 await service.softDeletePromotions([createdPromotion.id])
946
947 const promotions = await service.listPromotions({
948 id: [createdPromotion.id],
949 })
950
951 expect(promotions).toHaveLength(0)
952 })
953 })
954
955 describe("restore", () => {
956 it("should restore the promotions given an id successfully", async () => {
957 const createdPromotion = await createDefaultPromotion(service, {
958 code: "TEST",
959 type: "standard",
960 })
961
962 await service.softDeletePromotions([createdPromotion.id])
963
964 let promotions = await service.listPromotions({
965 id: [createdPromotion.id],
966 })
967
968 expect(promotions).toHaveLength(0)
969 await service.restorePromotions([createdPromotion.id])
970
971 promotions = await service.listPromotions({
972 id: [createdPromotion.id],
973 })
974 expect(promotions).toHaveLength(1)
975 })
976 })
977
978 describe("addPromotionRules", () => {
979 let promotion
980
981 beforeEach(async () => {
982 promotion = await createDefaultPromotion(service, {
983 code: "TEST",
984 application_method: {
985 type: "fixed",
986 target_type: "items",
987 allocation: "each",
988 value: 100,
989 max_quantity: 500,
990 } as any,
991 })
992 })
993
994 it("should throw an error when promotion with id does not exist", async () => {
995 let error
996
997 try {
998 await service.addPromotionRules("does-not-exist", [])
999 } catch (e) {
1000 error = e
1001 }
1002
1003 expect(error.message).toEqual(
1004 "Promotion with id: does-not-exist was not found"
1005 )
1006 })
1007
1008 it("should throw an error when a id is not provided", async () => {
1009 let error
1010
1011 try {
1012 await service.addPromotionRules(undefined as unknown as string, [])
1013 } catch (e) {
1014 error = e
1015 }
1016
1017 expect(error.message).toEqual("promotion - id must be defined")
1018 })
1019
1020 it("should successfully add rules to a promotion", async () => {
1021 const promotionRules = await service.addPromotionRules(promotion.id, [
1022 {
1023 attribute: "customer.customer_group.id",
1024 operator: "in",
1025 values: ["VIP", "top100"],
1026 },
1027 ])
1028
1029 expect(promotionRules).toEqual([
1030 expect.objectContaining({
1031 id: promotionRules[0].id,
1032 attribute: "customer.customer_group.id",
1033 operator: "in",
1034 values: expect.arrayContaining([
1035 expect.objectContaining({ value: "VIP" }),
1036 expect.objectContaining({ value: "top100" }),
1037 ]),
1038 }),
1039 ])
1040 })
1041 })
1042
1043 describe("addPromotionTargetRules", () => {
1044 let promotion
1045
1046 beforeEach(async () => {
1047 promotion = await createDefaultPromotion(service, {})
1048 })
1049
1050 it("should throw an error when promotion with id does not exist", async () => {
1051 let error
1052
1053 try {
1054 await service.addPromotionTargetRules("does-not-exist", [])
1055 } catch (e) {
1056 error = e
1057 }
1058
1059 expect(error.message).toEqual(
1060 "Promotion with id: does-not-exist was not found"
1061 )
1062 })
1063
1064 it("should throw an error when a id is not provided", async () => {
1065 let error
1066
1067 try {
1068 await service.addPromotionTargetRules(
1069 undefined as unknown as string,
1070 []
1071 )
1072 } catch (e) {
1073 error = e
1074 }
1075
1076 expect(error.message).toEqual("promotion - id must be defined")
1077 })
1078
1079 it("should successfully create target rules for a promotion", async () => {
1080 const promotionRules = await service.addPromotionTargetRules(
1081 promotion.id,
1082 [
1083 {
1084 attribute: "customer.customer_group.id",
1085 operator: "in",
1086 values: ["VIP", "top100"],
1087 },
1088 ]
1089 )
1090
1091 expect(promotionRules).toEqual([
1092 expect.objectContaining({
1093 id: promotionRules[0].id,
1094 attribute: "customer.customer_group.id",
1095 operator: "in",
1096 values: expect.arrayContaining([
1097 expect.objectContaining({ value: "VIP" }),
1098 expect.objectContaining({ value: "top100" }),
1099 ]),
1100 }),
1101 ])
1102 })
1103 })
1104
1105 describe("addPromotionBuyRules", () => {
1106 let promotion
1107
1108 beforeEach(async () => {
1109 promotion = await createDefaultPromotion(service, {
1110 type: PromotionType.BUYGET,
1111 application_method: {
1112 apply_to_quantity: 1,
1113 max_quantity: 1,
1114 allocation: "each",
1115 buy_rules_min_quantity: 1,
1116 buy_rules: [
1117 {
1118 attribute: "items.product_collection.id",
1119 operator: "eq",
1120 values: ["pcol_towel"],
1121 },
1122 ],
1123 target_rules: [
1124 {
1125 attribute: "items.product.id",
1126 operator: "in",
1127 values: ["prod_1", "prod_2"],
1128 },
1129 ],
1130 } as any,
1131 })
1132 })
1133
1134 it("should throw an error when promotion with id does not exist", async () => {
1135 let error
1136
1137 try {
1138 await service.addPromotionBuyRules("does-not-exist", [])
1139 } catch (e) {
1140 error = e
1141 }
1142
1143 expect(error.message).toEqual(
1144 "Promotion with id: does-not-exist was not found"
1145 )
1146 })
1147
1148 it("should throw an error when a id is not provided", async () => {
1149 let error
1150
1151 try {
1152 await service.addPromotionBuyRules(
1153 undefined as unknown as string,
1154 []
1155 )
1156 } catch (e) {
1157 error = e
1158 }
1159
1160 expect(error.message).toEqual("promotion - id must be defined")
1161 })
1162
1163 it("should successfully create buy rules for a buyget promotion", async () => {
1164 const promotionRules = await service.addPromotionBuyRules(
1165 promotion.id,
1166 [
1167 {
1168 attribute: "items.product.id",
1169 operator: "in",
1170 values: ["prod_3", "prod_4"],
1171 },
1172 ]
1173 )
1174
1175 expect(promotionRules).toEqual([
1176 expect.objectContaining({
1177 id: promotionRules[0].id,
1178 attribute: "items.product.id",
1179 operator: "in",
1180 values: expect.arrayContaining([
1181 expect.objectContaining({ value: "prod_3" }),
1182 expect.objectContaining({ value: "prod_4" }),
1183 ]),
1184 }),
1185 ])
1186 })
1187 })
1188
1189 describe("removePromotionRules", () => {
1190 let promotion
1191
1192 beforeEach(async () => {
1193 promotion = await createDefaultPromotion(service, {
1194 rules: [
1195 {
1196 attribute: "customer.customer_group.id",
1197 operator: "in",
1198 values: ["VIP", "top100"],
1199 },
1200 ],
1201 })
1202 })
1203
1204 it("should throw an error when promotion with id does not exist", async () => {
1205 let error
1206
1207 try {
1208 await service.removePromotionRules("does-not-exist", [])
1209 } catch (e) {
1210 error = e
1211 }
1212
1213 expect(error.message).toEqual(
1214 "Promotion with id: does-not-exist was not found"
1215 )
1216 })
1217
1218 it("should throw an error when a id is not provided", async () => {
1219 let error
1220
1221 try {
1222 await service.removePromotionRules(
1223 undefined as unknown as string,
1224 []
1225 )
1226 } catch (e) {
1227 error = e
1228 }
1229
1230 expect(error.message).toEqual("promotion - id must be defined")
1231 })
1232
1233 it("should successfully remove rules for a promotion", async () => {
1234 const ruleIds = promotion.rules.map((rule) => rule.id)
1235
1236 await service.removePromotionRules(promotion.id, ruleIds)
1237
1238 const updatedPromotion = await service.retrievePromotion(
1239 promotion.id,
1240 {
1241 relations: ["rules", "rules.values"],
1242 }
1243 )
1244
1245 expect(updatedPromotion).toEqual(
1246 expect.objectContaining({
1247 id: promotion.id,
1248 rules: [],
1249 })
1250 )
1251 })
1252 })
1253
1254 describe("removePromotionTargetRules", () => {
1255 let promotion
1256
1257 beforeEach(async () => {
1258 promotion = await createDefaultPromotion(service, {
1259 application_method: {
1260 target_rules: [
1261 {
1262 attribute: "customer.customer_group.id",
1263 operator: "in",
1264 values: ["VIP", "top100"],
1265 },
1266 ],
1267 } as any,
1268 })
1269 })
1270
1271 it("should throw an error when promotion with id does not exist", async () => {
1272 let error
1273
1274 try {
1275 await service.removePromotionTargetRules("does-not-exist", [])
1276 } catch (e) {
1277 error = e
1278 }
1279
1280 expect(error.message).toEqual(
1281 "Promotion with id: does-not-exist was not found"
1282 )
1283 })
1284
1285 it("should throw an error when a id is not provided", async () => {
1286 let error
1287
1288 try {
1289 await service.removePromotionTargetRules(
1290 undefined as unknown as string,
1291 []
1292 )
1293 } catch (e) {
1294 error = e
1295 }
1296
1297 expect(error.message).toEqual("promotion - id must be defined")
1298 })
1299
1300 it("should successfully create rules for a promotion", async () => {
1301 const ruleIds = promotion.application_method.target_rules.map(
1302 (rule) => rule.id
1303 )
1304
1305 await service.removePromotionTargetRules(promotion.id, ruleIds)
1306
1307 const updatedPromotion = await service.retrievePromotion(
1308 promotion.id,
1309 {
1310 relations: ["application_method.target_rules.values"],
1311 }
1312 )
1313
1314 expect(updatedPromotion).toEqual(
1315 expect.objectContaining({
1316 id: promotion.id,
1317 application_method: expect.objectContaining({
1318 target_rules: [],
1319 }),
1320 })
1321 )
1322 })
1323 })
1324
1325 describe("removePromotionBuyRules", () => {
1326 let promotion
1327
1328 beforeEach(async () => {
1329 promotion = await createDefaultPromotion(service, {
1330 type: PromotionType.BUYGET,
1331 application_method: {
1332 apply_to_quantity: 1,
1333 max_quantity: 1,
1334 allocation: "each",
1335 buy_rules_min_quantity: 1,
1336 target_rules: [
1337 {
1338 attribute: "items.product.id",
1339 operator: "in",
1340 values: ["prod_1", "prod_2"],
1341 },
1342 ],
1343 buy_rules: [
1344 {
1345 attribute: "items.product_collection.id",
1346 operator: "eq",
1347 values: ["pcol_towel"],
1348 },
1349 ],
1350 } as any,
1351 })
1352 })
1353
1354 it("should throw an error when promotion with id does not exist", async () => {
1355 let error
1356
1357 try {
1358 await service.removePromotionBuyRules("does-not-exist", [])
1359 } catch (e) {
1360 error = e
1361 }
1362
1363 expect(error.message).toEqual(
1364 "Promotion with id: does-not-exist was not found"
1365 )
1366 })
1367
1368 it("should throw an error when a id is not provided", async () => {
1369 let error
1370
1371 try {
1372 await service.removePromotionBuyRules(
1373 undefined as unknown as string,
1374 []
1375 )
1376 } catch (e) {
1377 error = e
1378 }
1379
1380 expect(error.message).toEqual("promotion - id must be defined")
1381 })
1382
1383 it("should successfully remove rules for a promotion", async () => {
1384 const ruleIds = promotion.application_method.buy_rules.map(
1385 (rule) => rule.id
1386 )
1387
1388 await service.removePromotionBuyRules(promotion.id, ruleIds)
1389
1390 const updatedPromotion = await service.retrievePromotion(
1391 promotion.id,
1392 {
1393 relations: ["application_method.buy_rules.values"],
1394 }
1395 )
1396
1397 expect(updatedPromotion).toEqual(
1398 expect.objectContaining({
1399 id: promotion.id,
1400 application_method: expect.objectContaining({
1401 buy_rules: [],
1402 }),
1403 })
1404 )
1405 })
1406 })
1407 })
1408 },
1409 })