1 import {
2 createOrderChangeWorkflow,
3 createOrderWorkflow,
4 } from "@medusajs/core-flows"
5 import {
6 medusaIntegrationTestRunner,
7 normalizeBigNumbers,
8 } from "@medusajs/test-utils"
9 import {
10 CreateOrderLineItemDTO,
11 IOrderModuleService,
12 OrderDTO,
13 } from "@medusajs/types"
14 import { Modules, ProductStatus } from "@medusajs/utils"
15 import {
16 adminHeaders,
17 createAdminUser,
18 } from "../../../helpers/create-admin-user"
19
20 jest.setTimeout(50000)
21
22 const env = {}
23
24 medusaIntegrationTestRunner({
25 env,
26 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
27 let appContainer
28 let orderModule: IOrderModuleService
29
30 beforeAll(async () => {
31 appContainer = getContainer()
32 orderModule = appContainer.resolve(Modules.ORDER)
33 })
34
35 beforeAll(async () => {
36 await createAdminUser(dbConnection, adminHeaders, appContainer)
37
38 await dbUtils.snapshot()
39 })
40
41 describe("CreateOrderWorkflow", () => {
42 it("should create an order with items quantity and no unit price and calculate prices based on the correct pricing context including quantity", async () => {
43 const salesChannel = await api.post(
44 "/admin/sales-channels",
45 {
46 name: "Test Sales Channel",
47 description: "Test Sales Channel Description",
48 },
49 adminHeaders
50 )
51
52 const productData = {
53 title: "Medusa T-Shirt based quantity",
54 handle: "t-shirt-with-quantity-prices",
55 status: ProductStatus.PUBLISHED,
56 sales_channels: [
57 {
58 id: salesChannel.data.sales_channel.id,
59 },
60 ],
61 options: [
62 {
63 title: "Size",
64 values: ["S"],
65 },
66 ],
67 variants: [
68 {
69 title: "S",
70 sku: "SHIRT-S-BLACK-w-quantity-prices",
71 options: {
72 Size: "S",
73 },
74 manage_inventory: false,
75 prices: [
76 {
77 amount: 1500,
78 currency_code: "usd",
79 min_quantity: 1,
80 max_quantity: 4,
81 },
82 {
83 amount: 1000,
84 currency_code: "usd",
85 min_quantity: 5,
86 max_quantity: 10,
87 },
88 ],
89 },
90 ],
91 }
92
93 const newProduct = await api.post(
94 `/admin/products`,
95 productData,
96 adminHeaders
97 )
98
99 const variantId = newProduct.data.product.variants[0].id
100 const salesChannelId = salesChannel.data.sales_channel.id
101 const customer = (
102 await api.post(
103 "/admin/customers",
104 {
105 email: "test1@email.com",
106 },
107 adminHeaders
108 )
109 ).data.customer
110 const region = (
111 await api.post(
112 "/admin/regions",
113 { name: "US", currency_code: "usd", countries: ["us"] },
114 adminHeaders
115 )
116 ).data.region
117
118 const { result: created } = await createOrderWorkflow(appContainer).run(
119 {
120 input: {
121 email: customer.email,
122 metadata: {
123 foo: "bar",
124 },
125 items: [
126 {
127 title: "Medusa T-Shirt based quantity",
128 variant_id: variantId,
129 quantity: 6,
130 } as CreateOrderLineItemDTO,
131 ],
132 sales_channel_id: salesChannelId,
133 region_id: region.id,
134 shipping_address: {
135 first_name: "Test",
136 last_name: "Test",
137 address_1: "Test",
138 city: "Test",
139 country_code: "US",
140 postal_code: "12345",
141 phone: "12345",
142 },
143 billing_address: {
144 first_name: "Test",
145 last_name: "Test",
146 address_1: "Test",
147 city: "Test",
148 country_code: "US",
149 postal_code: "12345",
150 },
151 shipping_methods: [
152 {
153 name: "Test shipping method",
154 amount: 10,
155 data: {},
156 tax_lines: [
157 {
158 description: "shipping Tax 1",
159 tax_rate_id: "tax_usa_shipping",
160 code: "code",
161 rate: 10,
162 },
163 ],
164 adjustments: [
165 {
166 code: "VIP_10",
167 amount: 1,
168 description: "VIP discount",
169 promotion_id: "prom_123",
170 },
171 ],
172 },
173 ],
174 currency_code: "usd",
175 customer_id: customer.id,
176 },
177 }
178 )
179
180 const order = (
181 await api.get(
182 "/admin/orders/" +
183 created.id +
184 "?fields=+raw_total,+raw_subtotal,+raw_discount_total",
185 adminHeaders
186 )
187 ).data.order
188
189 expect(order).toEqual(
190 expect.objectContaining({
191 original_item_subtotal: 6000,
192 original_item_tax_total: 0,
193 original_item_total: 6000,
194 original_shipping_subtotal: 10,
195 original_shipping_tax_total: 1,
196 original_shipping_total: 11,
197 original_tax_total: 1,
198 original_total: 6011,
199 item_subtotal: 6000,
200 item_tax_total: 0,
201 item_total: 6000,
202 })
203 )
204 })
205 })
206
207 describe("Orders - Admin", () => {
208 it("should get an order", async () => {
209 const created = await orderModule.createOrders({
210 region_id: "test_region_id",
211 email: "foo@bar.com",
212 metadata: {
213 foo: "bar",
214 },
215 items: [
216 {
217 title: "Custom Item 2",
218 quantity: 1,
219 unit_price: 50,
220 adjustments: [
221 {
222 code: "VIP_25 ETH",
223 amount: "0.000000000000000005",
224 description: "VIP discount",
225 promotion_id: "prom_123",
226 provider_id: "coupon_kings",
227 },
228 ],
229 },
230 ],
231 sales_channel_id: "test",
232 shipping_address: {
233 first_name: "Test",
234 last_name: "Test",
235 address_1: "Test",
236 city: "Test",
237 country_code: "US",
238 postal_code: "12345",
239 phone: "12345",
240 },
241 billing_address: {
242 first_name: "Test",
243 last_name: "Test",
244 address_1: "Test",
245 city: "Test",
246 country_code: "US",
247 postal_code: "12345",
248 },
249 shipping_methods: [
250 {
251 name: "Test shipping method",
252 amount: 10,
253 data: {},
254 tax_lines: [
255 {
256 description: "shipping Tax 1",
257 tax_rate_id: "tax_usa_shipping",
258 code: "code",
259 rate: 10,
260 },
261 ],
262 adjustments: [
263 {
264 code: "VIP_10",
265 amount: 1,
266 description: "VIP discount",
267 promotion_id: "prom_123",
268 },
269 ],
270 },
271 ],
272 currency_code: "usd",
273 customer_id: "joe",
274 })
275
276 const response = await api.get(
277 "/admin/orders/" +
278 created.id +
279 "?fields=+raw_total,+raw_subtotal,+raw_discount_total",
280 adminHeaders
281 )
282
283 const order = response.data.order
284 expect(order).toEqual({
285 id: expect.any(String),
286 status: "pending",
287 version: 1,
288 display_id: created.display_id,
289 custom_display_id: null,
290 payment_collections: [],
291 payment_status: "not_paid",
292 region_id: "test_region_id",
293 fulfillments: [],
294 locale: null,
295 metadata: {
296 foo: "bar",
297 },
298 fulfillment_status: "not_fulfilled",
299 summary: expect.objectContaining({
300 // TODO: add all summary fields
301 }),
302 total: 59.9,
303 subtotal: 60,
304 tax_total: 0.9,
305 discount_total: 1.1,
306 discount_tax_total: 0.1,
307 original_total: 61,
308 original_subtotal: 60,
309 original_tax_total: 1,
310 item_total: 50,
311 item_subtotal: 50,
312 item_tax_total: 0,
313 original_item_total: 50,
314 original_item_subtotal: 50,
315 original_item_tax_total: 0,
316 shipping_total: 9.9,
317 shipping_subtotal: 10,
318 shipping_tax_total: 0.9,
319 original_shipping_tax_total: 1,
320 original_shipping_subtotal: 10,
321 original_shipping_total: 11,
322 created_at: expect.any(String),
323 updated_at: expect.any(String),
324 raw_total: {
325 value: "59.899999999999999995",
326 precision: 20,
327 },
328 raw_subtotal: {
329 value: "60",
330 precision: 20,
331 },
332 raw_discount_total: {
333 value: "1.100000000000000005",
334 precision: 20,
335 },
336 items: [
337 {
338 id: expect.any(String),
339 title: "Custom Item 2",
340 subtitle: null,
341 thumbnail: null,
342 variant_id: null,
343 product_id: null,
344 product_title: null,
345 product_description: null,
346 product_subtitle: null,
347 product_type: null,
348 product_type_id: null,
349 product_collection: null,
350 product_handle: null,
351 variant_sku: null,
352 variant_barcode: null,
353 variant_title: null,
354 variant_option_values: null,
355 requires_shipping: true,
356 is_discountable: true,
357 is_giftcard: false,
358 is_tax_inclusive: false,
359 raw_compare_at_unit_price: null,
360 raw_unit_price: {
361 value: "50",
362 precision: 20,
363 },
364 is_custom_price: false,
365 metadata: null,
366 line_item_metadata: null,
367 created_at: expect.any(String),
368 updated_at: expect.any(String),
369 deleted_at: null,
370 tax_lines: [],
371 adjustments: expect.arrayContaining([
372 {
373 id: expect.any(String),
374 description: "VIP discount",
375 promotion_id: expect.any(String),
376 code: "VIP_25 ETH",
377 is_tax_inclusive: false,
378 raw_amount: {
379 value: "5e-18",
380 precision: 20,
381 },
382 version: 1,
383 provider_id: expect.any(String),
384 created_at: expect.any(String),
385 updated_at: expect.any(String),
386 deleted_at: null,
387 item_id: expect.any(String),
388 amount: 0,
389 subtotal: 0,
390 total: 0,
391 raw_subtotal: {
392 value: "5e-18",
393 precision: 20,
394 },
395 raw_total: {
396 value: "5e-18",
397 precision: 20,
398 },
399 },
400 ]),
401 compare_at_unit_price: null,
402 unit_price: 50,
403 quantity: 1,
404 raw_quantity: {
405 value: "1",
406 precision: 20,
407 },
408 detail: expect.objectContaining({
409 id: expect.any(String),
410 order_id: expect.any(String),
411 version: 1,
412 item_id: expect.any(String),
413 raw_quantity: {
414 value: "1",
415 precision: 20,
416 },
417 raw_fulfilled_quantity: {
418 value: "0",
419 precision: 20,
420 },
421 raw_shipped_quantity: {
422 value: "0",
423 precision: 20,
424 },
425 raw_return_requested_quantity: {
426 value: "0",
427 precision: 20,
428 },
429 raw_return_received_quantity: {
430 value: "0",
431 precision: 20,
432 },
433 raw_return_dismissed_quantity: {
434 value: "0",
435 precision: 20,
436 },
437 raw_written_off_quantity: {
438 value: "0",
439 precision: 20,
440 },
441 metadata: null,
442 created_at: expect.any(String),
443 updated_at: expect.any(String),
444 deleted_at: null,
445 quantity: 1,
446 fulfilled_quantity: 0,
447 shipped_quantity: 0,
448 return_requested_quantity: 0,
449 return_received_quantity: 0,
450 return_dismissed_quantity: 0,
451 written_off_quantity: 0,
452 }),
453 subtotal: 50,
454 total: 50,
455 original_total: 50,
456 original_subtotal: 50,
457 discount_total: 0,
458 discount_tax_total: 0,
459 discount_subtotal: 0,
460 tax_total: 0,
461 original_tax_total: 0,
462 refundable_total: 50,
463 refundable_total_per_unit: 50,
464 fulfilled_total: 0,
465 return_dismissed_total: 0,
466 return_received_total: 0,
467 return_requested_total: 0,
468 shipped_total: 0,
469 write_off_total: 0,
470 raw_subtotal: {
471 value: "50",
472 precision: 20,
473 },
474 raw_total: {
475 value: "49.999999999999999995",
476 precision: 20,
477 },
478 raw_original_total: {
479 value: "50",
480 precision: 20,
481 },
482 raw_discount_total: {
483 value: "5e-18",
484 precision: 20,
485 },
486 raw_discount_subtotal: {
487 precision: 20,
488 value: "5e-18",
489 },
490 raw_discount_tax_total: {
491 value: "0",
492 precision: 20,
493 },
494 raw_tax_total: {
495 value: "0",
496 precision: 20,
497 },
498 raw_original_tax_total: {
499 value: "0",
500 precision: 20,
501 },
502 raw_refundable_total: {
503 precision: 20,
504 value: "49.999999999999999995",
505 },
506 raw_refundable_total_per_unit: {
507 precision: 20,
508 value: "49.999999999999999995",
509 },
510 raw_fulfilled_total: {
511 precision: 20,
512 value: "0",
513 },
514 raw_original_subtotal: {
515 precision: 20,
516 value: "50",
517 },
518 raw_return_dismissed_total: {
519 precision: 20,
520 value: "0",
521 },
522 raw_return_received_total: {
523 precision: 20,
524 value: "0",
525 },
526 raw_return_requested_total: {
527 precision: 20,
528 value: "0",
529 },
530 raw_shipped_total: {
531 precision: 20,
532 value: "0",
533 },
534 raw_write_off_total: {
535 precision: 20,
536 value: "0",
537 },
538 },
539 ],
540 shipping_address: {
541 id: expect.any(String),
542 customer_id: null,
543 company: null,
544 first_name: "Test",
545 last_name: "Test",
546 address_1: "Test",
547 address_2: null,
548 city: "Test",
549 country_code: "US",
550 province: null,
551 postal_code: "12345",
552 phone: "12345",
553 metadata: null,
554 created_at: expect.any(String),
555 updated_at: expect.any(String),
556 deleted_at: null,
557 },
558 billing_address: {
559 id: expect.any(String),
560 customer_id: null,
561 company: null,
562 first_name: "Test",
563 last_name: "Test",
564 address_1: "Test",
565 address_2: null,
566 city: "Test",
567 country_code: "US",
568 province: null,
569 postal_code: "12345",
570 phone: null,
571 metadata: null,
572 created_at: expect.any(String),
573 updated_at: expect.any(String),
574 deleted_at: null,
575 },
576 shipping_methods: [
577 expect.objectContaining({
578 id: expect.any(String),
579 order_id: expect.any(String),
580 name: "Test shipping method",
581 description: null,
582 raw_amount: {
583 value: "10",
584 precision: 20,
585 },
586 is_tax_inclusive: false,
587 shipping_option_id: null,
588 data: {},
589 metadata: null,
590 created_at: expect.any(String),
591 updated_at: expect.any(String),
592 tax_lines: expect.arrayContaining([
593 {
594 id: expect.any(String),
595 description: "shipping Tax 1",
596 tax_rate_id: expect.any(String),
597 code: "code",
598 raw_rate: {
599 value: "10",
600 precision: 20,
601 },
602 provider_id: null,
603 data: null,
604 metadata: null,
605 created_at: expect.any(String),
606 updated_at: expect.any(String),
607 deleted_at: null,
608 shipping_method_id: expect.any(String),
609 rate: 10,
610 total: 0.9,
611 subtotal: 1,
612 raw_total: {
613 value: "0.9",
614 precision: 20,
615 },
616 raw_subtotal: {
617 value: "1",
618 precision: 20,
619 },
620 },
621 ]),
622 adjustments: expect.arrayContaining([
623 {
624 id: expect.any(String),
625 description: "VIP discount",
626 promotion_id: expect.any(String),
627 code: "VIP_10",
628 raw_amount: {
629 value: "1",
630 precision: 20,
631 },
632 provider_id: null,
633 created_at: expect.any(String),
634 updated_at: expect.any(String),
635 deleted_at: null,
636 shipping_method_id: expect.any(String),
637 amount: 1,
638 subtotal: 1,
639 total: 1.1,
640 raw_subtotal: {
641 value: "1",
642 precision: 20,
643 },
644 raw_total: {
645 value: "1.1",
646 precision: 20,
647 },
648 version: 1,
649 },
650 ]),
651 amount: 10,
652 subtotal: 10,
653 total: 9.9,
654 original_total: 11,
655 original_subtotal: 10,
656 discount_total: 1.1,
657 discount_subtotal: 1,
658 discount_tax_total: 0.1,
659 tax_total: 0.9,
660 original_tax_total: 1,
661 is_custom_amount: false,
662 deleted_at: null,
663 detail: expect.objectContaining({
664 id: expect.any(String),
665 order_id: expect.any(String),
666 shipping_method_id: expect.any(String),
667 version: 1,
668 created_at: expect.any(String),
669 updated_at: expect.any(String),
670 deleted_at: null,
671 claim_id: null,
672 exchange_id: null,
673 return_id: null,
674 }),
675 raw_subtotal: {
676 value: "10",
677 precision: 20,
678 },
679 raw_total: {
680 value: "9.9",
681 precision: 20,
682 },
683 raw_original_total: {
684 value: "11",
685 precision: 20,
686 },
687 raw_original_subtotal: {
688 value: "10",
689 precision: 20,
690 },
691 raw_discount_total: {
692 value: "1.1",
693 precision: 20,
694 },
695 raw_discount_subtotal: {
696 value: "1",
697 precision: 20,
698 },
699 raw_discount_tax_total: {
700 value: "0.1",
701 precision: 20,
702 },
703 raw_tax_total: {
704 value: "0.9",
705 precision: 20,
706 },
707 raw_original_tax_total: {
708 value: "1",
709 precision: 20,
710 },
711 }),
712 ],
713 credit_lines: [],
714 credit_line_subtotal: 0,
715 credit_line_tax_total: 0,
716 credit_line_total: 0,
717 })
718 })
719 })
720
721 it("should delete an order and related entities", async () => {
722 const toDeleteOrder = await orderModule.createOrders({
723 region_id: "test_region_id",
724 email: "foo@bar.com",
725 metadata: {
726 foo: "bar",
727 },
728 items: [
729 {
730 title: "Custom Item 1",
731 quantity: 1,
732 unit_price: 20,
733 adjustments: [
734 {
735 code: "VIP_25 ETH",
736 amount: "0.000000000000000005",
737 description: "VIP discount",
738 promotion_id: "prom_123",
739 provider_id: "coupon_kings",
740 },
741 ],
742 },
743 ],
744 sales_channel_id: "test",
745 shipping_address: {
746 first_name: "Shipping 1",
747 last_name: "Test 1",
748 address_1: "Test 1",
749 city: "Test 1",
750 country_code: "US",
751 postal_code: "12345",
752 phone: "12345",
753 },
754 billing_address: {
755 first_name: "Billing 1",
756 last_name: "Test 1",
757 address_1: "Test 1",
758 city: "Test 1",
759 country_code: "US",
760 postal_code: "12345",
761 },
762 shipping_methods: [
763 {
764 name: "Test shipping method",
765 amount: 10,
766 data: {},
767 tax_lines: [
768 {
769 description: "shipping Tax 1",
770 tax_rate_id: "tax_usa_shipping",
771 code: "code",
772 rate: 10,
773 },
774 ],
775 adjustments: [
776 {
777 code: "VIP_10",
778 amount: 1,
779 description: "VIP discount",
780 promotion_id: "prom_123",
781 },
782 ],
783 },
784 ],
785 currency_code: "usd",
786 customer_id: "joe",
787 })
788
789 const persistedOrder = (await orderModule.createOrders({
790 region_id: "test_region_id",
791 email: "foo@bar.com",
792 metadata: {
793 foo: "bar",
794 },
795 items: [
796 {
797 title: "Custom Item 2",
798 quantity: 1,
799 unit_price: 50,
800 adjustments: [
801 {
802 code: "VIP_25 ETH",
803 amount: "0.000000000000000005",
804 description: "VIP discount",
805 promotion_id: "prom_123",
806 provider_id: "coupon_kings",
807 },
808 ],
809 },
810 ],
811 sales_channel_id: "test",
812 shipping_address: {
813 first_name: "Shipping 2",
814 last_name: "Test 2",
815 address_1: "Test 2",
816 city: "Test 2",
817 country_code: "US",
818 postal_code: "12345",
819 phone: "12345",
820 },
821 billing_address: {
822 first_name: "Billing 2",
823 last_name: "Test 2",
824 address_1: "Test 2",
825 city: "Test 2",
826 country_code: "US",
827 postal_code: "12345",
828 },
829 shipping_methods: [
830 {
831 name: "Test shipping method",
832 amount: 10,
833 data: {},
834 tax_lines: [
835 {
836 description: "shipping Tax 2",
837 tax_rate_id: "tax_usa_shipping",
838 code: "code",
839 rate: 10,
840 },
841 ],
842 adjustments: [
843 {
844 code: "VIP_10",
845 amount: 1,
846 description: "VIP discount",
847 promotion_id: "prom_123",
848 },
849 ],
850 },
851 ],
852 currency_code: "usd",
853 customer_id: "joe",
854 })) as OrderDTO & {
855 shipping_address_id: string
856 billing_address_id: string
857 }
858
859 const { result: toDeleteOrderEdit } = await createOrderChangeWorkflow(
860 appContainer
861 ).run({
862 input: {
863 order_id: toDeleteOrder.id,
864 },
865 })
866
867 const { result: persistedOrderEdit } = await createOrderChangeWorkflow(
868 appContainer
869 ).run({
870 input: {
871 order_id: persistedOrder.id,
872 },
873 })
874
875 await orderModule.deleteOrders([toDeleteOrder.id])
876
877 const orderItems = (await dbConnection.raw("select * from order_item;"))
878 .rows
879
880 expect(orderItems.length).toBe(1)
881 expect(orderItems[0].id).toBe(persistedOrder.items[0].detail.id)
882
883 /**
884 * ORDER ITEMS AND LINE ITEMS
885 */
886
887 const orderLineItems = (
888 await dbConnection.raw("select * from order_line_item;")
889 ).rows
890
891 expect(orderLineItems.length).toBe(1)
892 expect(orderLineItems[0].id).toBe(persistedOrder.items[0].id)
893
894 const orderShipping = (
895 await dbConnection.raw("select * from order_shipping;")
896 ).rows
897
898 expect(orderShipping.length).toBe(1)
899 expect(orderShipping[0]).toEqual(
900 expect.objectContaining({
901 order_id: persistedOrder.id,
902 shipping_method_id: persistedOrder.shipping_methods[0].id,
903 })
904 )
905
906 /**
907 * ORDER SHIPPING AND SHIPPING METHODS
908 */
909
910 const orderShippingMethod = (
911 await dbConnection.raw("select * from order_shipping_method;")
912 ).rows
913
914 expect(orderShippingMethod.length).toBe(1)
915 expect(orderShippingMethod[0]).toEqual(
916 expect.objectContaining({
917 id: persistedOrder.shipping_methods[0].id,
918 })
919 )
920
921 /**
922 * ORDER BILLING AND SHIPPING ADDRESSES
923 */
924
925 const addresses = (await dbConnection.raw("select * from order_address;"))
926 .rows
927
928 expect(addresses.length).toBe(2)
929 expect(addresses.map((a) => a.id)).toEqual(
930 expect.arrayContaining([
931 persistedOrder.shipping_address_id,
932 persistedOrder.billing_address_id,
933 ])
934 )
935
936 /**
937 * ORDER SUMMARY
938 */
939
940 const orderSummary = (
941 await dbConnection.raw("select * from order_summary;")
942 ).rows
943
944 expect(orderSummary.length).toBe(1)
945 expect(orderSummary[0].totals.original_order_total).toBe(
946 normalizeBigNumbers(persistedOrder.summary.original_order_total)
947 )
948
949 /**
950 * ORDER CHANGES
951 */
952
953 const orderChangeRows = (
954 await dbConnection.raw("select * from order_change;")
955 ).rows
956
957 expect(orderChangeRows.length).toBe(1)
958 expect(orderChangeRows[0].id).toBe(persistedOrderEdit.id)
959
960 const orders = (
961 await api.get("/admin/orders?fields=*shipping_address", adminHeaders)
962 ).data.orders
963
964 expect(orders.length).toBe(1)
965 expect(orders[0].id).toBe(persistedOrder.id)
966 })
967 },
968 })