1 import { CreateOrderDTO, IOrderModuleService } from "@medusajs/framework/types"
2 import { Modules } from "@medusajs/framework/utils"
3 import {
4 SuiteOptions,
5 moduleIntegrationTestRunner,
6 normalizeBigNumbers,
7 } from "@medusajs/test-utils"
8
9 jest.setTimeout(100000)
10
11 moduleIntegrationTestRunner({
12 moduleName: Modules.ORDER,
13 testSuite: ({ service }: SuiteOptions<IOrderModuleService>) => {
14 describe("Order Module Service - Exchange flows", () => {
15 const input = {
16 email: "foo@bar.com",
17 items: [
18 {
19 title: "Item 1",
20 subtitle: "Subtitle 1",
21 thumbnail: "thumbnail1.jpg",
22 quantity: 1,
23 product_id: "product1",
24 product_title: "Product 1",
25 product_description: "Description 1",
26 product_subtitle: "Product Subtitle 1",
27 product_type: "Type 1",
28 product_type_id: "type_1",
29 product_collection: "Collection 1",
30 product_handle: "handle1",
31 variant_id: "variant1",
32 variant_sku: "SKU1",
33 variant_barcode: "Barcode1",
34 variant_title: "Variant 1",
35 variant_option_values: {
36 color: "Red",
37 size: "Large",
38 },
39 requires_shipping: true,
40 is_discountable: true,
41 is_tax_inclusive: true,
42 compare_at_unit_price: 10,
43 unit_price: 8,
44 tax_lines: [
45 {
46 description: "Tax 1",
47 tax_rate_id: "tax_usa",
48 code: "code",
49 rate: 0.1,
50 provider_id: "taxify_master",
51 },
52 ],
53 adjustments: [
54 {
55 code: "VIP_10",
56 amount: 10,
57 description: "VIP discount",
58 promotion_id: "prom_123",
59 provider_id: "coupon_kings",
60 },
61 ],
62 },
63 {
64 title: "Item 2",
65 quantity: 2,
66 unit_price: 5,
67 },
68 {
69 title: "Item 3",
70 quantity: 1,
71 unit_price: 30,
72 },
73 ],
74 sales_channel_id: "test",
75 shipping_address: {
76 first_name: "Test",
77 last_name: "Test",
78 address_1: "Test",
79 city: "Test",
80 country_code: "US",
81 postal_code: "12345",
82 phone: "12345",
83 },
84 billing_address: {
85 first_name: "Test",
86 last_name: "Test",
87 address_1: "Test",
88 city: "Test",
89 country_code: "US",
90 postal_code: "12345",
91 },
92 shipping_methods: [
93 {
94 name: "Test shipping method",
95 amount: 10,
96 },
97 ],
98 transactions: [
99 {
100 amount: 58,
101 currency_code: "USD",
102 reference: "payment",
103 reference_id: "pay_123",
104 },
105 ],
106 currency_code: "usd",
107 customer_id: "joe",
108 } as CreateOrderDTO
109
110 it("should exchange an item and add two new items to the order", async function () {
111 const createdOrder = await service.createOrders(input)
112 createdOrder.items = createdOrder.items!.sort((a, b) =>
113 a.title.localeCompare(b.title)
114 )
115
116 // Fullfilment
117 await service.registerFulfillment({
118 order_id: createdOrder.id,
119 items: createdOrder.items!.map((item) => {
120 return {
121 id: item.id,
122 quantity: item.quantity,
123 }
124 }),
125 })
126
127 // Shipment
128 await service.registerShipment({
129 order_id: createdOrder.id,
130 reference: Modules.FULFILLMENT,
131 items: createdOrder.items!.map((item) => {
132 return {
133 id: item.id,
134 quantity: item.quantity,
135 }
136 }),
137 })
138
139 // Exchange
140 const reason = await service.createReturnReasons({
141 value: "wrong-size",
142 label: "Wrong Size",
143 })
144
145 const orderExchange = await service.createExchange({
146 order_id: createdOrder.id,
147 description: "Exchange all the items",
148 internal_note: "user wants to return all items",
149 difference_due: 14,
150 shipping_methods: [
151 {
152 name: "Exchange method",
153 amount: 35,
154 provider_id: "dhl",
155 },
156 ],
157 return_items: [
158 {
159 id: createdOrder.items![0].id,
160 reason_id: reason.id,
161 quantity: 1,
162 note: "I don't need this",
163 },
164 ],
165 additional_items: [
166 {
167 id: createdOrder.items![2].id,
168 quantity: 1,
169 },
170 {
171 unit_price: 20,
172 quantity: 1,
173 title: "New item",
174 },
175 ],
176 return_amount: 5,
177 return_shipping: {
178 name: "return shipping method",
179 amount: 10,
180 provider_id: "test_provider_id",
181 },
182 })
183
184 expect(normalizeBigNumbers(orderExchange)).toEqual(
185 expect.objectContaining({
186 id: orderExchange.id,
187 order_id: createdOrder.id,
188 return: expect.objectContaining({
189 order_id: createdOrder.id,
190 exchange_id: orderExchange.id,
191 status: "requested",
192 items: expect.arrayContaining([
193 expect.objectContaining({
194 item_id: createdOrder.items![0].id,
195 quantity: 1,
196 }),
197 ]),
198 }),
199 additional_items: expect.arrayContaining([
200 expect.objectContaining({
201 exchange_id: orderExchange.id,
202 quantity: 1,
203 item: expect.objectContaining({
204 title: "New item",
205 }),
206 detail: expect.objectContaining({
207 quantity: 1,
208 }),
209 }),
210 expect.objectContaining({
211 exchange_id: orderExchange.id,
212 item_id: createdOrder.items![2].id,
213 quantity: 1,
214 item: expect.objectContaining({
215 title: "Item 3",
216 }),
217 detail: expect.objectContaining({
218 quantity: 2,
219 }),
220 }),
221 ]),
222 shipping_methods: expect.arrayContaining([
223 expect.objectContaining({
224 name: "return shipping method",
225 amount: 10,
226 }),
227 expect.objectContaining({
228 name: "Exchange method",
229 amount: 35,
230 }),
231 ]),
232 difference_due: 14,
233 })
234 )
235 })
236 })
237 },
238 })