1 import { CreateOrderDTO, IOrderModuleService } from "@medusajs/framework/types"
2 import { ClaimType, Modules } from "@medusajs/framework/utils"
3 import {
4 SuiteOptions,
5 moduleIntegrationTestRunner,
6 normalizeBigNumbers,
7 } from "@medusajs/test-utils"
8
9 jest.setTimeout(1000000)
10
11 moduleIntegrationTestRunner({
12 moduleName: Modules.ORDER,
13 testSuite: ({ service }: SuiteOptions<IOrderModuleService>) => {
14 describe("Order Module Service - Claim 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 claim 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 // Claim
140 const orderClaim = await service.createClaim({
141 order_id: createdOrder.id,
142 type: ClaimType.REPLACE,
143 description: "Claim all the items",
144 internal_note: "user wants to return all items",
145 shipping_methods: [
146 {
147 shipping_option_id: "shipping_dhl_id",
148 name: "Claim method",
149 amount: 35,
150 },
151 ],
152 claim_items: [
153 {
154 id: createdOrder.items![1].id,
155 quantity: 1,
156 reason: "production_failure",
157 },
158 ],
159 additional_items: [
160 {
161 id: createdOrder.items![2].id,
162 quantity: 1,
163 },
164 {
165 unit_price: 20,
166 quantity: 1,
167 title: "New item",
168 },
169 ],
170 return_amount: 5,
171 return_shipping: {
172 name: "return shipping method",
173 amount: 10,
174 provider_id: "test_provider_id",
175 },
176 })
177
178 expect(normalizeBigNumbers(orderClaim)).toEqual(
179 expect.objectContaining({
180 id: orderClaim.id,
181 order_id: createdOrder.id,
182 return: expect.objectContaining({
183 order_id: createdOrder.id,
184 claim_id: orderClaim.id,
185 status: "requested",
186 items: expect.arrayContaining([
187 expect.objectContaining({
188 item_id: createdOrder.items![1].id,
189 quantity: 1,
190 }),
191 ]),
192 }),
193 type: "replace",
194 additional_items: expect.arrayContaining([
195 expect.objectContaining({
196 claim_id: orderClaim.id,
197 is_additional_item: true,
198 quantity: 1,
199 item: expect.objectContaining({
200 title: "New item",
201 }),
202 detail: expect.objectContaining({
203 quantity: 1,
204 }),
205 }),
206 expect.objectContaining({
207 claim_id: orderClaim.id,
208 item_id: createdOrder.items![2].id,
209 is_additional_item: true,
210 quantity: 1,
211 item: expect.objectContaining({
212 title: "Item 3",
213 }),
214 detail: expect.objectContaining({
215 quantity: 2,
216 }),
217 }),
218 ]),
219 claim_items: [
220 expect.objectContaining({
221 reason: "production_failure",
222 claim_id: orderClaim.id,
223 is_additional_item: false,
224 quantity: 1,
225 item: expect.objectContaining({
226 title: "Item 2",
227 }),
228 detail: expect.objectContaining({
229 quantity: 2,
230 }),
231 }),
232 ],
233 shipping_methods: expect.arrayContaining([
234 expect.objectContaining({
235 name: "return shipping method",
236 amount: 10,
237 }),
238 expect.objectContaining({
239 name: "Claim method",
240 amount: 35,
241 }),
242 ]),
243 refund_amount: null,
244 })
245 )
246 })
247 })
248 },
249 })