1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 IInventoryServiceNext,
4 IPricingModuleService,
5 IProductModuleService,
6 IRegionModuleService,
7 ISalesChannelModuleService,
8 IStockLocationServiceNext,
9 ITaxModuleService,
10 } from "@medusajs/types"
11 import {
12 ContainerRegistrationKeys,
13 Modules,
14 ProductStatus,
15 PromotionStatus,
16 PromotionType,
17 } from "@medusajs/utils"
18 import {
19 adminHeaders,
20 createAdminUser,
21 } from "../../../helpers/create-admin-user"
22 import { setupTaxStructure } from "../fixtures"
23 import { removeDraftOrderPromotionsWorkflow } from "../../../../packages/core/core-flows/dist/draft-order/workflows/remove-draft-order-promotions"
24
25 jest.setTimeout(100000)
26
27 const env = {}
28
29 medusaIntegrationTestRunner({
30 env,
31 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
32 let appContainer
33 let regionModuleService: IRegionModuleService
34 let scModuleService: ISalesChannelModuleService
35 let productModule: IProductModuleService
36 let pricingModule: IPricingModuleService
37 let inventoryModule: IInventoryServiceNext
38 let stockLocationModule: IStockLocationServiceNext
39 let taxModule: ITaxModuleService
40 let remoteLink
41
42 beforeAll(async () => {
43 appContainer = getContainer()
44 regionModuleService = appContainer.resolve(Modules.REGION)
45 scModuleService = appContainer.resolve(Modules.SALES_CHANNEL)
46 productModule = appContainer.resolve(Modules.PRODUCT)
47 pricingModule = appContainer.resolve(Modules.PRICING)
48 inventoryModule = appContainer.resolve(Modules.INVENTORY)
49 stockLocationModule = appContainer.resolve(Modules.STOCK_LOCATION)
50 taxModule = appContainer.resolve(Modules.TAX)
51 remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
52 })
53
54 beforeAll(async () => {
55 await createAdminUser(dbConnection, adminHeaders, appContainer)
56
57 await dbUtils.snapshot()
58 })
59
60 describe("Draft Orders - Admin", () => {
61 it("should create a draft order", async () => {
62 const region = await regionModuleService.createRegions({
63 name: "US",
64 currency_code: "usd",
65 })
66
67 const salesChannel = await scModuleService.createSalesChannels({
68 name: "Webshop",
69 })
70
71 const location = await stockLocationModule.createStockLocations({
72 name: "Warehouse",
73 })
74
75 const [product, product_2] = await productModule.createProducts([
76 {
77 title: "Test product",
78 status: ProductStatus.PUBLISHED,
79 variants: [
80 {
81 title: "Test variant",
82 },
83 ],
84 },
85 {
86 title: "Another product",
87 status: ProductStatus.PUBLISHED,
88 variants: [
89 {
90 title: "Variant variable",
91 manage_inventory: false,
92 },
93 ],
94 },
95 ])
96
97 const inventoryItem = await inventoryModule.createInventoryItems({
98 sku: "inv-1234",
99 })
100
101 await inventoryModule.createInventoryLevels([
102 {
103 inventory_item_id: inventoryItem.id,
104 location_id: location.id,
105 stocked_quantity: 2,
106 reserved_quantity: 0,
107 },
108 ])
109
110 const [priceSet, priceSet_2] = await pricingModule.createPriceSets([
111 {
112 prices: [
113 {
114 amount: 3000,
115 currency_code: "usd",
116 },
117 ],
118 },
119 {
120 prices: [
121 {
122 amount: 1000,
123 currency_code: "usd",
124 },
125 ],
126 },
127 ])
128
129 await api.post(
130 "/admin/price-preferences",
131 {
132 attribute: "currency_code",
133 value: "usd",
134 is_tax_inclusive: true,
135 },
136 adminHeaders
137 )
138
139 await remoteLink.create([
140 {
141 [Modules.PRODUCT]: {
142 variant_id: product.variants[0].id,
143 },
144 [Modules.PRICING]: {
145 price_set_id: priceSet.id,
146 },
147 },
148 {
149 [Modules.PRODUCT]: {
150 variant_id: product_2.variants[0].id,
151 },
152 [Modules.PRICING]: {
153 price_set_id: priceSet_2.id,
154 },
155 },
156 {
157 [Modules.SALES_CHANNEL]: {
158 sales_channel_id: salesChannel.id,
159 },
160 [Modules.STOCK_LOCATION]: {
161 stock_location_id: location.id,
162 },
163 },
164 {
165 [Modules.PRODUCT]: {
166 variant_id: product.variants[0].id,
167 },
168 [Modules.INVENTORY]: {
169 inventory_item_id: inventoryItem.id,
170 },
171 },
172 {
173 [Modules.PRODUCT]: {
174 variant_id: product_2.variants[0].id,
175 },
176 [Modules.INVENTORY]: {
177 inventory_item_id: inventoryItem.id,
178 },
179 },
180 ])
181
182 await setupTaxStructure(taxModule)
183
184 const payload = {
185 email: "oli@test.dk",
186 region_id: region.id,
187 sales_channel_id: salesChannel.id,
188 currency_code: "usd",
189 shipping_address: {
190 first_name: "Test",
191 last_name: "Test",
192 address_1: "Test",
193 city: "Test",
194 country_code: "US",
195 postal_code: "12345",
196 phone: "12345",
197 },
198 billing_address: {
199 first_name: "Test",
200 last_name: "Test",
201 address_1: "Test",
202 city: "Test",
203 country_code: "US",
204 postal_code: "12345",
205 },
206 items: [
207 {
208 variant_id: product.variants[0].id,
209 quantity: 2,
210 },
211 {
212 variant_id: product_2.variants[0].id,
213 unit_price: 200,
214 quantity: 1,
215 metadata: {
216 note: "reduced price",
217 },
218 },
219 {
220 title: "Custom Item",
221 variant_sku: "sku123",
222 variant_barcode: "barcode123",
223 unit_price: 2200,
224 quantity: 1,
225 },
226 ],
227 shipping_methods: [
228 {
229 name: "test-method",
230 shipping_option_id: "test-option",
231 amount: 100,
232 },
233 ],
234 }
235
236 const response = await api.post(
237 "/admin/draft-orders",
238 payload,
239 adminHeaders
240 )
241
242 expect(response.data).toEqual(
243 expect.objectContaining({
244 draft_order: expect.objectContaining({
245 status: "draft",
246 version: 1,
247 summary: expect.objectContaining({
248 // TODO: add summary fields
249 }),
250 items: expect.arrayContaining([
251 expect.objectContaining({
252 title: "Test product",
253 subtitle: "Test variant",
254 product_title: "Test product",
255 product_description: null,
256 product_subtitle: null,
257 product_type: null,
258 product_type_id: null,
259 product_collection: null,
260 product_handle: "test-product",
261 variant_sku: null,
262 variant_barcode: null,
263 variant_title: "Test variant",
264 variant_option_values: null,
265 requires_shipping: true,
266 is_discountable: true,
267 is_tax_inclusive: true,
268 is_custom_price: false,
269 raw_compare_at_unit_price: null,
270 raw_unit_price: expect.objectContaining({
271 value: "3000",
272 }),
273 metadata: {},
274 tax_lines: [
275 expect.objectContaining({
276 code: "US_DEF",
277 provider_id: "system",
278 rate: 2,
279 }),
280 ],
281 adjustments: [],
282 unit_price: 3000,
283 quantity: 2,
284 raw_quantity: expect.objectContaining({
285 value: "2",
286 }),
287 detail: expect.objectContaining({
288 raw_quantity: expect.objectContaining({
289 value: "2",
290 }),
291 raw_fulfilled_quantity: expect.objectContaining({
292 value: "0",
293 }),
294 raw_shipped_quantity: expect.objectContaining({
295 value: "0",
296 }),
297 raw_return_requested_quantity: expect.objectContaining({
298 value: "0",
299 }),
300 raw_return_received_quantity: expect.objectContaining({
301 value: "0",
302 }),
303 raw_return_dismissed_quantity: expect.objectContaining({
304 value: "0",
305 }),
306 raw_written_off_quantity: expect.objectContaining({
307 value: "0",
308 }),
309 quantity: 2,
310 fulfilled_quantity: 0,
311 shipped_quantity: 0,
312 return_requested_quantity: 0,
313 return_received_quantity: 0,
314 return_dismissed_quantity: 0,
315 written_off_quantity: 0,
316 }),
317 }),
318 expect.objectContaining({
319 title: "Another product",
320 subtitle: "Variant variable",
321 raw_unit_price: expect.objectContaining({
322 value: "200",
323 }),
324 metadata: {
325 note: "reduced price",
326 },
327 unit_price: 200,
328 is_tax_inclusive: true,
329 quantity: 1,
330 raw_quantity: expect.objectContaining({
331 value: "1",
332 }),
333 }),
334 expect.objectContaining({
335 title: "Custom Item",
336 variant_sku: "sku123",
337 variant_barcode: "barcode123",
338 variant_title: null,
339 is_custom_price: true,
340 raw_unit_price: expect.objectContaining({
341 value: "2200",
342 }),
343 unit_price: 2200,
344 quantity: 1,
345 raw_quantity: expect.objectContaining({
346 value: "1",
347 }),
348 }),
349 ]),
350 shipping_address: expect.objectContaining({
351 last_name: "Test",
352 address_1: "Test",
353 city: "Test",
354 country_code: "US",
355 postal_code: "12345",
356 phone: "12345",
357 }),
358 billing_address: expect.objectContaining({
359 first_name: "Test",
360 last_name: "Test",
361 address_1: "Test",
362 city: "Test",
363 country_code: "US",
364 postal_code: "12345",
365 }),
366 shipping_methods: [
367 expect.objectContaining({
368 name: "test-method",
369 raw_amount: expect.objectContaining({
370 value: "100",
371 }),
372 is_tax_inclusive: false,
373 shipping_option_id: "test-option",
374 data: null,
375 tax_lines: [
376 expect.objectContaining({
377 code: "US_DEF",
378 provider_id: "system",
379 rate: 2,
380 }),
381 ],
382 adjustments: [],
383 amount: 100,
384 }),
385 ],
386 }),
387 })
388 )
389
390 expect(response.status).toEqual(200)
391 })
392
393 it("should create a draft order applying the correct promotion on the items", async () => {
394 const region = await regionModuleService.createRegions({
395 name: "US",
396 currency_code: "usd",
397 })
398
399 const salesChannel = await scModuleService.createSalesChannels({
400 name: "Webshop",
401 })
402
403 const location = await stockLocationModule.createStockLocations({
404 name: "Warehouse",
405 })
406
407 const [product, product_2] = await productModule.createProducts([
408 {
409 title: "Test product",
410 status: ProductStatus.PUBLISHED,
411 variants: [
412 {
413 title: "Test variant",
414 },
415 ],
416 },
417 {
418 title: "Another product",
419 status: ProductStatus.PUBLISHED,
420 variants: [
421 {
422 title: "Variant variable",
423 manage_inventory: false,
424 },
425 ],
426 },
427 ])
428
429 const inventoryItem = await inventoryModule.createInventoryItems({
430 sku: "inv-1234",
431 })
432
433 await inventoryModule.createInventoryLevels([
434 {
435 inventory_item_id: inventoryItem.id,
436 location_id: location.id,
437 stocked_quantity: 2,
438 reserved_quantity: 0,
439 },
440 ])
441
442 const [priceSet, priceSet_2] = await pricingModule.createPriceSets([
443 {
444 prices: [
445 {
446 amount: 3000,
447 currency_code: "usd",
448 },
449 ],
450 },
451 {
452 prices: [
453 {
454 amount: 1000,
455 currency_code: "usd",
456 },
457 ],
458 },
459 ])
460
461 /**
462 * Create a promotion to test with
463 */
464 const promotion = (
465 await api.post(
466 `/admin/promotions`,
467 {
468 code: "testytest",
469 type: PromotionType.STANDARD,
470 status: PromotionStatus.ACTIVE,
471 application_method: {
472 target_type: "items",
473 type: "fixed",
474 allocation: "each",
475 currency_code: "usd",
476 value: 100,
477 max_quantity: 100,
478 target_rules: [
479 {
480 attribute: "items.variant_id",
481 operator: "in",
482 values: [product.variants[0].id, product_2.variants[0].id],
483 },
484 ],
485 },
486 },
487 adminHeaders
488 )
489 ).data.promotion
490
491 await api.post(
492 "/admin/price-preferences",
493 {
494 attribute: "currency_code",
495 value: "usd",
496 is_tax_inclusive: true,
497 },
498 adminHeaders
499 )
500
501 await remoteLink.create([
502 {
503 [Modules.PRODUCT]: {
504 variant_id: product.variants[0].id,
505 },
506 [Modules.PRICING]: {
507 price_set_id: priceSet.id,
508 },
509 },
510 {
511 [Modules.PRODUCT]: {
512 variant_id: product_2.variants[0].id,
513 },
514 [Modules.PRICING]: {
515 price_set_id: priceSet_2.id,
516 },
517 },
518 {
519 [Modules.SALES_CHANNEL]: {
520 sales_channel_id: salesChannel.id,
521 },
522 [Modules.STOCK_LOCATION]: {
523 stock_location_id: location.id,
524 },
525 },
526 {
527 [Modules.PRODUCT]: {
528 variant_id: product.variants[0].id,
529 },
530 [Modules.INVENTORY]: {
531 inventory_item_id: inventoryItem.id,
532 },
533 },
534 {
535 [Modules.PRODUCT]: {
536 variant_id: product_2.variants[0].id,
537 },
538 [Modules.INVENTORY]: {
539 inventory_item_id: inventoryItem.id,
540 },
541 },
542 ])
543
544 await setupTaxStructure(taxModule)
545
546 const payload = {
547 email: "oli@test.dk",
548 region_id: region.id,
549 sales_channel_id: salesChannel.id,
550 currency_code: "usd",
551 shipping_address: {
552 first_name: "Test",
553 last_name: "Test",
554 address_1: "Test",
555 city: "Test",
556 country_code: "US",
557 postal_code: "12345",
558 phone: "12345",
559 },
560 billing_address: {
561 first_name: "Test",
562 last_name: "Test",
563 address_1: "Test",
564 city: "Test",
565 country_code: "US",
566 postal_code: "12345",
567 },
568 promo_codes: ["testytest"],
569 items: [
570 {
571 variant_id: product.variants[0].id,
572 is_discountable: true,
573 quantity: 2,
574 },
575 {
576 variant_id: product_2.variants[0].id,
577 is_discountable: true,
578 unit_price: 200,
579 quantity: 1,
580 metadata: {
581 note: "reduced price",
582 },
583 },
584 {
585 title: "Custom Item",
586 variant_sku: "sku123",
587 variant_barcode: "barcode123",
588 is_discountable: true,
589 unit_price: 2200,
590 quantity: 1,
591 },
592 ],
593 shipping_methods: [
594 {
595 name: "test-method",
596 shipping_option_id: "test-option",
597 amount: 100,
598 },
599 ],
600 }
601
602 const response = await api.post(
603 "/admin/draft-orders",
604 payload,
605 adminHeaders
606 )
607
608 expect(response.data).toEqual(
609 expect.objectContaining({
610 draft_order: expect.objectContaining({
611 status: "draft",
612 version: 1,
613 summary: expect.objectContaining({
614 // TODO: add summary fields
615 }),
616 items: expect.arrayContaining([
617 expect.objectContaining({
618 title: "Test product",
619 subtitle: "Test variant",
620 product_title: "Test product",
621 product_description: null,
622 product_subtitle: null,
623 product_type: null,
624 product_type_id: null,
625 product_collection: null,
626 product_handle: "test-product",
627 variant_sku: null,
628 variant_barcode: null,
629 variant_title: "Test variant",
630 variant_option_values: null,
631 requires_shipping: true,
632 is_discountable: true,
633 is_tax_inclusive: true,
634 is_custom_price: false,
635 raw_compare_at_unit_price: null,
636 raw_unit_price: expect.objectContaining({
637 value: "3000",
638 }),
639 metadata: {},
640 tax_lines: [
641 expect.objectContaining({
642 code: "US_DEF",
643 provider_id: "system",
644 rate: 2,
645 }),
646 ],
647 adjustments: [
648 expect.objectContaining({
649 amount: 200,
650 code: "testytest",
651 is_tax_inclusive: false,
652 promotion_id: promotion.id,
653 provider_id: null,
654 }),
655 ],
656 unit_price: 3000,
657 quantity: 2,
658 raw_quantity: expect.objectContaining({
659 value: "2",
660 }),
661 detail: expect.objectContaining({
662 raw_quantity: expect.objectContaining({
663 value: "2",
664 }),
665 raw_fulfilled_quantity: expect.objectContaining({
666 value: "0",
667 }),
668 raw_shipped_quantity: expect.objectContaining({
669 value: "0",
670 }),
671 raw_return_requested_quantity: expect.objectContaining({
672 value: "0",
673 }),
674 raw_return_received_quantity: expect.objectContaining({
675 value: "0",
676 }),
677 raw_return_dismissed_quantity: expect.objectContaining({
678 value: "0",
679 }),
680 raw_written_off_quantity: expect.objectContaining({
681 value: "0",
682 }),
683 quantity: 2,
684 fulfilled_quantity: 0,
685 shipped_quantity: 0,
686 return_requested_quantity: 0,
687 return_received_quantity: 0,
688 return_dismissed_quantity: 0,
689 written_off_quantity: 0,
690 }),
691 }),
692 expect.objectContaining({
693 title: "Another product",
694 subtitle: "Variant variable",
695 raw_unit_price: expect.objectContaining({
696 value: "200",
697 }),
698 metadata: {
699 note: "reduced price",
700 },
701 unit_price: 200,
702 is_tax_inclusive: true,
703 quantity: 1,
704 raw_quantity: expect.objectContaining({
705 value: "1",
706 }),
707 adjustments: [
708 expect.objectContaining({
709 amount: 100,
710 code: "testytest",
711 is_tax_inclusive: false,
712 promotion_id: promotion.id,
713 provider_id: null,
714 }),
715 ],
716 }),
717 expect.objectContaining({
718 title: "Custom Item",
719 variant_sku: "sku123",
720 variant_barcode: "barcode123",
721 variant_title: null,
722 is_custom_price: true,
723 raw_unit_price: expect.objectContaining({
724 value: "2200",
725 }),
726 unit_price: 2200,
727 quantity: 1,
728 raw_quantity: expect.objectContaining({
729 value: "1",
730 }),
731 }),
732 ]),
733 shipping_address: expect.objectContaining({
734 last_name: "Test",
735 address_1: "Test",
736 city: "Test",
737 country_code: "US",
738 postal_code: "12345",
739 phone: "12345",
740 }),
741 billing_address: expect.objectContaining({
742 first_name: "Test",
743 last_name: "Test",
744 address_1: "Test",
745 city: "Test",
746 country_code: "US",
747 postal_code: "12345",
748 }),
749 shipping_methods: [
750 expect.objectContaining({
751 name: "test-method",
752 raw_amount: expect.objectContaining({
753 value: "100",
754 }),
755 is_tax_inclusive: false,
756 shipping_option_id: "test-option",
757 data: null,
758 tax_lines: [
759 expect.objectContaining({
760 code: "US_DEF",
761 provider_id: "system",
762 rate: 2,
763 }),
764 ],
765 adjustments: [],
766 amount: 100,
767 }),
768 ],
769 }),
770 })
771 )
772
773 expect(response.status).toEqual(200)
774 })
775
776 it("should apply promotion to a draft order via order edit", async () => {
777 const region = await regionModuleService.createRegions({
778 name: "US",
779 currency_code: "usd",
780 })
781
782 const salesChannel = await scModuleService.createSalesChannels({
783 name: "Webshop",
784 })
785
786 const location = await stockLocationModule.createStockLocations({
787 name: "Warehouse",
788 })
789
790 const [product, product_2] = await productModule.createProducts([
791 {
792 title: "Test product",
793 status: ProductStatus.PUBLISHED,
794 variants: [
795 {
796 title: "Test variant",
797 },
798 ],
799 },
800 {
801 title: "Another product",
802 status: ProductStatus.PUBLISHED,
803 variants: [
804 {
805 title: "Variant variable",
806 manage_inventory: false,
807 },
808 ],
809 },
810 ])
811
812 const inventoryItem = await inventoryModule.createInventoryItems({
813 sku: "inv-1234",
814 })
815
816 await inventoryModule.createInventoryLevels([
817 {
818 inventory_item_id: inventoryItem.id,
819 location_id: location.id,
820 stocked_quantity: 2,
821 reserved_quantity: 0,
822 },
823 ])
824
825 const [priceSet, priceSet_2] = await pricingModule.createPriceSets([
826 {
827 prices: [
828 {
829 amount: 3000,
830 currency_code: "usd",
831 },
832 ],
833 },
834 {
835 prices: [
836 {
837 amount: 1000,
838 currency_code: "usd",
839 },
840 ],
841 },
842 ])
843
844 /**
845 * Create a promotion to test with
846 */
847 const promotion = (
848 await api.post(
849 `/admin/promotions`,
850 {
851 code: "testytest",
852 type: PromotionType.STANDARD,
853 status: PromotionStatus.ACTIVE,
854 is_tax_inclusive: true,
855 application_method: {
856 target_type: "items",
857 type: "percentage",
858 allocation: "each",
859 currency_code: "usd",
860 value: 10,
861 max_quantity: 10,
862 },
863 },
864 adminHeaders
865 )
866 ).data.promotion
867
868 await api.post(
869 "/admin/price-preferences",
870 {
871 attribute: "currency_code",
872 value: "usd",
873 is_tax_inclusive: true,
874 },
875 adminHeaders
876 )
877
878 await remoteLink.create([
879 {
880 [Modules.PRODUCT]: {
881 variant_id: product.variants[0].id,
882 },
883 [Modules.PRICING]: {
884 price_set_id: priceSet.id,
885 },
886 },
887 {
888 [Modules.PRODUCT]: {
889 variant_id: product_2.variants[0].id,
890 },
891 [Modules.PRICING]: {
892 price_set_id: priceSet_2.id,
893 },
894 },
895 {
896 [Modules.SALES_CHANNEL]: {
897 sales_channel_id: salesChannel.id,
898 },
899 [Modules.STOCK_LOCATION]: {
900 stock_location_id: location.id,
901 },
902 },
903 {
904 [Modules.PRODUCT]: {
905 variant_id: product.variants[0].id,
906 },
907 [Modules.INVENTORY]: {
908 inventory_item_id: inventoryItem.id,
909 },
910 },
911 {
912 [Modules.PRODUCT]: {
913 variant_id: product_2.variants[0].id,
914 },
915 [Modules.INVENTORY]: {
916 inventory_item_id: inventoryItem.id,
917 },
918 },
919 ])
920
921 await setupTaxStructure(taxModule)
922
923 const payload = {
924 email: "oli@test.dk",
925 region_id: region.id,
926 sales_channel_id: salesChannel.id,
927 currency_code: "usd",
928 shipping_address: {
929 first_name: "Test",
930 last_name: "Test",
931 address_1: "Test",
932 city: "Test",
933 country_code: "US",
934 postal_code: "12345",
935 phone: "12345",
936 },
937 billing_address: {
938 first_name: "Test",
939 last_name: "Test",
940 address_1: "Test",
941 city: "Test",
942 country_code: "US",
943 postal_code: "12345",
944 },
945 items: [
946 {
947 variant_id: product.variants[0].id,
948 is_discountable: true,
949 quantity: 2, // 2 * 3000$ = 6000$ TAX INCLUSIVE
950 },
951 {
952 variant_id: product_2.variants[0].id,
953 is_discountable: true,
954 quantity: 1, // 1 * 1000$ = 1000$ TAX INCLUSIVE
955 },
956 ],
957 shipping_methods: [
958 {
959 name: "test-method",
960 shipping_option_id: "test-option",
961 amount: 0,
962 },
963 ],
964 }
965
966 const response = await api.post(
967 "/admin/draft-orders",
968 payload,
969 adminHeaders
970 )
971
972 // begin Order Edit on a Draft Order
973 let orderPreview = (
974 await api.post(
975 `/admin/draft-orders/${response.data.draft_order.id}/edit`,
976 {},
977 adminHeaders
978 )
979 ).data.draft_order_preview
980
981 orderPreview = (
982 await api.post(
983 `/admin/draft-orders/${response.data.draft_order.id}/edit/promotions`,
984 {
985 promo_codes: [promotion.code],
986 },
987 adminHeaders
988 )
989 ).data.draft_order_preview
990
991 expect(orderPreview.total).toEqual(6300) // 2 * 3000$ - 10% + 1 * 1000$ - 10%
992 expect(orderPreview.original_total).toEqual(7000)
993 expect(orderPreview.discount_total).toEqual(700)
994
995 expect(orderPreview.items).toEqual(
996 expect.arrayContaining([
997 expect.objectContaining({
998 total: 5400, // 2 * 3000$ - 10% = 5400$
999 original_total: 6000,
1000 discount_total: 600,
1001 variant_id: product.variants[0].id,
1002 adjustments: expect.arrayContaining([
1003 expect.objectContaining({
1004 code: "testytest",
1005 amount: 600,
1006 promotion_id: promotion.id,
1007 is_tax_inclusive: true,
1008 }),
1009 ]),
1010 }),
1011 expect.objectContaining({
1012 total: 900, // 1000$ - 10% = 900$
1013 original_total: 1000,
1014 discount_total: 100,
1015 variant_id: product_2.variants[0].id,
1016 adjustments: expect.arrayContaining([
1017 expect.objectContaining({
1018 code: "testytest",
1019 amount: 100,
1020 promotion_id: promotion.id,
1021 is_tax_inclusive: true,
1022 }),
1023 ]),
1024 }),
1025 ])
1026 )
1027
1028 orderPreview = (
1029 await api.post(
1030 `/admin/draft-orders/${response.data.draft_order.id}/edit/confirm`,
1031 {},
1032 adminHeaders
1033 )
1034 ).data.draft_order_preview
1035
1036 const draftOrder = (
1037 await api.get(
1038 `/admin/draft-orders/${response.data.draft_order.id}?fields=+total,+original_total,+discount_total,+version,items.*`,
1039 adminHeaders
1040 )
1041 ).data.draft_order
1042
1043 expect(draftOrder.total).toEqual(6300) // 2 * 3000$ - 10% + 1 * 1000$ - 10%
1044 expect(draftOrder.original_total).toEqual(7000)
1045 expect(draftOrder.discount_total).toEqual(700)
1046 expect(draftOrder.version).toEqual(2)
1047
1048 expect(draftOrder.items).toEqual(
1049 expect.arrayContaining([
1050 expect.objectContaining({
1051 total: 5400,
1052 original_total: 6000,
1053 discount_total: 600,
1054 variant_id: product.variants[0].id,
1055 adjustments: [
1056 expect.objectContaining({
1057 code: "testytest",
1058 amount: 600,
1059 promotion_id: promotion.id,
1060 is_tax_inclusive: true,
1061 version: 2,
1062 }),
1063 ],
1064 }),
1065 expect.objectContaining({
1066 total: 900,
1067 original_total: 1000,
1068 discount_total: 100,
1069 variant_id: product_2.variants[0].id,
1070 adjustments: [
1071 expect.objectContaining({
1072 code: "testytest",
1073 amount: 100,
1074 promotion_id: promotion.id,
1075 is_tax_inclusive: true,
1076 version: 2,
1077 }),
1078 ],
1079 }),
1080 ])
1081 )
1082 })
1083
1084 it("should restore promotions and adjustments on a draft order after edit is canceled", async () => {
1085 const region = await regionModuleService.createRegions({
1086 name: "US",
1087 currency_code: "usd",
1088 })
1089
1090 const salesChannel = await scModuleService.createSalesChannels({
1091 name: "Webshop",
1092 })
1093
1094 const location = await stockLocationModule.createStockLocations({
1095 name: "Warehouse",
1096 })
1097
1098 const [product, product_2] = await productModule.createProducts([
1099 {
1100 title: "Test product",
1101 status: ProductStatus.PUBLISHED,
1102 variants: [
1103 {
1104 title: "Test variant",
1105 },
1106 ],
1107 },
1108 {
1109 title: "Another product",
1110 status: ProductStatus.PUBLISHED,
1111 variants: [
1112 {
1113 title: "Variant variable",
1114 manage_inventory: false,
1115 },
1116 ],
1117 },
1118 ])
1119
1120 const inventoryItem = await inventoryModule.createInventoryItems({
1121 sku: "inv-1234",
1122 })
1123
1124 await inventoryModule.createInventoryLevels([
1125 {
1126 inventory_item_id: inventoryItem.id,
1127 location_id: location.id,
1128 stocked_quantity: 2,
1129 reserved_quantity: 0,
1130 },
1131 ])
1132
1133 const [priceSet, priceSet_2] = await pricingModule.createPriceSets([
1134 {
1135 prices: [
1136 {
1137 amount: 3000,
1138 currency_code: "usd",
1139 },
1140 ],
1141 },
1142 {
1143 prices: [
1144 {
1145 amount: 1000,
1146 currency_code: "usd",
1147 },
1148 ],
1149 },
1150 ])
1151
1152 /**
1153 * Create a promotion to test with
1154 */
1155 const promotion = (
1156 await api.post(
1157 `/admin/promotions`,
1158 {
1159 code: "testytest",
1160 type: PromotionType.STANDARD,
1161 status: PromotionStatus.ACTIVE,
1162 is_tax_inclusive: true,
1163 application_method: {
1164 target_type: "items",
1165 type: "percentage",
1166 allocation: "each",
1167 currency_code: "usd",
1168 value: 10,
1169 max_quantity: 10,
1170 },
1171 },
1172 adminHeaders
1173 )
1174 ).data.promotion
1175
1176 // additional promotion that will be added then removed
1177 const promotionAdditional20 = (
1178 await api.post(
1179 `/admin/promotions`,
1180 {
1181 code: "additional_20",
1182 type: PromotionType.STANDARD,
1183 status: PromotionStatus.ACTIVE,
1184 is_tax_inclusive: true,
1185 application_method: {
1186 target_type: "items",
1187 type: "percentage",
1188 allocation: "each",
1189 currency_code: "usd",
1190 value: 20,
1191 max_quantity: 10,
1192 },
1193 },
1194 adminHeaders
1195 )
1196 ).data.promotion
1197
1198 await api.post(
1199 "/admin/price-preferences",
1200 {
1201 attribute: "currency_code",
1202 value: "usd",
1203 is_tax_inclusive: true,
1204 },
1205 adminHeaders
1206 )
1207
1208 await remoteLink.create([
1209 {
1210 [Modules.PRODUCT]: {
1211 variant_id: product.variants[0].id,
1212 },
1213 [Modules.PRICING]: {
1214 price_set_id: priceSet.id,
1215 },
1216 },
1217 {
1218 [Modules.PRODUCT]: {
1219 variant_id: product_2.variants[0].id,
1220 },
1221 [Modules.PRICING]: {
1222 price_set_id: priceSet_2.id,
1223 },
1224 },
1225 {
1226 [Modules.SALES_CHANNEL]: {
1227 sales_channel_id: salesChannel.id,
1228 },
1229 [Modules.STOCK_LOCATION]: {
1230 stock_location_id: location.id,
1231 },
1232 },
1233 {
1234 [Modules.PRODUCT]: {
1235 variant_id: product.variants[0].id,
1236 },
1237 [Modules.INVENTORY]: {
1238 inventory_item_id: inventoryItem.id,
1239 },
1240 },
1241 {
1242 [Modules.PRODUCT]: {
1243 variant_id: product_2.variants[0].id,
1244 },
1245 [Modules.INVENTORY]: {
1246 inventory_item_id: inventoryItem.id,
1247 },
1248 },
1249 ])
1250
1251 await setupTaxStructure(taxModule)
1252
1253 const payload = {
1254 email: "oli@test.dk",
1255 region_id: region.id,
1256 sales_channel_id: salesChannel.id,
1257 currency_code: "usd",
1258 shipping_address: {
1259 first_name: "Test",
1260 last_name: "Test",
1261 address_1: "Test",
1262 city: "Test",
1263 country_code: "US",
1264 postal_code: "12345",
1265 phone: "12345",
1266 },
1267 billing_address: {
1268 first_name: "Test",
1269 last_name: "Test",
1270 address_1: "Test",
1271 city: "Test",
1272 country_code: "US",
1273 postal_code: "12345",
1274 },
1275 items: [
1276 {
1277 variant_id: product.variants[0].id,
1278 is_discountable: true,
1279 quantity: 2, // 2 * 3000$ = 6000$ TAX INCLUSIVE
1280 },
1281 {
1282 variant_id: product_2.variants[0].id,
1283 is_discountable: true,
1284 quantity: 1, // 1 * 1000$ = 1000$ TAX INCLUSIVE
1285 },
1286 ],
1287 shipping_methods: [
1288 {
1289 name: "test-method",
1290 shipping_option_id: "test-option",
1291 amount: 0,
1292 },
1293 ],
1294 }
1295
1296 const response = await api.post(
1297 "/admin/draft-orders",
1298 payload,
1299 adminHeaders
1300 )
1301
1302 // begin Order Edit on a Draft Order
1303 let orderPreview = (
1304 await api.post(
1305 `/admin/draft-orders/${response.data.draft_order.id}/edit`,
1306 {},
1307 adminHeaders
1308 )
1309 ).data.draft_order_preview
1310
1311 // add the promotion
1312 orderPreview = (
1313 await api.post(
1314 `/admin/draft-orders/${response.data.draft_order.id}/edit/promotions`,
1315 {
1316 promo_codes: [promotion.code],
1317 },
1318 adminHeaders
1319 )
1320 ).data.draft_order_preview
1321
1322 // confirm changes which applies adjustemnts to the draft order
1323 orderPreview = (
1324 await api.post(
1325 `/admin/draft-orders/${response.data.draft_order.id}/edit/confirm`,
1326 {},
1327 adminHeaders
1328 )
1329 ).data.draft_order_preview
1330
1331 const draftOrder = (
1332 await api.get(
1333 `/admin/draft-orders/${response.data.draft_order.id}?fields=+total,+original_total,+discount_total,+version,items.*`,
1334 adminHeaders
1335 )
1336 ).data.draft_order
1337
1338 expect(draftOrder.total).toEqual(6300) // 2 * 3000$ - 10% + 1 * 1000$ - 10%
1339 expect(draftOrder.original_total).toEqual(7000)
1340 expect(draftOrder.discount_total).toEqual(700)
1341 expect(draftOrder.version).toEqual(2)
1342
1343 expect(draftOrder.items[0].adjustments[0].version).toEqual(2)
1344 expect(draftOrder.items[1].adjustments[0].version).toEqual(2)
1345
1346 expect(orderPreview.items).toEqual(
1347 expect.arrayContaining([
1348 expect.objectContaining({
1349 total: 5400,
1350 original_total: 6000,
1351 discount_total: 600,
1352 variant_id: product.variants[0].id,
1353 adjustments: [
1354 expect.objectContaining({
1355 code: "testytest",
1356 amount: 600,
1357 promotion_id: promotion.id,
1358 is_tax_inclusive: true,
1359 }),
1360 ],
1361 }),
1362 expect.objectContaining({
1363 total: 900,
1364 original_total: 1000,
1365 discount_total: 100,
1366 variant_id: product_2.variants[0].id,
1367 adjustments: [
1368 expect.objectContaining({
1369 code: "testytest",
1370 amount: 100,
1371 promotion_id: promotion.id,
1372 is_tax_inclusive: true,
1373 }),
1374 ],
1375 }),
1376 ])
1377 )
1378
1379 // begin another edit...
1380 orderPreview = (
1381 await api.post(
1382 `/admin/draft-orders/${response.data.draft_order.id}/edit`,
1383 {},
1384 adminHeaders
1385 )
1386 ).data.draft_order_preview
1387
1388 // ...and add the additional promotion
1389 orderPreview = (
1390 await api.post(
1391 `/admin/draft-orders/${response.data.draft_order.id}/edit/promotions`,
1392 {
1393 promo_codes: [promotionAdditional20.code],
1394 },
1395 adminHeaders
1396 )
1397 ).data.draft_order_preview
1398
1399 expect(orderPreview.total).toEqual(5040) // (2 * 3000$ - 10%) - 20% + (1 * 1000$ - 10%) - 20%
1400 expect(orderPreview.original_total).toEqual(7000)
1401 expect(orderPreview.discount_total).toEqual(600 + 100 + 1080 + 180)
1402
1403 expect(orderPreview.version).toEqual(2)
1404 expect(orderPreview.items[0].adjustments.length).toEqual(2)
1405 expect(orderPreview.items[1].adjustments.length).toEqual(2)
1406
1407 // new adjustments exists on the preview
1408 expect(orderPreview.items).toEqual(
1409 expect.arrayContaining([
1410 expect.objectContaining({
1411 total: 4320,
1412 original_total: 6000,
1413 discount_total: 1680,
1414 variant_id: product.variants[0].id,
1415 adjustments: expect.arrayContaining([
1416 expect.objectContaining({
1417 code: "testytest",
1418 amount: 480,
1419 promotion_id: promotion.id,
1420 is_tax_inclusive: true,
1421 }),
1422 expect.objectContaining({
1423 code: "additional_20",
1424 amount: 1200,
1425 promotion_id: promotionAdditional20.id,
1426 is_tax_inclusive: true,
1427 }),
1428 ]),
1429 }),
1430 expect.objectContaining({
1431 total: 720,
1432 original_total: 1000,
1433 discount_total: 280,
1434 variant_id: product_2.variants[0].id,
1435 adjustments: expect.arrayContaining([
1436 expect.objectContaining({
1437 code: "testytest",
1438 amount: 80,
1439 promotion_id: promotion.id,
1440 is_tax_inclusive: true,
1441 }),
1442 expect.objectContaining({
1443 code: "additional_20",
1444 amount: 200,
1445 promotion_id: promotionAdditional20.id,
1446 is_tax_inclusive: true,
1447 }),
1448 ]),
1449 }),
1450 ])
1451 )
1452
1453 await api.delete(
1454 `/admin/draft-orders/${response.data.draft_order.id}/edit`,
1455 adminHeaders
1456 )
1457
1458 const draftOrderAfterCancel = (
1459 await api.get(
1460 `/admin/draft-orders/${response.data.draft_order.id}?fields=+total,+original_total,+discount_total`,
1461 adminHeaders
1462 )
1463 ).data.draft_order
1464
1465 // reverted to be same as before second edit started (10% discount only applied)
1466 expect(draftOrderAfterCancel.total).toEqual(6300)
1467 expect(draftOrderAfterCancel.original_total).toEqual(7000)
1468 expect(draftOrderAfterCancel.discount_total).toEqual(700)
1469 expect(draftOrderAfterCancel.version).toEqual(2)
1470 })
1471
1472 it("should create a draft order and apply tax by product type", async () => {
1473 const productType = await productModule.createProductTypes({
1474 value: "test_product_type",
1475 })
1476
1477 const region = await regionModuleService.createRegions({
1478 name: "US",
1479 currency_code: "usd",
1480 })
1481
1482 const [taxRegion] = await taxModule.createTaxRegions([
1483 {
1484 country_code: "US",
1485 provider_id: "tp_system",
1486 default_tax_rate: {
1487 name: "US Default Rate",
1488 rate: 5,
1489 code: "US_DEF",
1490 },
1491 },
1492 ])
1493
1494 const [taxRate] = await taxModule.createTaxRates([
1495 {
1496 tax_region_id: taxRegion.id,
1497 name: "US Reduced",
1498 rate: 3,
1499 code: "USREDUCE_PROD_TYPE",
1500 },
1501 ])
1502
1503 await taxModule.createTaxRateRules([
1504 {
1505 reference: "product_type",
1506 reference_id: productType.id,
1507 tax_rate_id: taxRate.id,
1508 },
1509 ])
1510
1511 const salesChannel = await scModuleService.createSalesChannels({
1512 name: "Webshop",
1513 })
1514
1515 const location = await stockLocationModule.createStockLocations({
1516 name: "Warehouse",
1517 })
1518
1519 const [product] = await productModule.createProducts([
1520 {
1521 title: "Test product",
1522 status: ProductStatus.PUBLISHED,
1523 type_id: productType.id,
1524 variants: [
1525 {
1526 title: "Test variant",
1527 },
1528 ],
1529 },
1530 ])
1531
1532 const inventoryItem = await inventoryModule.createInventoryItems({
1533 sku: "inv-1234",
1534 })
1535
1536 await inventoryModule.createInventoryLevels([
1537 {
1538 inventory_item_id: inventoryItem.id,
1539 location_id: location.id,
1540 stocked_quantity: 2,
1541 reserved_quantity: 0,
1542 },
1543 ])
1544
1545 const [priceSet] = await pricingModule.createPriceSets([
1546 {
1547 prices: [
1548 {
1549 amount: 3000,
1550 currency_code: "usd",
1551 },
1552 ],
1553 },
1554 ])
1555
1556 await api.post(
1557 "/admin/price-preferences",
1558 {
1559 attribute: "currency_code",
1560 value: "usd",
1561 is_tax_inclusive: true,
1562 },
1563 adminHeaders
1564 )
1565
1566 /**
1567 * Create a promotion to test with
1568 */
1569 const promotion = (
1570 await api.post(
1571 `/admin/promotions`,
1572 {
1573 code: "testytest",
1574 type: PromotionType.STANDARD,
1575 status: PromotionStatus.ACTIVE,
1576 application_method: {
1577 target_type: "items",
1578 type: "fixed",
1579 allocation: "each",
1580 currency_code: "usd",
1581 value: 100,
1582 max_quantity: 100,
1583 target_rules: [
1584 {
1585 attribute: "items.variant_id",
1586 operator: "in",
1587 values: [product.variants[0].id],
1588 },
1589 ],
1590 },
1591 },
1592 adminHeaders
1593 )
1594 ).data.promotion
1595
1596 await remoteLink.create([
1597 {
1598 [Modules.PRODUCT]: {
1599 variant_id: product.variants[0].id,
1600 },
1601 [Modules.PRICING]: {
1602 price_set_id: priceSet.id,
1603 },
1604 },
1605 {
1606 [Modules.SALES_CHANNEL]: {
1607 sales_channel_id: salesChannel.id,
1608 },
1609 [Modules.STOCK_LOCATION]: {
1610 stock_location_id: location.id,
1611 },
1612 },
1613 {
1614 [Modules.PRODUCT]: {
1615 variant_id: product.variants[0].id,
1616 },
1617 [Modules.INVENTORY]: {
1618 inventory_item_id: inventoryItem.id,
1619 },
1620 },
1621 ])
1622
1623 const payload = {
1624 email: "oli@test.dk",
1625 region_id: region.id,
1626 sales_channel_id: salesChannel.id,
1627 currency_code: "usd",
1628 shipping_address: {
1629 first_name: "Test",
1630 last_name: "Test",
1631 address_1: "Test",
1632 city: "Test",
1633 country_code: "US",
1634 postal_code: "12345",
1635 phone: "12345",
1636 },
1637 billing_address: {
1638 first_name: "Test",
1639 last_name: "Test",
1640 address_1: "Test",
1641 city: "Test",
1642 country_code: "US",
1643 postal_code: "12345",
1644 },
1645 promo_codes: ["testytest"],
1646 items: [
1647 {
1648 variant_id: product.variants[0].id,
1649 product_type_id: productType.id,
1650 quantity: 2,
1651 },
1652 ],
1653 shipping_methods: [
1654 {
1655 name: "test-method",
1656 shipping_option_id: "test-option",
1657 amount: 100,
1658 },
1659 ],
1660 }
1661
1662 const response = await api.post(
1663 "/admin/draft-orders",
1664 payload,
1665 adminHeaders
1666 )
1667
1668 expect(response.data.draft_order.items[0].tax_lines[0].code).toEqual(
1669 "USREDUCE_PROD_TYPE"
1670 )
1671 expect(response.data.draft_order.items[0].tax_lines[0].rate).toEqual(3)
1672 })
1673 })
1674 },
1675 })