Raw
1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 Modules,
4 ProductStatus,
5 PromotionStatus,
6 PromotionType,
7 } from "@medusajs/utils"
8 import {
9 createAdminUser,
10 generatePublishableKey,
11 generateStoreHeaders,
12 } from "../../../../helpers/create-admin-user"
13 import { setupTaxStructure } from "../../../../modules/__tests__/fixtures/tax"
14 import { medusaTshirtProduct } from "../../../__fixtures__/product"
15 import {
16 updateCampaignsWorkflow,
17 updatePromotionRulesWorkflow,
18 updatePromotionsWorkflow,
19 } from "@medusajs/core-flows"
20
21 jest.setTimeout(500000)
22
23 const adminHeaders = {
24 headers: { "x-medusa-access-token": "test_token" },
25 }
26
27 const standardPromotionPayload = {
28 code: "TEST",
29 type: PromotionType.STANDARD,
30 status: PromotionStatus.ACTIVE,
31 is_automatic: true,
32 campaign: {
33 name: "test",
34 campaign_identifier: "test-1",
35 budget: {
36 type: "usage",
37 limit: 100,
38 },
39 },
40 application_method: {
41 target_type: "items",
42 type: "fixed",
43 allocation: "each",
44 currency_code: "usd",
45 value: 100,
46 max_quantity: 100,
47 target_rules: [
48 {
49 attribute: "test.test",
50 operator: "eq",
51 values: ["test1", "test2"],
52 },
53 ],
54 },
55 rules: [
56 {
57 attribute: "test.test",
58 operator: "eq",
59 values: ["test1", "test2"],
60 },
61 ],
62 }
63
64 medusaIntegrationTestRunner({
65 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
66 let appContainer
67 beforeAll(async () => {
68 appContainer = getContainer()
69 })
70
71 describe("Admin Promotions API", () => {
72 let promotion
73 let standardPromotion
74 let shippingProfile
75
76 const promotionRule = {
77 operator: "eq",
78 attribute: "old_attr",
79 values: ["old value"],
80 }
81
82 beforeAll(async () => {
83 await createAdminUser(dbConnection, adminHeaders, appContainer)
84
85 await setupTaxStructure(appContainer.resolve(Modules.TAX))
86
87 promotion = standardPromotion = (
88 await api.post(
89 `/admin/promotions`,
90 {
91 code: "TEST_ACROSS",
92 type: PromotionType.STANDARD,
93 status: PromotionStatus.ACTIVE,
94 application_method: {
95 type: "fixed",
96 allocation: "across",
97 target_type: "items",
98 value: 100,
99 target_rules: [promotionRule],
100 currency_code: "usd",
101 },
102 rules: [promotionRule],
103 },
104 adminHeaders
105 )
106 ).data.promotion
107
108 shippingProfile = (
109 await api.post(
110 `/admin/shipping-profiles`,
111 { name: "default", type: "default" },
112 adminHeaders
113 )
114 ).data.shipping_profile
115
116 await dbUtils.snapshot()
117 })
118
119 describe("GET /admin/promotions/:id", () => {
120 it("should throw an error if id does not exist", async () => {
121 const { response } = await api
122 .get(`/admin/promotions/does-not-exist`, adminHeaders)
123 .catch((e) => e)
124
125 expect(response.status).toEqual(404)
126 expect(response.data.message).toEqual(
127 "Promotion with id or code: does-not-exist was not found"
128 )
129 })
130
131 it("should get the requested promotion by id or codde", async () => {
132 let response = await api.get(
133 `/admin/promotions/${promotion.id}`,
134 adminHeaders
135 )
136
137 expect(response.status).toEqual(200)
138 expect(response.data.promotion).toEqual(
139 expect.objectContaining({
140 id: promotion.id,
141 })
142 )
143
144 response = await api.get(
145 `/admin/promotions/${promotion.code}`,
146 adminHeaders
147 )
148
149 expect(response.status).toEqual(200)
150 expect(response.data.promotion).toEqual(
151 expect.objectContaining({
152 id: promotion.id,
153 })
154 )
155 })
156
157 it("should get the requested promotion with filtered fields and relations", async () => {
158 const response = await api.get(
159 `/admin/promotions/${promotion.id}?fields=id,code`,
160 adminHeaders
161 )
162
163 expect(response.status).toEqual(200)
164 expect(response.data.promotion).toEqual({
165 id: promotion.id,
166 code: promotion.code,
167 })
168 })
169 })
170
171 describe("GET /admin/promotions", () => {
172 it("should get all promotions and its count", async () => {
173 const response = await api.get(`/admin/promotions`, adminHeaders)
174
175 expect(response.status).toEqual(200)
176 expect(response.data.count).toEqual(1)
177 expect(response.data.promotions).toEqual([
178 expect.objectContaining({
179 id: expect.any(String),
180 }),
181 ])
182 })
183
184 it("should support search of promotions", async () => {
185 await api.post(
186 `/admin/promotions`,
187 {
188 code: "first",
189 type: PromotionType.STANDARD,
190 status: PromotionStatus.ACTIVE,
191 application_method: {
192 type: "fixed",
193 target_type: "order",
194 value: 100,
195 currency_code: "usd",
196 },
197 },
198 adminHeaders
199 )
200
201 const response = await api.get(
202 `/admin/promotions?q=fir`,
203 adminHeaders
204 )
205
206 expect(response.status).toEqual(200)
207 expect(response.data.promotions).toEqual([
208 expect.objectContaining({
209 code: "first",
210 }),
211 ])
212 })
213
214 it("should get all promotions and its count filtered", async () => {
215 const response = await api.get(
216 `/admin/promotions?fields=code,created_at,application_method.id`,
217 adminHeaders
218 )
219
220 expect(response.status).toEqual(200)
221 expect(response.data.count).toEqual(1)
222 expect(response.data.promotions).toEqual([
223 {
224 id: expect.any(String),
225 code: "TEST_ACROSS",
226 created_at: expect.any(String),
227 application_method: {
228 id: expect.any(String),
229 },
230 },
231 ])
232 })
233 })
234
235 describe("POST /admin/promotions", () => {
236 it("should throw an error if required params are not passed", async () => {
237 const { response } = await api
238 .post(
239 `/admin/promotions`,
240 { type: PromotionType.STANDARD },
241 adminHeaders
242 )
243 .catch((e) => e)
244
245 expect(response.status).toEqual(400)
246 expect(response.data.message).toEqual(
247 "Invalid request: Field 'code' is required; Field 'application_method' is required"
248 )
249 })
250
251 it("should throw error when an incorrect status is passed", async () => {
252 const { response } = await api
253 .post(
254 `/admin/promotions`,
255 { ...standardPromotionPayload, status: "does-not-exist" },
256 adminHeaders
257 )
258 .catch((e) => e)
259
260 expect(response.status).toEqual(400)
261 expect(response.data).toEqual({
262 type: "invalid_data",
263 message:
264 "Invalid request: Expected: 'draft, active, inactive' for field 'status', but got: 'does-not-exist'",
265 })
266 })
267
268 it("should create a standard promotion successfully", async () => {
269 const response = await api.post(
270 `/admin/promotions`,
271 standardPromotionPayload,
272 adminHeaders
273 )
274
275 expect(response.status).toEqual(200)
276 expect(response.data.promotion).toEqual(
277 expect.objectContaining({
278 id: expect.any(String),
279 code: "TEST",
280 type: "standard",
281 is_automatic: true,
282 campaign: expect.objectContaining({
283 name: "test",
284 campaign_identifier: "test-1",
285 budget: expect.objectContaining({
286 currency_code: null,
287 type: "usage",
288 limit: 100,
289 }),
290 }),
291 application_method: expect.objectContaining({
292 value: 100,
293 max_quantity: 100,
294 type: "fixed",
295 target_type: "items",
296 allocation: "each",
297 target_rules: [
298 expect.objectContaining({
299 operator: "eq",
300 attribute: "test.test",
301 values: expect.arrayContaining([
302 expect.objectContaining({ value: "test1" }),
303 expect.objectContaining({ value: "test2" }),
304 ]),
305 }),
306 ],
307 }),
308 rules: [
309 expect.objectContaining({
310 operator: "eq",
311 attribute: "test.test",
312 values: expect.arrayContaining([
313 expect.objectContaining({ value: "test1" }),
314 expect.objectContaining({ value: "test2" }),
315 ]),
316 }),
317 ],
318 })
319 )
320 })
321
322 it("should throw an error if buy_rules params are not passed", async () => {
323 const { response } = await api
324 .post(
325 `/admin/promotions`,
326 {
327 code: "TEST",
328 type: PromotionType.BUYGET,
329 status: PromotionStatus.ACTIVE,
330 is_automatic: true,
331 application_method: {
332 target_type: "items",
333 type: "fixed",
334 allocation: "each",
335 value: 100,
336 max_quantity: 100,
337 currency_code: "usd",
338 target_rules: [
339 {
340 attribute: "test.test",
341 operator: "eq",
342 values: ["test1", "test2"],
343 },
344 ],
345 },
346 rules: [
347 {
348 attribute: "test.test",
349 operator: "eq",
350 values: ["test1", "test2"],
351 },
352 ],
353 },
354 adminHeaders
355 )
356 .catch((e) => e)
357
358 expect(response.status).toEqual(400)
359 expect(response.data.message).toEqual(
360 "Invalid request: Buyget promotions require at least one buy rule and quantities to be defined"
361 )
362 })
363
364 it("should throw an error if buy_rules params are not passed", async () => {
365 const { response } = await api
366 .post(
367 `/admin/promotions`,
368 {
369 code: "TEST",
370 type: PromotionType.BUYGET,
371 status: PromotionStatus.ACTIVE,
372 is_automatic: true,
373 application_method: {
374 target_type: "items",
375 type: "fixed",
376 allocation: "each",
377 value: 100,
378 max_quantity: 100,
379 currency_code: "usd",
380 buy_rules: [
381 {
382 attribute: "test.test",
383 operator: "eq",
384 values: ["test1", "test2"],
385 },
386 ],
387 },
388 rules: [
389 {
390 attribute: "test.test",
391 operator: "eq",
392 values: ["test1", "test2"],
393 },
394 ],
395 },
396 adminHeaders
397 )
398 .catch((e) => e)
399
400 expect(response.status).toEqual(400)
401 // expect(response.data.message).toEqual(
402 // "Target rules are required for buyget promotion type"
403 // )
404 })
405
406 it("should create a buyget promotion successfully", async () => {
407 const response = await api.post(
408 `/admin/promotions`,
409 {
410 code: "TEST",
411 type: PromotionType.BUYGET,
412 status: PromotionStatus.ACTIVE,
413 is_automatic: true,
414 campaign: {
415 name: "test",
416 campaign_identifier: "test-1",
417 budget: {
418 type: "usage",
419 limit: 100,
420 },
421 },
422 application_method: {
423 target_type: "items",
424 type: "fixed",
425 allocation: "each",
426 value: 100,
427 max_quantity: 100,
428 apply_to_quantity: 1,
429 buy_rules_min_quantity: 1,
430 currency_code: "usd",
431 target_rules: [
432 {
433 attribute: "test.test",
434 operator: "eq",
435 values: ["test1", "test2"],
436 },
437 ],
438 buy_rules: [
439 {
440 attribute: "test.test",
441 operator: "eq",
442 values: ["test1", "test2"],
443 },
444 ],
445 },
446 rules: [
447 {
448 attribute: "test.test",
449 operator: "eq",
450 values: ["test1", "test2"],
451 },
452 ],
453 },
454 adminHeaders
455 )
456
457 expect(response.status).toEqual(200)
458 expect(response.data.promotion).toEqual(
459 expect.objectContaining({
460 id: expect.any(String),
461 code: "TEST",
462 type: "buyget",
463 is_automatic: true,
464 campaign: expect.objectContaining({
465 name: "test",
466 campaign_identifier: "test-1",
467 budget: expect.objectContaining({
468 type: "usage",
469 limit: 100,
470 }),
471 }),
472 application_method: expect.objectContaining({
473 value: 100,
474 max_quantity: 100,
475 type: "fixed",
476 target_type: "items",
477 allocation: "each",
478 apply_to_quantity: 1,
479 buy_rules_min_quantity: 1,
480 target_rules: [
481 expect.objectContaining({
482 operator: "eq",
483 attribute: "test.test",
484 values: expect.arrayContaining([
485 expect.objectContaining({ value: "test1" }),
486 expect.objectContaining({ value: "test2" }),
487 ]),
488 }),
489 ],
490 buy_rules: [
491 expect.objectContaining({
492 operator: "eq",
493 attribute: "test.test",
494 values: expect.arrayContaining([
495 expect.objectContaining({ value: "test1" }),
496 expect.objectContaining({ value: "test2" }),
497 ]),
498 }),
499 ],
500 }),
501 rules: [
502 expect.objectContaining({
503 operator: "eq",
504 attribute: "test.test",
505 values: expect.arrayContaining([
506 expect.objectContaining({ value: "test1" }),
507 expect.objectContaining({ value: "test2" }),
508 ]),
509 }),
510 ],
511 })
512 )
513 })
514
515 describe("with cart", () => {
516 it("should add promotion to cart only when gte rule matches", async () => {
517 const publishableKey = await generatePublishableKey(appContainer)
518 const storeHeaders = generateStoreHeaders({ publishableKey })
519
520 const salesChannel = (
521 await api.post(
522 "/admin/sales-channels",
523 { name: "Webshop", description: "channel" },
524 adminHeaders
525 )
526 ).data.sales_channel
527
528 const region = (
529 await api.post(
530 "/admin/regions",
531 {
532 payment_providers: ["pp_system_default"],
533 name: "US",
534 currency_code: "usd",
535 countries: ["us"],
536 },
537 adminHeaders
538 )
539 ).data.region
540
541 const product = (
542 await api.post(
543 "/admin/products",
544 {
545 ...medusaTshirtProduct,
546 shipping_profile_id: shippingProfile.id,
547 },
548 adminHeaders
549 )
550 ).data.product
551
552 const cart = (
553 await api.post(
554 `/store/carts`,
555 {
556 currency_code: "usd",
557 sales_channel_id: salesChannel.id,
558 region_id: region.id,
559 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
560 promo_codes: [promotion.code],
561 },
562 storeHeaders
563 )
564 ).data.cart
565
566 const response = await api.post(
567 `/admin/promotions`,
568 {
569 code: "TEST",
570 type: PromotionType.STANDARD,
571 status: PromotionStatus.ACTIVE,
572 is_automatic: true,
573 application_method: {
574 target_type: "items",
575 type: "fixed",
576 allocation: "each",
577 currency_code: "usd",
578 value: 100,
579 max_quantity: 100,
580 },
581 rules: [
582 {
583 attribute: "subtotal",
584 operator: "gte",
585 values: "2000",
586 },
587 ],
588 },
589 adminHeaders
590 )
591
592 expect(response.status).toEqual(200)
593 expect(response.data.promotion).toEqual(
594 expect.objectContaining({
595 id: expect.any(String),
596 code: "TEST",
597 type: "standard",
598 is_automatic: true,
599 application_method: expect.objectContaining({
600 value: 100,
601 max_quantity: 100,
602 type: "fixed",
603 target_type: "items",
604 allocation: "each",
605 target_rules: [],
606 }),
607 rules: [
608 expect.objectContaining({
609 operator: "gte",
610 attribute: "subtotal",
611 values: expect.arrayContaining([
612 expect.objectContaining({ value: "2000" }),
613 ]),
614 }),
615 ],
616 })
617 )
618
619 const cartWithPromotion1 = (
620 await api.post(
621 `/store/carts/${cart.id}`,
622 { promo_codes: [promotion.code] },
623 storeHeaders
624 )
625 ).data.cart
626
627 expect(cartWithPromotion1).toEqual(
628 expect.objectContaining({
629 items: [
630 expect.objectContaining({
631 adjustments: [],
632 }),
633 ],
634 promotions: [],
635 })
636 )
637
638 const cartWithPromotion2 = (
639 await api.post(
640 `/store/carts/${cart.id}/line-items`,
641 { variant_id: product.variants[0].id, quantity: 40 },
642 storeHeaders
643 )
644 ).data.cart
645
646 expect(cartWithPromotion2).toEqual(
647 expect.objectContaining({
648 items: [
649 expect.objectContaining({
650 adjustments: [
651 expect.objectContaining({
652 code: response.data.promotion.code,
653 }),
654 ],
655 }),
656 ],
657 promotions: [
658 expect.objectContaining({
659 code: response.data.promotion.code,
660 }),
661 ],
662 })
663 )
664 })
665
666 it("should add promotion and remove it from cart using delete", async () => {
667 const publishableKey = await generatePublishableKey(appContainer)
668 const storeHeaders = generateStoreHeaders({ publishableKey })
669
670 const salesChannel = (
671 await api.post(
672 "/admin/sales-channels",
673 { name: "Webshop", description: "channel" },
674 adminHeaders
675 )
676 ).data.sales_channel
677
678 const region = (
679 await api.post(
680 "/admin/regions",
681 {
682 payment_providers: ["pp_system_default"],
683 name: "US",
684 currency_code: "usd",
685 countries: ["us"],
686 },
687 adminHeaders
688 )
689 ).data.region
690
691 const product = (
692 await api.post(
693 "/admin/products",
694 {
695 ...medusaTshirtProduct,
696 shipping_profile_id: shippingProfile.id,
697 },
698 adminHeaders
699 )
700 ).data.product
701
702 const cart = (
703 await api.post(
704 `/store/carts`,
705 {
706 currency_code: "usd",
707 sales_channel_id: salesChannel.id,
708 region_id: region.id,
709 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
710 },
711 storeHeaders
712 )
713 ).data.cart
714
715 const response = await api.post(
716 `/admin/promotions`,
717 {
718 code: "TEST",
719 type: PromotionType.STANDARD,
720 status: PromotionStatus.ACTIVE,
721 is_automatic: false,
722 application_method: {
723 target_type: "items",
724 type: "fixed",
725 allocation: "each",
726 currency_code: "usd",
727 value: 100,
728 max_quantity: 100,
729 },
730 rules: [
731 {
732 attribute: "subtotal",
733 operator: "gte",
734 values: "1",
735 },
736 ],
737 },
738 adminHeaders
739 )
740
741 await Promise.all([
742 api
743 .post(
744 `/store/carts/${cart.id}`,
745 {
746 promo_codes: [response.data.promotion.code],
747 },
748 storeHeaders
749 )
750 .catch(() => {}),
751 api
752 .post(
753 `/store/carts/${cart.id}`,
754 {
755 promo_codes: [response.data.promotion.code],
756 },
757 storeHeaders
758 )
759 .catch(() => {}),
760 ])
761
762 const cartAfterPromotion = (
763 await api.get(`/store/carts/${cart.id}`, storeHeaders)
764 ).data.cart
765
766 expect(cartAfterPromotion).toEqual(
767 expect.objectContaining({
768 promotions: [
769 expect.objectContaining({
770 code: response.data.promotion.code,
771 }),
772 ],
773 })
774 )
775
776 const cartWithPromotion2 = (
777 await api.post(
778 `/store/carts/${cart.id}/line-items`,
779 { variant_id: product.variants[0].id, quantity: 40 },
780 storeHeaders
781 )
782 ).data.cart
783
784 expect(cartWithPromotion2).toEqual(
785 expect.objectContaining({
786 promotions: [
787 expect.objectContaining({
788 code: response.data.promotion.code,
789 }),
790 ],
791 })
792 )
793
794 await api.delete(`/store/carts/${cart.id}/promotions`, {
795 data: {
796 promo_codes: [response.data.promotion.code],
797 },
798 ...storeHeaders,
799 })
800
801 const cartWithoutPromotion = (
802 await api.get(`/store/carts/${cart.id}`, storeHeaders)
803 ).data.cart
804
805 expect(cartWithoutPromotion).toEqual(
806 expect.objectContaining({
807 promotions: [],
808 })
809 )
810 })
811
812 it("should limit usage of promotion per email attribute as defined in campaign budget", async () => {
813 const publishableKey = await generatePublishableKey(appContainer)
814 const storeHeaders = generateStoreHeaders({ publishableKey })
815
816 const salesChannel = (
817 await api.post(
818 "/admin/sales-channels",
819 { name: "Webshop", description: "channel" },
820 adminHeaders
821 )
822 ).data.sales_channel
823
824 const region = (
825 await api.post(
826 "/admin/regions",
827 {
828 payment_providers: ["pp_system_default"],
829 name: "US",
830 currency_code: "usd",
831 countries: ["us"],
832 },
833 adminHeaders
834 )
835 ).data.region
836
837 const product = (
838 await api.post(
839 "/admin/products",
840 {
841 ...medusaTshirtProduct,
842 },
843 adminHeaders
844 )
845 ).data.product
846
847 const campaign = (
848 await api.post(
849 `/admin/campaigns`,
850 {
851 name: "TEST",
852 budget: {
853 type: "use_by_attribute",
854 limit: 2,
855 attribute: "customer_email",
856 },
857 campaign_identifier: "PROMO_CAMPAIGN",
858 },
859 adminHeaders
860 )
861 ).data.campaign
862
863 const response = await api.post(
864 `/admin/promotions`,
865 {
866 code: "TEST_PROMO",
867 type: PromotionType.STANDARD,
868 status: PromotionStatus.ACTIVE,
869 is_automatic: false,
870 application_method: {
871 target_type: "items",
872 type: "fixed",
873 allocation: "each",
874 currency_code: "usd",
875 value: 100,
876 max_quantity: 100,
877 },
878 campaign_id: campaign.id,
879 },
880 adminHeaders
881 )
882
883 let cart = (
884 await api.post(
885 `/store/carts`,
886 {
887 currency_code: "usd",
888 sales_channel_id: salesChannel.id,
889 region_id: region.id,
890 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
891 email: "canusethistwice@test.com",
892 promo_codes: [response.data.promotion.code],
893 },
894 storeHeaders
895 )
896 ).data.cart
897
898 expect(cart).toEqual(
899 expect.objectContaining({
900 promotions: [
901 expect.objectContaining({
902 code: response.data.promotion.code,
903 }),
904 ],
905 })
906 )
907
908 let promotionCampaign = (
909 await api.get(
910 `/admin/campaigns/${campaign.id}?fields=budget.*,budget.usages.*`,
911 adminHeaders
912 )
913 ).data.campaign
914
915 expect(promotionCampaign).toEqual(
916 expect.objectContaining({
917 budget: expect.objectContaining({
918 used: 0,
919 limit: 2,
920 attribute: "customer_email",
921 type: "use_by_attribute",
922 usages: [],
923 }),
924 })
925 )
926
927 let paymentCollection = (
928 await api.post(
929 `/store/payment-collections`,
930 { cart_id: cart.id },
931 storeHeaders
932 )
933 ).data.payment_collection
934
935 await api.post(
936 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
937 { provider_id: "pp_system_default" },
938 storeHeaders
939 )
940
941 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
942
943 cart = (
944 await api.post(
945 `/store/carts`,
946 {
947 currency_code: "usd",
948 sales_channel_id: salesChannel.id,
949 region_id: region.id,
950 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
951 email: "canusethistwice@test.com",
952 promo_codes: [response.data.promotion.code],
953 },
954 storeHeaders
955 )
956 ).data.cart
957
958 expect(cart).toEqual(
959 expect.objectContaining({
960 promotions: [
961 expect.objectContaining({
962 code: response.data.promotion.code,
963 }),
964 ],
965 })
966 )
967
968 promotionCampaign = (
969 await api.get(
970 `/admin/campaigns/${campaign.id}?fields=budget.*,budget.usages.*`,
971 adminHeaders
972 )
973 ).data.campaign
974
975 expect(promotionCampaign).toEqual(
976 expect.objectContaining({
977 budget: expect.objectContaining({
978 used: 1,
979 limit: 2,
980 attribute: "customer_email",
981 type: "use_by_attribute",
982 usages: [
983 // usage recorder after first complete
984 expect.objectContaining({
985 attribute_value: "canusethistwice@test.com",
986 used: 1,
987 }),
988 ],
989 }),
990 })
991 )
992
993 paymentCollection = (
994 await api.post(
995 `/store/payment-collections`,
996 { cart_id: cart.id },
997 storeHeaders
998 )
999 ).data.payment_collection
1000
1001 await api.post(
1002 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
1003 { provider_id: "pp_system_default" },
1004 storeHeaders
1005 )
1006
1007 // complete for the second time
1008 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
1009
1010 promotionCampaign = (
1011 await api.get(
1012 `/admin/campaigns/${campaign.id}?fields=budget.*,budget.usages.*`,
1013 adminHeaders
1014 )
1015 ).data.campaign
1016
1017 expect(promotionCampaign).toEqual(
1018 expect.objectContaining({
1019 budget: expect.objectContaining({
1020 used: 2,
1021 limit: 2,
1022 attribute: "customer_email",
1023 type: "use_by_attribute",
1024 usages: [
1025 expect.objectContaining({
1026 attribute_value: "canusethistwice@test.com",
1027 used: 2,
1028 }),
1029 ],
1030 }),
1031 })
1032 )
1033
1034 cart = (
1035 await api.post(
1036 `/store/carts`,
1037 {
1038 currency_code: "usd",
1039 sales_channel_id: salesChannel.id,
1040 region_id: region.id,
1041 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1042 email: "canusethistwice@test.com",
1043 },
1044 storeHeaders
1045 )
1046 ).data.cart
1047
1048 cart = (
1049 await api.post(
1050 `/store/carts/${cart.id}`,
1051 {
1052 promo_codes: [response.data.promotion.code],
1053 },
1054 storeHeaders
1055 )
1056 ).data.cart
1057
1058 expect(cart.promotions.length).toEqual(0) // prmotion is not applied
1059
1060 cart = (
1061 await api.post(
1062 `/store/carts/${cart.id}`,
1063 {
1064 email: "canuseit@test.com",
1065 promo_codes: [response.data.promotion.code],
1066 },
1067 storeHeaders
1068 )
1069 ).data.cart
1070
1071 // promotion is successfully applied with different email
1072 expect(cart.promotions.length).toEqual(1)
1073 expect(cart.promotions[0].code).toEqual(
1074 response.data.promotion.code
1075 )
1076 })
1077
1078 it("should remove promotion after email is replaced by already used email for that promotion", async () => {
1079 const publishableKey = await generatePublishableKey(appContainer)
1080 const storeHeaders = generateStoreHeaders({ publishableKey })
1081
1082 const salesChannel = (
1083 await api.post(
1084 "/admin/sales-channels",
1085 { name: "Webshop", description: "channel" },
1086 adminHeaders
1087 )
1088 ).data.sales_channel
1089
1090 const region = (
1091 await api.post(
1092 "/admin/regions",
1093 {
1094 payment_providers: ["pp_system_default"],
1095 name: "US",
1096 currency_code: "usd",
1097 countries: ["us"],
1098 },
1099 adminHeaders
1100 )
1101 ).data.region
1102
1103 const product = (
1104 await api.post(
1105 "/admin/products",
1106 {
1107 ...medusaTshirtProduct,
1108 },
1109 adminHeaders
1110 )
1111 ).data.product
1112
1113 const campaign = (
1114 await api.post(
1115 `/admin/campaigns`,
1116 {
1117 name: "TEST",
1118 budget: {
1119 type: "use_by_attribute",
1120 limit: 1,
1121 attribute: "customer_email",
1122 },
1123 campaign_identifier: "PROMO_CAMPAIGN",
1124 },
1125 adminHeaders
1126 )
1127 ).data.campaign
1128
1129 const response = await api.post(
1130 `/admin/promotions`,
1131 {
1132 code: "TEST_PROMO",
1133 type: PromotionType.STANDARD,
1134 status: PromotionStatus.ACTIVE,
1135 is_automatic: false,
1136 application_method: {
1137 target_type: "items",
1138 type: "fixed",
1139 allocation: "each",
1140 currency_code: "usd",
1141 value: 100,
1142 max_quantity: 100,
1143 },
1144 campaign_id: campaign.id,
1145 },
1146 adminHeaders
1147 )
1148
1149 let cart = (
1150 await api.post(
1151 `/store/carts`,
1152 {
1153 currency_code: "usd",
1154 sales_channel_id: salesChannel.id,
1155 region_id: region.id,
1156 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1157 email: "canuseitonce@test.com",
1158 promo_codes: [response.data.promotion.code],
1159 },
1160 storeHeaders
1161 )
1162 ).data.cart
1163
1164 expect(cart).toEqual(
1165 expect.objectContaining({
1166 promotions: [
1167 expect.objectContaining({
1168 code: response.data.promotion.code,
1169 }),
1170 ],
1171 })
1172 )
1173
1174 let promotionCampaign = (
1175 await api.get(
1176 `/admin/campaigns/${campaign.id}?fields=budget.*,budget.usages.*`,
1177 adminHeaders
1178 )
1179 ).data.campaign
1180
1181 expect(promotionCampaign).toEqual(
1182 expect.objectContaining({
1183 budget: expect.objectContaining({
1184 used: 0,
1185 limit: 1,
1186 attribute: "customer_email",
1187 type: "use_by_attribute",
1188 usages: [],
1189 }),
1190 })
1191 )
1192
1193 let paymentCollection = (
1194 await api.post(
1195 `/store/payment-collections`,
1196 { cart_id: cart.id },
1197 storeHeaders
1198 )
1199 ).data.payment_collection
1200
1201 await api.post(
1202 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
1203 { provider_id: "pp_system_default" },
1204 storeHeaders
1205 )
1206
1207 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
1208
1209 cart = (
1210 await api.post(
1211 `/store/carts`,
1212 {
1213 currency_code: "usd",
1214 sales_channel_id: salesChannel.id,
1215 region_id: region.id,
1216 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1217 promo_codes: [response.data.promotion.code],
1218 email: "fakeemail@test.com",
1219 },
1220 storeHeaders
1221 )
1222 ).data.cart
1223
1224 expect(cart).toEqual(
1225 expect.objectContaining({
1226 promotions: [
1227 expect.objectContaining({
1228 code: response.data.promotion.code,
1229 }),
1230 ],
1231 })
1232 )
1233
1234 cart = (
1235 await api.post(
1236 `/store/carts/${cart.id}`,
1237 {
1238 email: "canuseitonce@test.com",
1239 },
1240 storeHeaders
1241 )
1242 ).data.cart
1243
1244 expect(cart.promotions.length).toEqual(0) // prmotion is removed
1245 })
1246
1247 it("should throw if email is not provided when campaign budget type is use_by_attribute", async () => {
1248 const publishableKey = await generatePublishableKey(appContainer)
1249 const storeHeaders = generateStoreHeaders({ publishableKey })
1250
1251 const salesChannel = (
1252 await api.post(
1253 "/admin/sales-channels",
1254 { name: "Webshop", description: "channel" },
1255 adminHeaders
1256 )
1257 ).data.sales_channel
1258
1259 const region = (
1260 await api.post(
1261 "/admin/regions",
1262 {
1263 payment_providers: ["pp_system_default"],
1264 name: "US",
1265 currency_code: "usd",
1266 countries: ["us"],
1267 },
1268 adminHeaders
1269 )
1270 ).data.region
1271
1272 const product = (
1273 await api.post(
1274 "/admin/products",
1275 {
1276 ...medusaTshirtProduct,
1277 },
1278 adminHeaders
1279 )
1280 ).data.product
1281
1282 const campaign = (
1283 await api.post(
1284 `/admin/campaigns`,
1285 {
1286 name: "TEST",
1287 budget: {
1288 type: "use_by_attribute",
1289 limit: 1,
1290 attribute: "customer_email",
1291 },
1292 campaign_identifier: "PROMO_CAMPAIGN",
1293 },
1294 adminHeaders
1295 )
1296 ).data.campaign
1297
1298 const response = await api.post(
1299 `/admin/promotions`,
1300 {
1301 code: "TEST_PROMO",
1302 type: PromotionType.STANDARD,
1303 status: PromotionStatus.ACTIVE,
1304 is_automatic: false,
1305 application_method: {
1306 target_type: "items",
1307 type: "fixed",
1308 allocation: "each",
1309 currency_code: "usd",
1310 value: 100,
1311 max_quantity: 100,
1312 },
1313 campaign_id: campaign.id,
1314 },
1315 adminHeaders
1316 )
1317
1318 let cart = (
1319 await api.post(
1320 `/store/carts`,
1321 {
1322 currency_code: "usd",
1323 sales_channel_id: salesChannel.id,
1324 region_id: region.id,
1325 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1326 },
1327 storeHeaders
1328 )
1329 ).data.cart
1330
1331 const err = await api
1332 .post(
1333 `/store/carts/${cart.id}`,
1334 {
1335 promo_codes: [response.data.promotion.code],
1336 },
1337 storeHeaders
1338 )
1339 .catch((e) => e)
1340
1341 expect(err.response.status).toEqual(400)
1342 expect(err.response.data).toEqual({
1343 type: "invalid_data",
1344 message: `Attribute value for "customer_email" is required by promotion campaing budget`,
1345 })
1346 })
1347
1348 it("should add promotion and remove it from cart using update", async () => {
1349 const publishableKey = await generatePublishableKey(appContainer)
1350 const storeHeaders = generateStoreHeaders({ publishableKey })
1351
1352 const salesChannel = (
1353 await api.post(
1354 "/admin/sales-channels",
1355 { name: "Webshop", description: "channel" },
1356 adminHeaders
1357 )
1358 ).data.sales_channel
1359
1360 const region = (
1361 await api.post(
1362 "/admin/regions",
1363 {
1364 payment_providers: ["pp_system_default"],
1365 name: "US",
1366 currency_code: "usd",
1367 countries: ["us"],
1368 },
1369 adminHeaders
1370 )
1371 ).data.region
1372
1373 const product = (
1374 await api.post(
1375 "/admin/products",
1376 {
1377 ...medusaTshirtProduct,
1378 shipping_profile_id: shippingProfile.id,
1379 },
1380 adminHeaders
1381 )
1382 ).data.product
1383
1384 const cart = (
1385 await api.post(
1386 `/store/carts`,
1387 {
1388 currency_code: "usd",
1389 sales_channel_id: salesChannel.id,
1390 region_id: region.id,
1391 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1392 },
1393 storeHeaders
1394 )
1395 ).data.cart
1396
1397 const response = await api.post(
1398 `/admin/promotions`,
1399 {
1400 code: "TEST",
1401 type: PromotionType.STANDARD,
1402 status: PromotionStatus.ACTIVE,
1403 is_automatic: false,
1404 application_method: {
1405 target_type: "items",
1406 type: "fixed",
1407 allocation: "each",
1408 currency_code: "usd",
1409 value: 100,
1410 max_quantity: 100,
1411 },
1412 rules: [
1413 {
1414 attribute: "subtotal",
1415 operator: "gte",
1416 values: "1",
1417 },
1418 ],
1419 },
1420 adminHeaders
1421 )
1422
1423 await api.post(
1424 `/store/carts/${cart.id}`,
1425 {
1426 promo_codes: [response.data.promotion.code],
1427 },
1428 storeHeaders
1429 )
1430
1431 const cartAfterPromotion = (
1432 await api.get(`/store/carts/${cart.id}`, storeHeaders)
1433 ).data.cart
1434
1435 expect(cartAfterPromotion).toEqual(
1436 expect.objectContaining({
1437 items: [
1438 expect.objectContaining({
1439 adjustments: [
1440 expect.objectContaining({
1441 code: response.data.promotion.code,
1442 }),
1443 ],
1444 }),
1445 ],
1446 promotions: [
1447 expect.objectContaining({
1448 code: response.data.promotion.code,
1449 }),
1450 ],
1451 })
1452 )
1453
1454 const cartWithPromotion2 = (
1455 await api.post(
1456 `/store/carts/${cart.id}/line-items`,
1457 { variant_id: product.variants[0].id, quantity: 40 },
1458 storeHeaders
1459 )
1460 ).data.cart
1461
1462 expect(cartWithPromotion2).toEqual(
1463 expect.objectContaining({
1464 items: [
1465 expect.objectContaining({
1466 adjustments: [
1467 expect.objectContaining({
1468 code: response.data.promotion.code,
1469 }),
1470 ],
1471 }),
1472 ],
1473 promotions: [
1474 expect.objectContaining({
1475 code: response.data.promotion.code,
1476 }),
1477 ],
1478 })
1479 )
1480
1481 await api.post(
1482 `/store/carts/${cart.id}`,
1483 {
1484 promo_codes: [],
1485 },
1486 storeHeaders
1487 )
1488
1489 const cartWithoutPromotion = (
1490 await api.get(`/store/carts/${cart.id}`, storeHeaders)
1491 ).data.cart
1492
1493 expect(cartWithoutPromotion).toEqual(
1494 expect.objectContaining({
1495 items: [
1496 expect.objectContaining({
1497 adjustments: [],
1498 }),
1499 ],
1500 promotions: [],
1501 })
1502 )
1503 })
1504
1505 it("should add an automatic promotion and after cart update, should delete the adjustment and create a new one for the automatic promotion", async () => {
1506 const publishableKey = await generatePublishableKey(appContainer)
1507 const storeHeaders = generateStoreHeaders({ publishableKey })
1508
1509 const salesChannel = (
1510 await api.post(
1511 "/admin/sales-channels",
1512 { name: "Webshop", description: "channel" },
1513 adminHeaders
1514 )
1515 ).data.sales_channel
1516
1517 const region = (
1518 await api.post(
1519 "/admin/regions",
1520 {
1521 payment_providers: ["pp_system_default"],
1522 name: "US",
1523 currency_code: "usd",
1524 countries: ["us"],
1525 },
1526 adminHeaders
1527 )
1528 ).data.region
1529
1530 const product = (
1531 await api.post(
1532 "/admin/products",
1533 {
1534 ...medusaTshirtProduct,
1535 shipping_profile_id: shippingProfile.id,
1536 },
1537 adminHeaders
1538 )
1539 ).data.product
1540
1541 const customer = (
1542 await api.post(
1543 "/admin/customers",
1544 { email: "test@test.com" },
1545 adminHeaders
1546 )
1547 ).data.customer
1548
1549 const response = await api.post(
1550 `/admin/promotions`,
1551 {
1552 code: "AUTO_TEST",
1553 type: PromotionType.STANDARD,
1554 status: PromotionStatus.ACTIVE,
1555 is_automatic: true,
1556 application_method: {
1557 target_type: "items",
1558 type: "fixed",
1559 allocation: "each",
1560 currency_code: "usd",
1561 value: 100,
1562 max_quantity: 100,
1563 },
1564 rules: [
1565 {
1566 attribute: "customer_id",
1567 operator: "eq",
1568 values: customer.id,
1569 },
1570 ],
1571 },
1572 adminHeaders
1573 )
1574
1575 // Should apply automatic promotion
1576 const cart = (
1577 await api.post(
1578 `/store/carts`,
1579 {
1580 currency_code: "usd",
1581 sales_channel_id: salesChannel.id,
1582 email: customer.email,
1583 region_id: region.id,
1584 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1585 },
1586 storeHeaders
1587 )
1588 ).data.cart
1589
1590 const shippingAddressData = {
1591 address_1: "test address 1",
1592 address_2: "test address 2",
1593 city: "SF",
1594 country_code: "us",
1595 province: "CA",
1596 postal_code: "94016",
1597 }
1598
1599 // Should not duplicate an already applied automatic promotion
1600 await api.post(
1601 `/store/carts/${cart.id}`,
1602 {
1603 shipping_address: shippingAddressData,
1604 },
1605 storeHeaders
1606 )
1607
1608 const cartAfterPromotion = (
1609 await api.get(`/store/carts/${cart.id}`, storeHeaders)
1610 ).data.cart
1611
1612 const itemWithPromotion = cartAfterPromotion.items[0]
1613 const adjustments = itemWithPromotion.adjustments
1614
1615 expect(adjustments.length).toEqual(1)
1616
1617 expect(cartAfterPromotion).toEqual(
1618 expect.objectContaining({
1619 items: [
1620 expect.objectContaining({
1621 adjustments: [
1622 expect.objectContaining({
1623 code: response.data.promotion.code,
1624 }),
1625 ],
1626 }),
1627 ],
1628 promotions: [
1629 expect.objectContaining({
1630 code: response.data.promotion.code,
1631 }),
1632 ],
1633 })
1634 )
1635 })
1636
1637 it.skip("should add two promotions and remove one from cart using delete", async () => {
1638 const publishableKey = await generatePublishableKey(appContainer)
1639 const storeHeaders = generateStoreHeaders({ publishableKey })
1640
1641 const salesChannel = (
1642 await api.post(
1643 "/admin/sales-channels",
1644 { name: "Webshop", description: "channel" },
1645 adminHeaders
1646 )
1647 ).data.sales_channel
1648
1649 const region = (
1650 await api.post(
1651 "/admin/regions",
1652 {
1653 payment_providers: ["pp_system_default"],
1654 name: "US",
1655 currency_code: "usd",
1656 countries: ["us"],
1657 },
1658 adminHeaders
1659 )
1660 ).data.region
1661
1662 const product = (
1663 await api.post(
1664 "/admin/products",
1665 {
1666 ...medusaTshirtProduct,
1667 shipping_profile_id: shippingProfile.id,
1668 },
1669 adminHeaders
1670 )
1671 ).data.product
1672
1673 const cart = (
1674 await api.post(
1675 `/store/carts`,
1676 {
1677 currency_code: "usd",
1678 sales_channel_id: salesChannel.id,
1679 region_id: region.id,
1680 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1681 },
1682 storeHeaders
1683 )
1684 ).data.cart
1685
1686 const promo1 = await api.post(
1687 `/admin/promotions`,
1688 {
1689 code: "TEST",
1690 type: PromotionType.STANDARD,
1691 status: PromotionStatus.ACTIVE,
1692 is_automatic: false,
1693 application_method: {
1694 target_type: "items",
1695 type: "fixed",
1696 allocation: "each",
1697 currency_code: "usd",
1698 value: 100,
1699 max_quantity: 100,
1700 },
1701 rules: [
1702 {
1703 attribute: "subtotal",
1704 operator: "gte",
1705 values: "1",
1706 },
1707 ],
1708 },
1709 adminHeaders
1710 )
1711 const promo2 = await api.post(
1712 `/admin/promotions`,
1713 {
1714 code: "TEST2",
1715 type: PromotionType.STANDARD,
1716 status: PromotionStatus.ACTIVE,
1717 is_automatic: false,
1718 application_method: {
1719 target_type: "items",
1720 type: "fixed",
1721 allocation: "each",
1722 currency_code: "usd",
1723 value: 100,
1724 max_quantity: 100,
1725 },
1726 rules: [
1727 {
1728 attribute: "subtotal",
1729 operator: "gte",
1730 values: "2000",
1731 },
1732 ],
1733 },
1734 adminHeaders
1735 )
1736 const cartWithPromotion2 = (
1737 await api.post(
1738 `/store/carts/${cart.id}/line-items`,
1739 { variant_id: product.variants[0].id, quantity: 40 },
1740 storeHeaders
1741 )
1742 ).data.cart
1743
1744 expect(cartWithPromotion2).toEqual(
1745 expect.objectContaining({
1746 items: [
1747 expect.objectContaining({
1748 adjustments: [
1749 expect.objectContaining({
1750 code: promo1.data.promotion.code,
1751 }),
1752 expect.objectContaining({
1753 code: promo2.data.promotion.code,
1754 }),
1755 ],
1756 }),
1757 ],
1758 promotions: [
1759 expect.objectContaining({
1760 code: promo1.data.promotion.code,
1761 }),
1762 expect.objectContaining({
1763 code: promo2.data.promotion.code,
1764 }),
1765 ],
1766 })
1767 )
1768
1769 await api.delete(`/store/carts/${cart.id}/promotions`, {
1770 data: {
1771 promo_codes: [promo1.data.promotion.code],
1772 },
1773 ...storeHeaders,
1774 })
1775
1776 const cartWithoutPromotion1 = (
1777 await api.get(`/store/carts/${cart.id}`, storeHeaders)
1778 ).data.cart
1779
1780 expect(cartWithoutPromotion1).toEqual(
1781 expect.objectContaining({
1782 items: [
1783 expect.objectContaining({
1784 adjustments: [
1785 expect.objectContaining({
1786 code: promo2.data.promotion.code,
1787 }),
1788 ],
1789 }),
1790 ],
1791 promotions: [
1792 expect.objectContaining({
1793 code: promo2.data.promotion.code,
1794 }),
1795 ],
1796 })
1797 )
1798 })
1799
1800 it.skip("should add two promotions and remove one from cart using update", async () => {
1801 const publishableKey = await generatePublishableKey(appContainer)
1802 const storeHeaders = generateStoreHeaders({ publishableKey })
1803
1804 const salesChannel = (
1805 await api.post(
1806 "/admin/sales-channels",
1807 { name: "Webshop", description: "channel" },
1808 adminHeaders
1809 )
1810 ).data.sales_channel
1811
1812 const region = (
1813 await api.post(
1814 "/admin/regions",
1815 {
1816 payment_providers: ["pp_system_default"],
1817 name: "US",
1818 currency_code: "usd",
1819 countries: ["us"],
1820 },
1821 adminHeaders
1822 )
1823 ).data.region
1824
1825 const product = (
1826 await api.post(
1827 "/admin/products",
1828 {
1829 ...medusaTshirtProduct,
1830 shipping_profile_id: shippingProfile.id,
1831 },
1832 adminHeaders
1833 )
1834 ).data.product
1835
1836 const cart = (
1837 await api.post(
1838 `/store/carts`,
1839 {
1840 currency_code: "usd",
1841 sales_channel_id: salesChannel.id,
1842 region_id: region.id,
1843 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1844 },
1845 storeHeaders
1846 )
1847 ).data.cart
1848
1849 const [promo1, promo2, promoAutomatic] = await Promise.all([
1850 api.post(
1851 `/admin/promotions`,
1852 {
1853 code: "TEST",
1854 type: PromotionType.STANDARD,
1855 status: PromotionStatus.ACTIVE,
1856 is_automatic: false,
1857 application_method: {
1858 target_type: "items",
1859 type: "fixed",
1860 allocation: "each",
1861 currency_code: "usd",
1862 value: 50,
1863 max_quantity: 100,
1864 },
1865 rules: [
1866 {
1867 attribute: "subtotal",
1868 operator: "gte",
1869 values: "1",
1870 },
1871 ],
1872 },
1873 adminHeaders
1874 ),
1875 api.post(
1876 `/admin/promotions`,
1877 {
1878 code: "TEST_CODE_123",
1879 type: PromotionType.STANDARD,
1880 status: PromotionStatus.ACTIVE,
1881 is_automatic: false,
1882 application_method: {
1883 target_type: "items",
1884 type: "fixed",
1885 allocation: "each",
1886 currency_code: "usd",
1887 value: 10,
1888 max_quantity: 100,
1889 },
1890 rules: [
1891 {
1892 attribute: "subtotal",
1893 operator: "gte",
1894 values: "2000",
1895 },
1896 ],
1897 },
1898 adminHeaders
1899 ),
1900 api.post(
1901 `/admin/promotions`,
1902 {
1903 code: "AUTOMATIC_PROMO",
1904 type: PromotionType.STANDARD,
1905 status: PromotionStatus.ACTIVE,
1906 is_automatic: true,
1907 application_method: {
1908 target_type: "items",
1909 type: "fixed",
1910 allocation: "each",
1911 currency_code: "usd",
1912 value: 5,
1913 max_quantity: 100,
1914 },
1915 rules: [
1916 {
1917 attribute: "subtotal",
1918 operator: "gte",
1919 values: "500",
1920 },
1921 ],
1922 },
1923 adminHeaders
1924 ),
1925 ])
1926
1927 // apply promotions
1928 await api.post(
1929 `/store/carts/${cart.id}`,
1930 {
1931 promo_codes: [
1932 promo1.data.promotion.code,
1933 promo2.data.promotion.code,
1934 ],
1935 },
1936 storeHeaders
1937 )
1938
1939 const cartWithPromotion2 = (
1940 await api.post(
1941 `/store/carts/${cart.id}/line-items`,
1942 {
1943 variant_id: product.variants[0].id,
1944 quantity: 40,
1945 },
1946 storeHeaders
1947 )
1948 ).data.cart
1949
1950 expect(cartWithPromotion2).toEqual(
1951 expect.objectContaining({
1952 items: [
1953 expect.objectContaining({
1954 adjustments: [
1955 expect.objectContaining({
1956 code: promo1.data.promotion.code,
1957 }),
1958 expect.objectContaining({
1959 code: promo2.data.promotion.code,
1960 }),
1961 expect.objectContaining({
1962 code: promoAutomatic.data.promotion.code,
1963 }),
1964 ],
1965 }),
1966 ],
1967 promotions: [
1968 expect.objectContaining({
1969 code: promo1.data.promotion.code,
1970 }),
1971 expect.objectContaining({
1972 code: promo2.data.promotion.code,
1973 }),
1974 expect.objectContaining({
1975 code: promoAutomatic.data.promotion.code,
1976 }),
1977 ],
1978 })
1979 )
1980
1981 await api.post(
1982 `/store/carts/${cart.id}`,
1983 {
1984 promo_codes: [promo2.data.promotion.code],
1985 },
1986 storeHeaders
1987 )
1988
1989 const cartWithoutPromotion1 = (
1990 await api.get(`/store/carts/${cart.id}`, storeHeaders)
1991 ).data.cart
1992
1993 expect(cartWithoutPromotion1).toEqual(
1994 expect.objectContaining({
1995 items: [
1996 expect.objectContaining({
1997 adjustments: [
1998 expect.objectContaining({
1999 code: promo2.data.promotion.code,
2000 }),
2001 expect.objectContaining({
2002 code: promoAutomatic.data.promotion.code,
2003 }),
2004 ],
2005 }),
2006 ],
2007 promotions: [
2008 expect.objectContaining({
2009 code: promo2.data.promotion.code,
2010 }),
2011 expect.objectContaining({
2012 code: promoAutomatic.data.promotion.code,
2013 }),
2014 ],
2015 })
2016 )
2017 })
2018 })
2019
2020 it("should add tax inclusive promotion to cart successfully in a tax inclusive currency", async () => {
2021 const publishableKey = await generatePublishableKey(appContainer)
2022 const storeHeaders = generateStoreHeaders({ publishableKey })
2023
2024 const salesChannel = (
2025 await api.post(
2026 "/admin/sales-channels",
2027 { name: "Webshop", description: "channel" },
2028 adminHeaders
2029 )
2030 ).data.sales_channel
2031
2032 await api.post(
2033 "/admin/price-preferences",
2034 {
2035 attribute: "currency_code",
2036 value: "dkk",
2037 is_tax_inclusive: true,
2038 },
2039 adminHeaders
2040 )
2041
2042 const region = (
2043 await api.post(
2044 "/admin/regions",
2045 {
2046 payment_providers: ["pp_system_default"],
2047 name: "DK",
2048 currency_code: "dkk",
2049 countries: ["dk"],
2050 },
2051 adminHeaders
2052 )
2053 ).data.region
2054
2055 const product = (
2056 await api.post("/admin/products", medusaTshirtProduct, adminHeaders)
2057 ).data.product
2058
2059 const response = await api.post(
2060 `/admin/promotions`,
2061 {
2062 code: "FIXED_10",
2063 type: PromotionType.STANDARD,
2064 status: PromotionStatus.ACTIVE,
2065 is_tax_inclusive: true,
2066 is_automatic: true,
2067 application_method: {
2068 target_type: "items",
2069 type: "fixed",
2070 allocation: "across",
2071 currency_code: "dkk",
2072 value: 100,
2073 },
2074 },
2075 adminHeaders
2076 )
2077
2078 expect(response.status).toEqual(200)
2079 expect(response.data.promotion).toEqual(
2080 expect.objectContaining({
2081 id: expect.any(String),
2082 code: "FIXED_10",
2083 type: "standard",
2084 is_tax_inclusive: true,
2085 is_automatic: true,
2086 application_method: expect.objectContaining({
2087 value: 100,
2088 type: "fixed",
2089 target_type: "items",
2090 allocation: "across",
2091 }),
2092 })
2093 )
2094
2095 const cart = (
2096 await api.post(
2097 `/store/carts?fields=*items,*items.adjustments`,
2098 {
2099 currency_code: "dkk",
2100 sales_channel_id: salesChannel.id,
2101 region_id: region.id,
2102 items: [
2103 {
2104 variant_id: product.variants[0].id,
2105 quantity: 1,
2106 },
2107 ],
2108 promo_codes: [response.data.promotion.code],
2109 },
2110 storeHeaders
2111 )
2112 ).data.cart
2113
2114 /**
2115 * Orignal total -> 1300 dkk (tax incl.)
2116 * Tax rate -> 25%
2117 * Promotion -> FIXED 100 dkk (tax incl.)
2118 *
2119 * We want total to be 1300 dkk - 100 dkk = 1200 dkk
2120 */
2121 expect(cart).toEqual(
2122 expect.objectContaining({
2123 currency_code: "dkk",
2124
2125 subtotal: 1040, // taxable base (item subtotal - discount subtotal) = 1040 - 80 = 960
2126 total: 1200, // total = taxable base * (1 + tax rate) = 960 * (1 + 0.25) = 1200
2127 tax_total: 240,
2128
2129 original_total: 1300,
2130 original_tax_total: 260,
2131
2132 discount_total: 100,
2133 discount_subtotal: 80,
2134 discount_tax_total: 20,
2135
2136 item_total: 1200,
2137 item_subtotal: 1040,
2138 item_tax_total: 240,
2139
2140 original_item_total: 1300,
2141 original_item_subtotal: 1040,
2142 original_item_tax_total: 260,
2143
2144 shipping_total: 0,
2145 shipping_subtotal: 0,
2146 shipping_tax_total: 0,
2147
2148 original_shipping_tax_total: 0,
2149 original_shipping_subtotal: 0,
2150 original_shipping_total: 0,
2151
2152 items: expect.arrayContaining([
2153 expect.objectContaining({
2154 quantity: 1,
2155 unit_price: 1300,
2156
2157 subtotal: 1040,
2158 tax_total: 240,
2159 total: 1200,
2160
2161 original_total: 1300,
2162 original_tax_total: 260,
2163
2164 discount_total: 100,
2165 discount_subtotal: 80,
2166 discount_tax_total: 20,
2167
2168 adjustments: expect.arrayContaining([
2169 expect.objectContaining({
2170 amount: 100,
2171 is_tax_inclusive: true,
2172 provider_id: null,
2173 code: "FIXED_10",
2174 }),
2175 ]),
2176 }),
2177 ]),
2178 })
2179 )
2180
2181 const paymentCollection = (
2182 await api.post(
2183 `/store/payment-collections`,
2184 { cart_id: cart.id },
2185 storeHeaders
2186 )
2187 ).data.payment_collection
2188
2189 await api.post(
2190 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
2191 { provider_id: "pp_system_default" },
2192 storeHeaders
2193 )
2194
2195 const order = (
2196 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
2197 ).data.order
2198
2199 /**
2200 * Orignal total -> 1300 dkk (tax incl.)
2201 * Tax rate -> 25%
2202 * Promotion -> FIXED 100 dkk (tax incl.)
2203 *
2204 * We want total to be 1300 dkk - 100 dkk = 1200 dkk
2205 */
2206 expect(order).toEqual(
2207 expect.objectContaining({
2208 currency_code: "dkk",
2209
2210 subtotal: 1040, // taxable base (item subtotal - discount subtotal) = 1040 - 80 = 960
2211 total: 1200, // total = taxable base * (1 + tax rate) = 960 * (1 + 0.25) = 1200
2212 tax_total: 240,
2213
2214 original_total: 1300,
2215 original_tax_total: 260,
2216
2217 discount_total: 100,
2218 discount_subtotal: 80,
2219 discount_tax_total: 20,
2220
2221 item_total: 1200,
2222 item_subtotal: 1040,
2223 item_tax_total: 240,
2224
2225 original_item_total: 1300,
2226 original_item_subtotal: 1040,
2227 original_item_tax_total: 260,
2228
2229 shipping_total: 0,
2230 shipping_subtotal: 0,
2231 shipping_tax_total: 0,
2232
2233 original_shipping_tax_total: 0,
2234 original_shipping_subtotal: 0,
2235 original_shipping_total: 0,
2236
2237 items: expect.arrayContaining([
2238 expect.objectContaining({
2239 quantity: 1,
2240 unit_price: 1300,
2241
2242 subtotal: 1040,
2243 tax_total: 240,
2244 total: 1200,
2245
2246 original_total: 1300,
2247 original_tax_total: 260,
2248
2249 discount_total: 100,
2250 discount_subtotal: 80,
2251 discount_tax_total: 20,
2252
2253 adjustments: expect.arrayContaining([
2254 expect.objectContaining({
2255 amount: 100,
2256 is_tax_inclusive: true,
2257 provider_id: null,
2258 code: "FIXED_10",
2259 }),
2260 ]),
2261 }),
2262 ]),
2263 })
2264 )
2265 })
2266
2267 it("should add tax inclusive promotion to cart successfully in a tax inclusive currency with 2 items and each allocation", async () => {
2268 const publishableKey = await generatePublishableKey(appContainer)
2269 const storeHeaders = generateStoreHeaders({ publishableKey })
2270
2271 const salesChannel = (
2272 await api.post(
2273 "/admin/sales-channels",
2274 { name: "Webshop", description: "channel" },
2275 adminHeaders
2276 )
2277 ).data.sales_channel
2278
2279 await api.post(
2280 "/admin/price-preferences",
2281 {
2282 attribute: "currency_code",
2283 value: "dkk",
2284 is_tax_inclusive: true,
2285 },
2286 adminHeaders
2287 )
2288
2289 const region = (
2290 await api.post(
2291 "/admin/regions",
2292 {
2293 payment_providers: ["pp_system_default"],
2294 name: "DK",
2295 currency_code: "dkk",
2296 countries: ["dk"],
2297 },
2298 adminHeaders
2299 )
2300 ).data.region
2301
2302 const product = (
2303 await api.post(
2304 "/admin/products",
2305 {
2306 title: "Discounted Medusa T-Shirt",
2307 status: ProductStatus.PUBLISHED,
2308 handle: "discounted-medusa-t-shirt",
2309 options: [
2310 {
2311 title: "Size",
2312 values: ["S", "M"],
2313 },
2314 ],
2315 variants: [
2316 {
2317 title: "S",
2318 sku: "SHIRT-S",
2319 options: {
2320 Size: "S",
2321 },
2322 manage_inventory: false,
2323 prices: [
2324 {
2325 amount: 1000,
2326 currency_code: "dkk",
2327 },
2328 ],
2329 },
2330 {
2331 title: "M",
2332 sku: "SHIRT-M",
2333 options: {
2334 Size: "S",
2335 },
2336 manage_inventory: false,
2337 prices: [
2338 {
2339 amount: 500,
2340 currency_code: "dkk",
2341 },
2342 ],
2343 },
2344 ],
2345 },
2346 adminHeaders
2347 )
2348 ).data.product
2349
2350 const response = await api.post(
2351 `/admin/promotions`,
2352 {
2353 code: "FIXED_10",
2354 type: PromotionType.STANDARD,
2355 status: PromotionStatus.ACTIVE,
2356 is_tax_inclusive: true,
2357 is_automatic: true,
2358 application_method: {
2359 target_type: "items",
2360 type: "fixed",
2361 allocation: "each",
2362 currency_code: "dkk",
2363 value: 100,
2364 max_quantity: 2,
2365 },
2366 },
2367 adminHeaders
2368 )
2369
2370 expect(response.status).toEqual(200)
2371 expect(response.data.promotion).toEqual(
2372 expect.objectContaining({
2373 id: expect.any(String),
2374 code: "FIXED_10",
2375 type: "standard",
2376 is_tax_inclusive: true,
2377 is_automatic: true,
2378 application_method: expect.objectContaining({
2379 value: 100,
2380 type: "fixed",
2381 target_type: "items",
2382 allocation: "each",
2383 max_quantity: 2,
2384 }),
2385 })
2386 )
2387
2388 const cart = (
2389 await api.post(
2390 `/store/carts?fields=*items,*items.adjustments`,
2391 {
2392 currency_code: "dkk",
2393 sales_channel_id: salesChannel.id,
2394 region_id: region.id,
2395 items: [
2396 {
2397 variant_id: product.variants[0].id,
2398 quantity: 1,
2399 },
2400 {
2401 variant_id: product.variants[1].id,
2402 quantity: 1,
2403 },
2404 ],
2405 promo_codes: [response.data.promotion.code],
2406 },
2407 storeHeaders
2408 )
2409 ).data.cart
2410
2411 /**
2412 * Orignal total -> 1500 dkk (tax incl.)
2413 * Promotion -> FIXED 100 dkk per item (tax incl.)
2414 * Tax rate -> 25%
2415 *
2416 * We want total to be 1500 dkk - 100 dkk - 100 dkk = 1300 dkk
2417 */
2418 expect(cart).toEqual(
2419 expect.objectContaining({
2420 currency_code: "dkk",
2421
2422 total: 1300,
2423 subtotal: 1200, // taxable base (item subtotal - discount subtotal) = 1200 - 200 = 1000
2424 tax_total: 260,
2425
2426 discount_total: 200, // 2 * 100 dkk fixed tax inclusive
2427 discount_subtotal: 160,
2428 discount_tax_total: 40,
2429
2430 original_total: 1500,
2431 original_tax_total: 300,
2432
2433 item_total: 1300,
2434 item_subtotal: 1200,
2435 item_tax_total: 260,
2436
2437 original_item_total: 1500,
2438 original_item_subtotal: 1200,
2439 original_item_tax_total: 300,
2440
2441 shipping_total: 0,
2442 shipping_subtotal: 0,
2443 shipping_tax_total: 0,
2444
2445 original_shipping_tax_total: 0,
2446 original_shipping_subtotal: 0,
2447 original_shipping_total: 0,
2448
2449 items: expect.arrayContaining([
2450 expect.objectContaining({
2451 quantity: 1,
2452 unit_price: 500,
2453
2454 subtotal: 400,
2455 total: 400, // 400 - 80 = 320 -> 320 * 1.25 = 400
2456 tax_total: 80,
2457
2458 original_total: 500,
2459 original_tax_total: 100,
2460
2461 discount_total: 100,
2462 discount_subtotal: 80,
2463 discount_tax_total: 20,
2464
2465 adjustments: expect.arrayContaining([
2466 expect.objectContaining({
2467 amount: 100,
2468 is_tax_inclusive: true,
2469 }),
2470 ]),
2471 }),
2472 expect.objectContaining({
2473 quantity: 1,
2474 unit_price: 1000,
2475
2476 subtotal: 800, // 800 - 80 = 720 -> 720 * 1.25 = 900
2477 total: 900,
2478 tax_total: 180,
2479
2480 original_total: 1000,
2481 original_tax_total: 200,
2482
2483 discount_total: 100,
2484 discount_subtotal: 80,
2485 discount_tax_total: 20,
2486
2487 adjustments: expect.arrayContaining([
2488 expect.objectContaining({
2489 amount: 100,
2490 is_tax_inclusive: true,
2491 }),
2492 ]),
2493 }),
2494 ]),
2495 })
2496 )
2497
2498 const paymentCollection = (
2499 await api.post(
2500 `/store/payment-collections`,
2501 { cart_id: cart.id },
2502 storeHeaders
2503 )
2504 ).data.payment_collection
2505
2506 await api.post(
2507 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
2508 { provider_id: "pp_system_default" },
2509 storeHeaders
2510 )
2511
2512 const order = (
2513 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
2514 ).data.order
2515
2516 /**
2517 * Orignal total -> 1500 dkk (tax incl.)
2518 * Promotion -> FIXED 100 dkk per item (tax incl.)
2519 * Tax rate -> 25%
2520 *
2521 * We want total to be 1500 dkk - 100 dkk - 100 dkk = 1300 dkk
2522 */
2523 expect(order).toEqual(
2524 expect.objectContaining({
2525 currency_code: "dkk",
2526
2527 total: 1300,
2528 subtotal: 1200, // taxable base (item subtotal - discount subtotal) = 1200 - 200 = 1000
2529 tax_total: 260,
2530
2531 discount_total: 200, // 2 * 100 dkk fixed tax inclusive
2532 discount_subtotal: 160,
2533 discount_tax_total: 40,
2534
2535 original_total: 1500,
2536 original_tax_total: 300,
2537
2538 item_total: 1300,
2539 item_subtotal: 1200,
2540 item_tax_total: 260,
2541
2542 original_item_total: 1500,
2543 original_item_subtotal: 1200,
2544 original_item_tax_total: 300,
2545
2546 shipping_total: 0,
2547 shipping_subtotal: 0,
2548 shipping_tax_total: 0,
2549
2550 original_shipping_tax_total: 0,
2551 original_shipping_subtotal: 0,
2552 original_shipping_total: 0,
2553
2554 items: expect.arrayContaining([
2555 expect.objectContaining({
2556 quantity: 1,
2557 unit_price: 500,
2558
2559 subtotal: 400,
2560 total: 400, // 400 - 80 = 320 -> 320 * 1.25 = 400
2561 tax_total: 80,
2562
2563 original_total: 500,
2564 original_tax_total: 100,
2565
2566 discount_total: 100,
2567 discount_subtotal: 80,
2568 discount_tax_total: 20,
2569
2570 adjustments: expect.arrayContaining([
2571 expect.objectContaining({
2572 amount: 100,
2573 is_tax_inclusive: true,
2574 }),
2575 ]),
2576 }),
2577 expect.objectContaining({
2578 quantity: 1,
2579 unit_price: 1000,
2580
2581 subtotal: 800, // 800 - 80 = 720 -> 720 * 1.25 = 900
2582 total: 900,
2583 tax_total: 180,
2584
2585 original_total: 1000,
2586 original_tax_total: 200,
2587
2588 discount_total: 100,
2589 discount_subtotal: 80,
2590 discount_tax_total: 20,
2591
2592 adjustments: expect.arrayContaining([
2593 expect.objectContaining({
2594 amount: 100,
2595 is_tax_inclusive: true,
2596 }),
2597 ]),
2598 }),
2599 ]),
2600 })
2601 )
2602 })
2603
2604 it("should add tax exclusive promotion to cart successfully for tax inclusive currency", async () => {
2605 const publishableKey = await generatePublishableKey(appContainer)
2606 const storeHeaders = generateStoreHeaders({ publishableKey })
2607
2608 const salesChannel = (
2609 await api.post(
2610 "/admin/sales-channels",
2611 { name: "Webshop", description: "channel" },
2612 adminHeaders
2613 )
2614 ).data.sales_channel
2615
2616 await api.post(
2617 "/admin/price-preferences",
2618 {
2619 attribute: "currency_code",
2620 value: "dkk",
2621 is_tax_inclusive: true,
2622 },
2623 adminHeaders
2624 )
2625
2626 const region = (
2627 await api.post(
2628 "/admin/regions",
2629 {
2630 payment_providers: ["pp_system_default"],
2631 name: "DK",
2632 currency_code: "dkk",
2633 countries: ["dk"],
2634 },
2635 adminHeaders
2636 )
2637 ).data.region
2638
2639 const product = (
2640 await api.post("/admin/products", medusaTshirtProduct, adminHeaders)
2641 ).data.product
2642
2643 const response = await api.post(
2644 `/admin/promotions`,
2645 {
2646 code: "FIXED_10",
2647 type: PromotionType.STANDARD,
2648 status: PromotionStatus.ACTIVE,
2649 is_automatic: true,
2650 application_method: {
2651 target_type: "items",
2652 type: "fixed",
2653 allocation: "across",
2654 currency_code: "dkk",
2655 value: 100,
2656 },
2657 },
2658 adminHeaders
2659 )
2660
2661 expect(response.status).toEqual(200)
2662 expect(response.data.promotion).toEqual(
2663 expect.objectContaining({
2664 id: expect.any(String),
2665 code: "FIXED_10",
2666 type: "standard",
2667 is_tax_inclusive: false, // tax exclusive by default
2668 is_automatic: true,
2669 application_method: expect.objectContaining({
2670 value: 100,
2671 type: "fixed",
2672 target_type: "items",
2673 allocation: "across",
2674 }),
2675 })
2676 )
2677
2678 const cart = (
2679 await api.post(
2680 `/store/carts?fields=*items,*items.adjustments`,
2681 {
2682 currency_code: "dkk",
2683 sales_channel_id: salesChannel.id,
2684 region_id: region.id,
2685 items: [
2686 {
2687 variant_id: product.variants[0].id,
2688 quantity: 1,
2689 },
2690 ],
2691 promo_codes: [response.data.promotion.code],
2692 },
2693 storeHeaders
2694 )
2695 ).data.cart
2696
2697 /**
2698 * Orignal total -> 1300 dkk (tax incl.)
2699 * Tax rate -> 25%
2700 * Promotion -> FIXED 100 dkk (tax exclusive !)
2701 */
2702 expect(cart).toEqual(
2703 expect.objectContaining({
2704 currency_code: "dkk",
2705
2706 subtotal: 1040, // taxable base (item subtotal - discount subtotal) = 1040 - 100 = 940
2707 total: 1175, // total = taxable base * (1 + tax rate) = 940 * (1 + 0.25) = 1175
2708 tax_total: 235,
2709
2710 original_total: 1300,
2711 original_tax_total: 260,
2712
2713 discount_total: 125,
2714 discount_subtotal: 100,
2715 discount_tax_total: 25,
2716
2717 item_total: 1175,
2718 item_subtotal: 1040,
2719 item_tax_total: 235,
2720
2721 original_item_total: 1300,
2722 original_item_subtotal: 1040,
2723 original_item_tax_total: 260,
2724
2725 shipping_total: 0,
2726 shipping_subtotal: 0,
2727 shipping_tax_total: 0,
2728
2729 original_shipping_tax_total: 0,
2730 original_shipping_subtotal: 0,
2731 original_shipping_total: 0,
2732
2733 items: expect.arrayContaining([
2734 expect.objectContaining({
2735 quantity: 1,
2736 unit_price: 1300,
2737
2738 subtotal: 1040,
2739 tax_total: 235,
2740 total: 1175,
2741
2742 original_total: 1300,
2743 original_tax_total: 260,
2744
2745 discount_total: 125,
2746 discount_subtotal: 100,
2747 discount_tax_total: 25,
2748
2749 adjustments: expect.arrayContaining([
2750 expect.objectContaining({
2751 amount: 100,
2752 }),
2753 ]),
2754 }),
2755 ]),
2756 })
2757 )
2758
2759 const paymentCollection = (
2760 await api.post(
2761 `/store/payment-collections`,
2762 { cart_id: cart.id },
2763 storeHeaders
2764 )
2765 ).data.payment_collection
2766
2767 await api.post(
2768 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
2769 { provider_id: "pp_system_default" },
2770 storeHeaders
2771 )
2772
2773 const order = (
2774 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
2775 ).data.order
2776
2777 /**
2778 * Orignal total -> 1300 dkk (tax incl.)
2779 * Tax rate -> 25%
2780 * Promotion -> FIXED 100 dkk (tax exclusive !)
2781 */
2782 expect(order).toEqual(
2783 expect.objectContaining({
2784 currency_code: "dkk",
2785
2786 subtotal: 1040, // taxable base (item subtotal - discount subtotal) = 1040 - 100 = 940
2787 total: 1175, // total = taxable base * (1 + tax rate) = 940 * (1 + 0.25) = 1175
2788 tax_total: 235,
2789
2790 original_total: 1300,
2791 original_tax_total: 260,
2792
2793 discount_total: 125,
2794 discount_subtotal: 100,
2795 discount_tax_total: 25,
2796
2797 item_total: 1175,
2798 item_subtotal: 1040,
2799 item_tax_total: 235,
2800
2801 original_item_total: 1300,
2802 original_item_subtotal: 1040,
2803 original_item_tax_total: 260,
2804
2805 shipping_total: 0,
2806 shipping_subtotal: 0,
2807 shipping_tax_total: 0,
2808
2809 original_shipping_tax_total: 0,
2810 original_shipping_subtotal: 0,
2811 original_shipping_total: 0,
2812
2813 items: expect.arrayContaining([
2814 expect.objectContaining({
2815 quantity: 1,
2816 unit_price: 1300,
2817
2818 subtotal: 1040,
2819 tax_total: 235,
2820 total: 1175,
2821
2822 original_total: 1300,
2823 original_tax_total: 260,
2824
2825 discount_total: 125,
2826 discount_subtotal: 100,
2827 discount_tax_total: 25,
2828
2829 adjustments: expect.arrayContaining([
2830 expect.objectContaining({
2831 amount: 100,
2832 }),
2833 ]),
2834 }),
2835 ]),
2836 })
2837 )
2838 })
2839
2840 it("should add tax exclusive promotion to cart successfully for tax exclusive currency", async () => {
2841 const publishableKey = await generatePublishableKey(appContainer)
2842 const storeHeaders = generateStoreHeaders({ publishableKey })
2843
2844 const salesChannel = (
2845 await api.post(
2846 "/admin/sales-channels",
2847 { name: "Webshop", description: "channel" },
2848 adminHeaders
2849 )
2850 ).data.sales_channel
2851
2852 await api.post(
2853 "/admin/price-preferences",
2854 {
2855 attribute: "currency_code",
2856 value: "dkk",
2857 is_tax_inclusive: false,
2858 },
2859 adminHeaders
2860 )
2861
2862 const region = (
2863 await api.post(
2864 "/admin/regions",
2865 {
2866 payment_providers: ["pp_system_default"],
2867 name: "DK",
2868 currency_code: "dkk",
2869 countries: ["dk"],
2870 },
2871 adminHeaders
2872 )
2873 ).data.region
2874
2875 const product = (
2876 await api.post("/admin/products", medusaTshirtProduct, adminHeaders)
2877 ).data.product
2878
2879 const response = await api.post(
2880 `/admin/promotions`,
2881 {
2882 code: "FIXED_10",
2883 type: PromotionType.STANDARD,
2884 status: PromotionStatus.ACTIVE,
2885 is_automatic: true,
2886 application_method: {
2887 target_type: "items",
2888 type: "fixed",
2889 allocation: "across",
2890 currency_code: "dkk",
2891 value: 100,
2892 },
2893 },
2894 adminHeaders
2895 )
2896
2897 expect(response.status).toEqual(200)
2898 expect(response.data.promotion).toEqual(
2899 expect.objectContaining({
2900 id: expect.any(String),
2901 code: "FIXED_10",
2902 type: "standard",
2903 is_tax_inclusive: false, // tax exclusive by default
2904 is_automatic: true,
2905 application_method: expect.objectContaining({
2906 value: 100,
2907 type: "fixed",
2908 target_type: "items",
2909 allocation: "across",
2910 }),
2911 })
2912 )
2913
2914 const cart = (
2915 await api.post(
2916 `/store/carts?fields=*items,*items.adjustments`,
2917 {
2918 currency_code: "dkk",
2919 sales_channel_id: salesChannel.id,
2920 region_id: region.id,
2921 items: [
2922 {
2923 variant_id: product.variants[0].id,
2924 quantity: 1,
2925 },
2926 ],
2927 promo_codes: [response.data.promotion.code],
2928 },
2929 storeHeaders
2930 )
2931 ).data.cart
2932
2933 /**
2934 * Orignal total -> 1300 dkk (tax excl.)
2935 * Tax rate -> 25%
2936 * Promotion -> FIXED 100 dkk (tax exclusive !)
2937 */
2938 expect(cart).toEqual(
2939 expect.objectContaining({
2940 currency_code: "dkk",
2941
2942 subtotal: 1300, // taxable base (item subtotal - discount subtotal) = 1300 - 100 = 1200
2943 total: 1500, // total = taxable base * (1 + tax rate) = 1200 * (1 + 0.25) = 1500
2944 tax_total: 300,
2945
2946 original_total: 1625,
2947 original_tax_total: 325,
2948
2949 discount_total: 125,
2950 discount_subtotal: 100,
2951 discount_tax_total: 25,
2952
2953 item_total: 1500,
2954 item_subtotal: 1300,
2955 item_tax_total: 300,
2956
2957 original_item_total: 1625,
2958 original_item_subtotal: 1300,
2959 original_item_tax_total: 325,
2960
2961 shipping_total: 0,
2962 shipping_subtotal: 0,
2963 shipping_tax_total: 0,
2964
2965 original_shipping_tax_total: 0,
2966 original_shipping_subtotal: 0,
2967 original_shipping_total: 0,
2968
2969 items: expect.arrayContaining([
2970 expect.objectContaining({
2971 quantity: 1,
2972 unit_price: 1300,
2973
2974 subtotal: 1300,
2975 total: 1500,
2976 tax_total: 300,
2977
2978 discount_total: 125,
2979 discount_subtotal: 100,
2980 discount_tax_total: 25,
2981
2982 original_total: 1625,
2983 original_tax_total: 325,
2984
2985 adjustments: expect.arrayContaining([
2986 expect.objectContaining({
2987 amount: 100,
2988 }),
2989 ]),
2990 }),
2991 ]),
2992 })
2993 )
2994
2995 const paymentCollection = (
2996 await api.post(
2997 `/store/payment-collections`,
2998 { cart_id: cart.id },
2999 storeHeaders
3000 )
3001 ).data.payment_collection
3002
3003 await api.post(
3004 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3005 { provider_id: "pp_system_default" },
3006 storeHeaders
3007 )
3008
3009 const order = (
3010 await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
3011 ).data.order
3012
3013 /**
3014 * Orignal total -> 1300 dkk (tax excl.)
3015 * Tax rate -> 25%
3016 * Promotion -> FIXED 100 dkk (tax exclusive !)
3017 */
3018 expect(order).toEqual(
3019 expect.objectContaining({
3020 currency_code: "dkk",
3021
3022 subtotal: 1300, // taxable base (item subtotal - discount subtotal) = 1300 - 100 = 1200
3023 total: 1500, // total = taxable base * (1 + tax rate) = 1200 * (1 + 0.25) = 1500
3024 tax_total: 300,
3025
3026 original_total: 1625,
3027 original_tax_total: 325,
3028
3029 discount_total: 125,
3030 discount_subtotal: 100,
3031 discount_tax_total: 25,
3032
3033 item_total: 1500,
3034 item_subtotal: 1300,
3035 item_tax_total: 300,
3036
3037 original_item_total: 1625,
3038 original_item_subtotal: 1300,
3039 original_item_tax_total: 325,
3040
3041 shipping_total: 0,
3042 shipping_subtotal: 0,
3043 shipping_tax_total: 0,
3044
3045 original_shipping_tax_total: 0,
3046 original_shipping_subtotal: 0,
3047 original_shipping_total: 0,
3048
3049 items: expect.arrayContaining([
3050 expect.objectContaining({
3051 quantity: 1,
3052 unit_price: 1300,
3053
3054 subtotal: 1300,
3055 total: 1500,
3056 tax_total: 300,
3057
3058 discount_total: 125,
3059 discount_subtotal: 100,
3060 discount_tax_total: 25,
3061
3062 original_total: 1625,
3063 original_tax_total: 325,
3064
3065 adjustments: expect.arrayContaining([
3066 expect.objectContaining({
3067 amount: 100,
3068 }),
3069 ]),
3070 }),
3071 ]),
3072 })
3073 )
3074 })
3075 })
3076
3077 describe("DELETE /admin/promotions/:id", () => {
3078 it("should delete promotion successfully", async () => {
3079 const deleteRes = await api.delete(
3080 `/admin/promotions/${promotion.id}`,
3081 adminHeaders
3082 )
3083
3084 expect(deleteRes.status).toEqual(200)
3085
3086 const { response } = await api
3087 .get(`/admin/promotions/${promotion.id}`, adminHeaders)
3088 .catch((e) => e)
3089
3090 expect(response.status).toEqual(404)
3091 expect(response.data.message).toEqual(
3092 `Promotion with id or code: ${promotion.id} was not found`
3093 )
3094 })
3095 })
3096
3097 describe("POST /admin/promotions/:id", () => {
3098 it("should throw an error if id does not exist", async () => {
3099 const { response } = await api
3100 .post(
3101 `/admin/promotions/does-not-exist`,
3102 { type: PromotionType.STANDARD },
3103 adminHeaders
3104 )
3105 .catch((e) => e)
3106
3107 expect(response.status).toEqual(404)
3108 expect(response.data.message).toEqual(
3109 `Promotion id not found: does-not-exist`
3110 )
3111 })
3112
3113 it("should update a promotion successfully", async () => {
3114 const response = await api.post(
3115 `/admin/promotions/${promotion.id}`,
3116 {
3117 code: "TEST_TWO",
3118 application_method: { value: 200 },
3119 is_tax_inclusive: true,
3120 },
3121 adminHeaders
3122 )
3123
3124 expect(response.status).toEqual(200)
3125 expect(response.data.promotion).toEqual(
3126 expect.objectContaining({
3127 id: expect.any(String),
3128 code: "TEST_TWO",
3129 application_method: expect.objectContaining({
3130 value: 200,
3131 }),
3132 is_tax_inclusive: true,
3133 })
3134 )
3135 })
3136
3137 it("should update a buyget promotion successfully", async () => {
3138 const response = await api.post(
3139 `/admin/promotions/${promotion.id}`,
3140 {
3141 code: "TEST_TWO",
3142 application_method: {
3143 value: 200,
3144 buy_rules_min_quantity: 6,
3145 },
3146 },
3147 adminHeaders
3148 )
3149
3150 expect(response.status).toEqual(200)
3151 expect(response.data.promotion).toEqual(
3152 expect.objectContaining({
3153 id: promotion.id,
3154 code: "TEST_TWO",
3155 application_method: expect.objectContaining({
3156 value: 200,
3157 buy_rules_min_quantity: 6,
3158 }),
3159 })
3160 )
3161 })
3162
3163 it("should update a buyget promotion when type is passed in the payload", async () => {
3164 const buygetPromotion = (
3165 await api.post(
3166 `/admin/promotions`,
3167 {
3168 code: "BUYGET_UPDATE",
3169 type: PromotionType.BUYGET,
3170 status: PromotionStatus.ACTIVE,
3171 application_method: {
3172 target_type: "items",
3173 type: "fixed",
3174 allocation: "each",
3175 value: 100,
3176 max_quantity: 100,
3177 apply_to_quantity: 1,
3178 buy_rules_min_quantity: 1,
3179 currency_code: "usd",
3180 target_rules: [promotionRule],
3181 buy_rules: [promotionRule],
3182 },
3183 },
3184 adminHeaders
3185 )
3186 ).data.promotion
3187
3188 // Buy rules already exist on the persisted promotion, so a partial
3189 // update that echoes back the type should not require them again.
3190 const response = await api.post(
3191 `/admin/promotions/${buygetPromotion.id}`,
3192 {
3193 type: PromotionType.BUYGET,
3194 status: PromotionStatus.INACTIVE,
3195 },
3196 adminHeaders
3197 )
3198
3199 expect(response.status).toEqual(200)
3200 expect(response.data.promotion).toEqual(
3201 expect.objectContaining({
3202 id: buygetPromotion.id,
3203 type: "buyget",
3204 status: "inactive",
3205 })
3206 )
3207 })
3208 })
3209
3210 describe("POST /admin/promotions/:id/rules/batch", () => {
3211 it("should throw error when required params are missing", async () => {
3212 const { response } = await api
3213 .post(
3214 `/admin/promotions/${standardPromotion.id}/rules/batch`,
3215 {
3216 create: [
3217 {
3218 operator: "eq",
3219 values: ["new value"],
3220 },
3221 ],
3222 },
3223 adminHeaders
3224 )
3225 .catch((e) => e)
3226
3227 expect(response.status).toEqual(400)
3228 // expect(response.data).toEqual({
3229 // type: "invalid_data",
3230 // message:
3231 // "attribute must be a string, attribute should not be empty",
3232 // })
3233 })
3234
3235 it("should throw error when promotion does not exist", async () => {
3236 const { response } = await api
3237 .post(
3238 `/admin/promotions/does-not-exist/rules/batch`,
3239 {
3240 create: [
3241 {
3242 attribute: "new_attr",
3243 operator: "eq",
3244 values: ["new value"],
3245 },
3246 ],
3247 },
3248 adminHeaders
3249 )
3250 .catch((e) => e)
3251
3252 expect(response.status).toEqual(404)
3253 expect(response.data).toEqual({
3254 type: "not_found",
3255 message: "Promotion with id: does-not-exist was not found",
3256 })
3257 })
3258
3259 it("should add rules to a promotion successfully", async () => {
3260 const response = await api.post(
3261 `/admin/promotions/${standardPromotion.id}/rules/batch`,
3262 {
3263 create: [
3264 {
3265 operator: "eq",
3266 attribute: "new_attr",
3267 values: ["new value"],
3268 },
3269 ],
3270 },
3271 adminHeaders
3272 )
3273
3274 expect(response.status).toEqual(200)
3275
3276 const promotion = (
3277 await api.get(
3278 `/admin/promotions/${standardPromotion.id}?fields=*rules`,
3279 adminHeaders
3280 )
3281 ).data.promotion
3282
3283 expect(promotion).toEqual(
3284 expect.objectContaining({
3285 id: standardPromotion.id,
3286 rules: expect.arrayContaining([
3287 expect.objectContaining({
3288 operator: "eq",
3289 attribute: "old_attr",
3290 values: [expect.objectContaining({ value: "old value" })],
3291 }),
3292 expect.objectContaining({
3293 operator: "eq",
3294 attribute: "new_attr",
3295 values: [expect.objectContaining({ value: "new value" })],
3296 }),
3297 ]),
3298 })
3299 )
3300 })
3301 })
3302
3303 describe("POST /admin/promotions/:id/target-rules/batch", () => {
3304 it("should throw error when required params are missing", async () => {
3305 const { response } = await api
3306 .post(
3307 `/admin/promotions/${standardPromotion.id}/target-rules/batch`,
3308 {
3309 create: [
3310 {
3311 operator: "eq",
3312 values: ["new value"],
3313 },
3314 ],
3315 },
3316 adminHeaders
3317 )
3318 .catch((e) => e)
3319
3320 expect(response.status).toEqual(400)
3321 // expect(response.data).toEqual({
3322 // type: "invalid_data",
3323 // message:
3324 // "attribute must be a string, attribute should not be empty",
3325 // })
3326 })
3327
3328 it("should throw error when promotion does not exist", async () => {
3329 const { response } = await api
3330 .post(
3331 `/admin/promotions/does-not-exist/target-rules/batch`,
3332 {
3333 create: [
3334 {
3335 attribute: "new_attr",
3336 operator: "eq",
3337 values: ["new value"],
3338 },
3339 ],
3340 },
3341 adminHeaders
3342 )
3343 .catch((e) => e)
3344
3345 expect(response.status).toEqual(404)
3346 expect(response.data).toEqual({
3347 type: "not_found",
3348 message: "Promotion with id: does-not-exist was not found",
3349 })
3350 })
3351
3352 it("should add target rules to a promotion successfully", async () => {
3353 const response = await api.post(
3354 `/admin/promotions/${standardPromotion.id}/target-rules/batch`,
3355 {
3356 create: [
3357 {
3358 operator: "eq",
3359 attribute: "new_attr",
3360 values: ["new value"],
3361 },
3362 ],
3363 },
3364 adminHeaders
3365 )
3366
3367 expect(response.status).toEqual(200)
3368
3369 const promotion = (
3370 await api.get(
3371 `/admin/promotions/${standardPromotion.id}`,
3372 adminHeaders
3373 )
3374 ).data.promotion
3375
3376 expect(promotion).toEqual(
3377 expect.objectContaining({
3378 id: standardPromotion.id,
3379 application_method: expect.objectContaining({
3380 target_rules: expect.arrayContaining([
3381 expect.objectContaining({
3382 operator: "eq",
3383 attribute: "old_attr",
3384 values: [expect.objectContaining({ value: "old value" })],
3385 }),
3386 expect.objectContaining({
3387 operator: "eq",
3388 attribute: "new_attr",
3389 values: [expect.objectContaining({ value: "new value" })],
3390 }),
3391 ]),
3392 }),
3393 })
3394 )
3395 })
3396 })
3397
3398 describe("POST /admin/promotions/:id/buy-rules/batch", () => {
3399 it("should throw error when required params are missing", async () => {
3400 const { response } = await api
3401 .post(
3402 `/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
3403 {
3404 create: [
3405 {
3406 operator: "eq",
3407 values: ["new value"],
3408 },
3409 ],
3410 },
3411 adminHeaders
3412 )
3413 .catch((e) => e)
3414
3415 expect(response.status).toEqual(400)
3416 // expect(response.data).toEqual({
3417 // type: "invalid_data",
3418 // message:
3419 // "attribute must be a string, attribute should not be empty",
3420 // })
3421 })
3422
3423 it("should throw error when promotion does not exist", async () => {
3424 const { response } = await api
3425 .post(
3426 `/admin/promotions/does-not-exist/buy-rules/batch`,
3427 {
3428 create: [
3429 {
3430 attribute: "new_attr",
3431 operator: "eq",
3432 values: ["new value"],
3433 },
3434 ],
3435 },
3436 adminHeaders
3437 )
3438 .catch((e) => e)
3439
3440 expect(response.status).toEqual(404)
3441 expect(response.data).toEqual({
3442 type: "not_found",
3443 message: "Promotion with id: does-not-exist was not found",
3444 })
3445 })
3446
3447 it("should throw an error when trying to add buy rules to a standard promotion", async () => {
3448 const { response } = await api
3449 .post(
3450 `/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
3451 {
3452 create: [
3453 {
3454 operator: "eq",
3455 attribute: "new_attr",
3456 values: ["new value"],
3457 },
3458 ],
3459 },
3460 adminHeaders
3461 )
3462 .catch((e) => e)
3463
3464 expect(response.status).toEqual(400)
3465 expect(response.data).toEqual({
3466 type: "invalid_data",
3467 message: "Can't add buy rules to a standard promotion",
3468 })
3469 })
3470
3471 it("should add buy rules to a buyget promotion successfully", async () => {
3472 const buyGetPromotion = (
3473 await api.post(
3474 `/admin/promotions`,
3475 {
3476 code: "TEST_BUYGET",
3477 type: PromotionType.BUYGET,
3478 application_method: {
3479 type: "fixed",
3480 target_type: "items",
3481 allocation: "each",
3482 max_quantity: 1,
3483 value: 100,
3484 apply_to_quantity: 1,
3485 buy_rules_min_quantity: 1,
3486 buy_rules: [promotionRule],
3487 target_rules: [promotionRule],
3488 currency_code: "usd",
3489 },
3490 rules: [promotionRule],
3491 },
3492 adminHeaders
3493 )
3494 ).data.promotion
3495
3496 const response = await api.post(
3497 `/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
3498 {
3499 create: [
3500 {
3501 operator: "eq",
3502 attribute: "new_attr",
3503 values: ["new value"],
3504 },
3505 ],
3506 },
3507 adminHeaders
3508 )
3509
3510 expect(response.status).toEqual(200)
3511
3512 const promotion = (
3513 await api.get(
3514 `/admin/promotions/${buyGetPromotion.id}`,
3515 adminHeaders
3516 )
3517 ).data.promotion
3518
3519 expect(promotion).toEqual(
3520 expect.objectContaining({
3521 id: buyGetPromotion.id,
3522 application_method: expect.objectContaining({
3523 buy_rules: expect.arrayContaining([
3524 expect.objectContaining({
3525 operator: "eq",
3526 attribute: "old_attr",
3527 values: [expect.objectContaining({ value: "old value" })],
3528 }),
3529 expect.objectContaining({
3530 operator: "eq",
3531 attribute: "new_attr",
3532 values: [expect.objectContaining({ value: "new value" })],
3533 }),
3534 ]),
3535 }),
3536 })
3537 )
3538 })
3539 })
3540
3541 describe("POST /admin/promotions/:id/rules/batch", () => {
3542 it("should throw error when promotion does not exist", async () => {
3543 const { response } = await api
3544 .post(
3545 `/admin/promotions/does-not-exist/rules/batch`,
3546 { delete: ["test-rule-id"] },
3547 adminHeaders
3548 )
3549 .catch((e) => e)
3550
3551 expect(response.status).toEqual(404)
3552 expect(response.data).toEqual({
3553 type: "not_found",
3554 message: "Promotion with id: does-not-exist was not found",
3555 })
3556 })
3557
3558 it("should remove rules from a promotion successfully", async () => {
3559 const response = await api.post(
3560 `/admin/promotions/${standardPromotion.id}/rules/batch`,
3561 { delete: [standardPromotion.rules[0].id] },
3562 adminHeaders
3563 )
3564 expect(response.status).toEqual(200)
3565 expect(response.data.deleted).toEqual({
3566 ids: [standardPromotion.rules[0].id],
3567 object: "promotion-rule",
3568 deleted: true,
3569 })
3570
3571 const promotion = (
3572 await api.get(
3573 `/admin/promotions/${standardPromotion.id}?fields=*rules`,
3574 adminHeaders
3575 )
3576 ).data.promotion
3577
3578 expect(promotion.rules!.length).toEqual(0)
3579 })
3580 })
3581
3582 describe("POST /admin/promotions/:id/target-rules/batch", () => {
3583 it("should throw error when promotion does not exist", async () => {
3584 const { response } = await api
3585 .post(
3586 `/admin/promotions/does-not-exist/target-rules/batch`,
3587 { delete: ["test-rule-id"] },
3588 adminHeaders
3589 )
3590 .catch((e) => e)
3591
3592 expect(response.status).toEqual(404)
3593 expect(response.data).toEqual({
3594 type: "not_found",
3595 message: "Promotion with id: does-not-exist was not found",
3596 })
3597 })
3598
3599 it("should remove target rules from a promotion successfully", async () => {
3600 const ruleId = standardPromotion.application_method.target_rules[0].id
3601 const response = await api.post(
3602 `/admin/promotions/${standardPromotion.id}/target-rules/batch`,
3603 { delete: [ruleId] },
3604 adminHeaders
3605 )
3606
3607 expect(response.status).toEqual(200)
3608 expect(response.data.deleted).toEqual({
3609 ids: [ruleId],
3610 object: "promotion-rule",
3611 deleted: true,
3612 })
3613
3614 const promotion = (
3615 await api.get(
3616 `/admin/promotions/${standardPromotion.id}?fields=*application_method.target_rules`,
3617 adminHeaders
3618 )
3619 ).data.promotion
3620
3621 expect(promotion.application_method!.target_rules!.length).toEqual(0)
3622 })
3623 })
3624
3625 describe("POST /admin/promotions/:id/buy-rules/batch", () => {
3626 it("should throw error when promotion does not exist", async () => {
3627 const { response } = await api
3628 .post(
3629 `/admin/promotions/does-not-exist/buy-rules/batch`,
3630 { delete: ["test-rule-id"] },
3631 adminHeaders
3632 )
3633 .catch((e) => e)
3634
3635 expect(response.status).toEqual(404)
3636 expect(response.data).toEqual({
3637 type: "not_found",
3638 message: "Promotion with id: does-not-exist was not found",
3639 })
3640 })
3641
3642 it("should remove buy rules from a promotion successfully", async () => {
3643 const buyGetPromotion = (
3644 await api.post(
3645 `/admin/promotions`,
3646 {
3647 code: "TEST_BUYGET",
3648 type: PromotionType.BUYGET,
3649 application_method: {
3650 type: "fixed",
3651 currency_code: "usd",
3652 target_type: "items",
3653 allocation: "each",
3654 max_quantity: 1,
3655 value: 100,
3656 apply_to_quantity: 1,
3657 buy_rules_min_quantity: 1,
3658 buy_rules: [promotionRule],
3659 target_rules: [promotionRule],
3660 },
3661 rules: [promotionRule],
3662 },
3663 adminHeaders
3664 )
3665 ).data.promotion
3666
3667 const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
3668 const response = await api.post(
3669 `/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
3670 { delete: [ruleId] },
3671 adminHeaders
3672 )
3673
3674 expect(response.status).toEqual(200)
3675 expect(response.data.deleted).toEqual({
3676 ids: [ruleId],
3677 object: "promotion-rule",
3678 deleted: true,
3679 })
3680
3681 const promotion = (
3682 await api.get(
3683 `/admin/promotions/${buyGetPromotion.id}?fields=*application_method.buy_rules`,
3684 adminHeaders
3685 )
3686 ).data.promotion
3687
3688 expect(promotion.application_method!.buy_rules!.length).toEqual(0)
3689 })
3690 })
3691
3692 describe("POST /admin/promotions/:id/rules/batch", () => {
3693 it("should throw error when promotion does not exist", async () => {
3694 const { response } = await api
3695 .post(
3696 `/admin/promotions/does-not-exist/rules/batch`,
3697 {
3698 update: [
3699 {
3700 id: standardPromotion.rules[0].id,
3701 attribute: "new_attr",
3702 operator: "eq",
3703 values: ["new value"],
3704 },
3705 ],
3706 },
3707 adminHeaders
3708 )
3709 .catch((e) => e)
3710
3711 expect(response.status).toEqual(404)
3712 expect(response.data).toEqual({
3713 type: "not_found",
3714 message: "Promotion with id: does-not-exist was not found",
3715 })
3716 })
3717
3718 it("should throw error when promotion rule id does not exist", async () => {
3719 const { response } = await api
3720 .post(
3721 `/admin/promotions/${standardPromotion.id}/rules/batch`,
3722 {
3723 update: [
3724 {
3725 id: "does-not-exist",
3726 attribute: "new_attr",
3727 operator: "eq",
3728 values: ["new value"],
3729 },
3730 ],
3731 },
3732 adminHeaders
3733 )
3734 .catch((e) => e)
3735
3736 expect(response.status).toEqual(400)
3737 expect(response.data).toEqual({
3738 type: "invalid_data",
3739 message: "Promotion rules with id - does-not-exist not found",
3740 })
3741 })
3742
3743 it("should add rules to a promotion successfully", async () => {
3744 const response = await api.post(
3745 `/admin/promotions/${standardPromotion.id}/rules/batch`,
3746 {
3747 update: [
3748 {
3749 id: standardPromotion.rules[0].id,
3750 operator: "eq",
3751 attribute: "new_attr",
3752 values: ["new value"],
3753 },
3754 ],
3755 },
3756 adminHeaders
3757 )
3758
3759 expect(response.status).toEqual(200)
3760
3761 const promotion = (
3762 await api.get(
3763 `/admin/promotions/${standardPromotion.id}?fields=*rules`,
3764 adminHeaders
3765 )
3766 ).data.promotion
3767
3768 expect(promotion).toEqual(
3769 expect.objectContaining({
3770 id: standardPromotion.id,
3771 rules: expect.arrayContaining([
3772 expect.objectContaining({
3773 operator: "eq",
3774 attribute: "new_attr",
3775 values: [expect.objectContaining({ value: "new value" })],
3776 }),
3777 ]),
3778 })
3779 )
3780 })
3781 })
3782
3783 describe("GET /admin/promotions/rule-attribute-options/:ruleType", () => {
3784 it("should throw error when ruleType is invalid", async () => {
3785 const { response } = await api
3786 .get(
3787 `/admin/promotions/rule-attribute-options/does-not-exist`,
3788 adminHeaders
3789 )
3790 .catch((e) => e)
3791
3792 expect(response.status).toEqual(400)
3793 expect(response.data).toEqual({
3794 type: "invalid_data",
3795 message: "Invalid param rule_type (does-not-exist)",
3796 })
3797 })
3798
3799 it("return all rule attributes for a valid ruleType", async () => {
3800 const response = await api.get(
3801 `/admin/promotions/rule-attribute-options/rules`,
3802 adminHeaders
3803 )
3804
3805 expect(response.status).toEqual(200)
3806 expect(response.data.attributes).toEqual(
3807 expect.arrayContaining([
3808 expect.objectContaining({
3809 id: "currency_code",
3810 value: "currency_code",
3811 label: "Currency Code",
3812 field_type: "select",
3813 required: false,
3814 disguised: true,
3815 hydrate: true,
3816 }),
3817 expect.objectContaining({
3818 id: "customer_group",
3819 value: "customer.groups.id",
3820 label: "Customer Group",
3821 required: false,
3822 field_type: "multiselect",
3823 }),
3824 expect.objectContaining({
3825 id: "region",
3826 value: "region.id",
3827 label: "Region",
3828 required: false,
3829 field_type: "multiselect",
3830 }),
3831 expect.objectContaining({
3832 id: "country",
3833 value: "shipping_address.country_code",
3834 label: "Country",
3835 required: false,
3836 field_type: "multiselect",
3837 }),
3838 expect.objectContaining({
3839 id: "sales_channel",
3840 value: "sales_channel_id",
3841 label: "Sales Channel",
3842 required: false,
3843 field_type: "multiselect",
3844 }),
3845 ])
3846 )
3847 })
3848
3849 it("return all product target rule attributes by default", async () => {
3850 const response = await api.get(
3851 `/admin/promotions/rule-attribute-options/target-rules`,
3852 adminHeaders
3853 )
3854
3855 expect(response.status).toEqual(200)
3856 expect(response.data.attributes).toEqual(
3857 expect.arrayContaining([
3858 expect.objectContaining({
3859 id: "product",
3860 value: "items.product.id",
3861 label: "Product",
3862 required: false,
3863 field_type: "multiselect",
3864 operators: expect.anything(),
3865 }),
3866 expect.objectContaining({
3867 id: "product_category",
3868 value: "items.product.categories.id",
3869 label: "Product Category",
3870 required: false,
3871 field_type: "multiselect",
3872 operators: expect.anything(),
3873 }),
3874 expect.objectContaining({
3875 id: "product_collection",
3876 value: "items.product.collection_id",
3877 label: "Product Collection",
3878 required: false,
3879 field_type: "multiselect",
3880 operators: expect.anything(),
3881 }),
3882 expect.objectContaining({
3883 id: "product_type",
3884 value: "items.product.type_id",
3885 label: "Product Type",
3886 required: false,
3887 field_type: "multiselect",
3888 operators: expect.anything(),
3889 }),
3890 expect.objectContaining({
3891 id: "product_tag",
3892 value: "items.product.tags.id",
3893 label: "Product Tag",
3894 required: false,
3895 field_type: "multiselect",
3896 operators: expect.anything(),
3897 }),
3898 ])
3899 )
3900 })
3901
3902 it("return all target rule attributes when application method target type is shipping_methods", async () => {
3903 const response = await api.get(
3904 `/admin/promotions/rule-attribute-options/target-rules?application_method_target_type=shipping_methods`,
3905 adminHeaders
3906 )
3907
3908 expect(response.status).toEqual(200)
3909 expect(response.data.attributes).toEqual(
3910 expect.arrayContaining([
3911 expect.objectContaining({
3912 id: "shipping_option_type",
3913 value:
3914 "shipping_methods.shipping_option.shipping_option_type_id",
3915 label: "Shipping Option Type",
3916 required: false,
3917 field_type: "multiselect",
3918 operators: expect.anything(),
3919 }),
3920 ])
3921 )
3922 })
3923 })
3924
3925 describe("GET /admin/promotions/rule-value-options/:ruleType/:ruleAttributeId", () => {
3926 it("should throw error when ruleType is invalid", async () => {
3927 const { response } = await api
3928 .get(
3929 `/admin/promotions/rule-value-options/does-not-exist/region`,
3930 adminHeaders
3931 )
3932 .catch((e) => e)
3933
3934 expect(response.status).toEqual(400)
3935 expect(response.data).toEqual({
3936 type: "invalid_data",
3937 message: "Invalid param rule_type (does-not-exist)",
3938 })
3939 })
3940
3941 it("should throw error when ruleAttributeId is invalid", async () => {
3942 const { response } = await api
3943 .get(
3944 `/admin/promotions/rule-value-options/rules/does-not-exist`,
3945 adminHeaders
3946 )
3947 .catch((e) => e)
3948
3949 expect(response.status).toEqual(400)
3950 expect(response.data).toEqual({
3951 type: "invalid_data",
3952 message: "Invalid rule attribute - does-not-exist",
3953 })
3954 })
3955
3956 it("should throw 400 (not 500) when ruleAttributeId is invalid and a value filter is provided", async () => {
3957 const { response } = await api
3958 .get(
3959 `/admin/promotions/rule-value-options/rules/does-not-exist?value=test`,
3960 adminHeaders
3961 )
3962 .catch((e) => e)
3963
3964 expect(response.status).toEqual(400)
3965 expect(response.data).toEqual({
3966 type: "invalid_data",
3967 message: "Invalid rule attribute - does-not-exist",
3968 })
3969 })
3970
3971 it("should return all values based on rule types", async () => {
3972 const region1 = (
3973 await api.post(
3974 "/admin/regions",
3975 {
3976 payment_providers: ["pp_system_default"],
3977 name: "North America",
3978 currency_code: "usd",
3979 },
3980 adminHeaders
3981 )
3982 ).data.region
3983
3984 const region2 = (
3985 await api.post(
3986 "/admin/regions",
3987 {
3988 payment_providers: ["pp_system_default"],
3989 name: "Europe",
3990 currency_code: "eur",
3991 },
3992 adminHeaders
3993 )
3994 ).data.region
3995
3996 let response = await api.get(
3997 `/admin/promotions/rule-value-options/rules/region`,
3998 adminHeaders
3999 )
4000
4001 expect(response.status).toEqual(200)
4002 expect(response.data.values.length).toEqual(2)
4003 expect(response.data.values).toEqual(
4004 expect.arrayContaining([
4005 {
4006 label: "North America",
4007 value: region1.id,
4008 },
4009 {
4010 label: "Europe",
4011 value: region2.id,
4012 },
4013 ])
4014 )
4015
4016 response = await api.get(
4017 `/admin/promotions/rule-value-options/rules/currency_code?limit=2&order=name`,
4018 adminHeaders
4019 )
4020
4021 expect(response.status).toEqual(200)
4022 expect(response.data.values.length).toEqual(2)
4023 expect(response.data.values).toEqual(
4024 expect.arrayContaining([
4025 { label: "Afghan Afghani", value: "afn" },
4026 { label: "Albanian Lek", value: "all" },
4027 ])
4028 )
4029
4030 const group = (
4031 await api.post(
4032 "/admin/customer-groups",
4033 { name: "VIP" },
4034 adminHeaders
4035 )
4036 ).data.customer_group
4037
4038 response = await api.get(
4039 `/admin/promotions/rule-value-options/rules/customer_group`,
4040 adminHeaders
4041 )
4042
4043 expect(response.status).toEqual(200)
4044 expect(response.data.values).toEqual([
4045 {
4046 label: "VIP",
4047 value: group.id,
4048 },
4049 ])
4050
4051 const salesChannel = (
4052 await api.post(
4053 "/admin/sales-channels",
4054 { name: "Instagram" },
4055 adminHeaders
4056 )
4057 ).data.sales_channel
4058
4059 response = await api.get(
4060 `/admin/promotions/rule-value-options/rules/sales_channel`,
4061 adminHeaders
4062 )
4063
4064 expect(response.status).toEqual(200)
4065 // TODO: This is returning a default sales channel, but very flakily
4066 // Figure out why this happens and fix
4067 // expect(response.data.values.length).toEqual(1)
4068 expect(response.data.values).toEqual(
4069 expect.arrayContaining([
4070 { label: "Instagram", value: salesChannel.id },
4071 ])
4072 )
4073
4074 response = await api.get(
4075 `/admin/promotions/rule-value-options/rules/country?limit=2`,
4076 adminHeaders
4077 )
4078
4079 expect(response.status).toEqual(200)
4080 expect(response.data.values.length).toEqual(2)
4081 expect(response.data.values).toEqual(
4082 expect.arrayContaining([
4083 {
4084 label: "Andorra",
4085 value: "ad",
4086 },
4087 {
4088 label: "United Arab Emirates",
4089 value: "ae",
4090 },
4091 ])
4092 )
4093
4094 const product1 = (
4095 await api.post(
4096 "/admin/products",
4097 {
4098 title: "Test product 1",
4099 options: [{ title: "size", values: ["large", "small"] }],
4100 shipping_profile_id: shippingProfile.id,
4101 },
4102 adminHeaders
4103 )
4104 ).data.product
4105
4106 const product2 = (
4107 await api.post(
4108 "/admin/products",
4109 {
4110 title: "Test product 2",
4111 options: [{ title: "size", values: ["large", "small"] }],
4112 shipping_profile_id: shippingProfile.id,
4113 },
4114 adminHeaders
4115 )
4116 ).data.product
4117
4118 response = await api.get(
4119 `/admin/promotions/rule-value-options/target-rules/product`,
4120 adminHeaders
4121 )
4122
4123 expect(response.status).toEqual(200)
4124 expect(response.data.values.length).toEqual(2)
4125 expect(response.data.values).toEqual(
4126 expect.arrayContaining([
4127 { label: "Test product 1", value: product1.id },
4128 { label: "Test product 2", value: product2.id },
4129 ])
4130 )
4131
4132 const category = (
4133 await api.post(
4134 "/admin/product-categories",
4135 { name: "test category 1" },
4136 adminHeaders
4137 )
4138 ).data.product_category
4139
4140 response = await api.get(
4141 `/admin/promotions/rule-value-options/target-rules/product_category`,
4142 adminHeaders
4143 )
4144
4145 expect(response.status).toEqual(200)
4146 expect(response.data.values).toEqual([
4147 { label: "test category 1", value: category.id },
4148 ])
4149
4150 const collection = (
4151 await api.post(
4152 "/admin/collections",
4153 { title: "test collection 1" },
4154 adminHeaders
4155 )
4156 ).data.collection
4157
4158 response = await api.get(
4159 `/admin/promotions/rule-value-options/target-rules/product_collection`,
4160 adminHeaders
4161 )
4162
4163 expect(response.status).toEqual(200)
4164 expect(response.data.values).toEqual([
4165 { label: "test collection 1", value: collection.id },
4166 ])
4167
4168 const type = (
4169 await api.post(
4170 "/admin/product-types",
4171 { value: "test type" },
4172 adminHeaders
4173 )
4174 ).data.product_type
4175
4176 response = await api.get(
4177 `/admin/promotions/rule-value-options/target-rules/product_type`,
4178 adminHeaders
4179 )
4180
4181 expect(response.status).toEqual(200)
4182 expect(response.data.values).toEqual([
4183 { label: "test type", value: type.id },
4184 ])
4185
4186 const tag1 = (
4187 await api.post(
4188 "/admin/product-tags",
4189 { value: "test tag 1" },
4190 adminHeaders
4191 )
4192 ).data.product_tag
4193
4194 const tag2 = (
4195 await api.post(
4196 "/admin/product-tags",
4197 { value: "test tag 2" },
4198 adminHeaders
4199 )
4200 ).data.product_tag
4201
4202 response = await api.get(
4203 `/admin/promotions/rule-value-options/target-rules/product_tag`,
4204 adminHeaders
4205 )
4206
4207 expect(response.status).toEqual(200)
4208 expect(response.data.values.length).toEqual(2)
4209 expect(response.data.values).toEqual(
4210 expect.arrayContaining([
4211 { label: "test tag 1", value: tag1.id },
4212 { label: "test tag 2", value: tag2.id },
4213 ])
4214 )
4215
4216 const soType1 = (
4217 await api.post(
4218 "/admin/shipping-option-types",
4219 { label: "Test 1", code: "test_1" },
4220 adminHeaders
4221 )
4222 ).data.shipping_option_type
4223
4224 const soType2 = (
4225 await api.post(
4226 "/admin/shipping-option-types",
4227 { label: "Test 2", code: "test_2" },
4228 adminHeaders
4229 )
4230 ).data.shipping_option_type
4231
4232 response = await api.get(
4233 `/admin/promotions/rule-value-options/target-rules/shipping_option_type?application_method_target_type=shipping_methods`,
4234 adminHeaders
4235 )
4236
4237 expect(response.status).toEqual(200)
4238 expect(response.data.values.length).toEqual(2)
4239 expect(response.data.values).toEqual(
4240 expect.arrayContaining([
4241 { label: "Test 1", value: soType1.id },
4242 { label: "Test 2", value: soType2.id },
4243 ])
4244 )
4245 })
4246 })
4247 })
4248
4249 describe("Promotions Workflows", () => {
4250 it("updateCampaignsWorkflow - should not call listCampaigns when campaignsData is empty", async () => {
4251 const promotionService = appContainer.resolve(Modules.PROMOTION)
4252 const listCampaignsSpy = jest.spyOn(promotionService, "listCampaigns")
4253
4254 const { result } = await updateCampaignsWorkflow(appContainer).run({
4255 input: { campaignsData: [] },
4256 })
4257
4258 expect(result).toEqual([])
4259 expect(listCampaignsSpy).not.toHaveBeenCalled()
4260
4261 listCampaignsSpy.mockRestore()
4262 })
4263
4264 it("updatePromotionRulesWorkflow - should not call listPromotionRules when data is empty", async () => {
4265 const promotionService = appContainer.resolve(Modules.PROMOTION)
4266 const listPromotionRulesSpy = jest.spyOn(
4267 promotionService,
4268 "listPromotionRules"
4269 )
4270
4271 const { result } = await updatePromotionRulesWorkflow(appContainer).run(
4272 {
4273 input: { data: [] },
4274 }
4275 )
4276
4277 expect(result).toEqual([])
4278 expect(listPromotionRulesSpy).not.toHaveBeenCalled()
4279
4280 listPromotionRulesSpy.mockRestore()
4281 })
4282
4283 it("updatePromotionsWorkflow - should not call listPromotions when promotionsData is empty", async () => {
4284 const promotionService = appContainer.resolve(Modules.PROMOTION)
4285 const listPromotionsSpy = jest.spyOn(promotionService, "listPromotions")
4286
4287 const { result } = await updatePromotionsWorkflow(appContainer).run({
4288 input: { promotionsData: [] },
4289 })
4290
4291 expect(result).toEqual([])
4292 expect(listPromotionsSpy).not.toHaveBeenCalled()
4293
4294 listPromotionsSpy.mockRestore()
4295 })
4296 })
4297 },
4298 })