1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { ClaimType, Modules, RuleOperator } from "@medusajs/utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7 import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
8 import { createOrderSeeder } from "../../fixtures/order"
9
10 jest.setTimeout(300000)
11
12 medusaIntegrationTestRunner({
13 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
14 let order
15 let shippingProfile
16 let fulfillmentSet
17 let location
18 let item
19 let returnShippingOption
20 let outboundShippingOption
21 const shippingProviderId = "manual_test-provider"
22
23 beforeAll(async () => {
24 const container = getContainer()
25
26 await setupTaxStructure(container.resolve(Modules.TAX))
27 await createAdminUser(dbConnection, adminHeaders, container)
28
29 const inventoryItemOverride = (
30 await api.post(
31 `/admin/inventory-items`,
32 { sku: "test-variant", requires_shipping: false },
33 adminHeaders
34 )
35 ).data.inventory_item
36
37 const seeders = await createOrderSeeder({
38 api,
39 container,
40 inventoryItemOverride,
41 withoutShipping: true,
42 })
43 order = seeders.order
44
45 shippingProfile = (
46 await api.post(
47 `/admin/shipping-profiles`,
48 { name: "Test", type: "default" },
49 adminHeaders
50 )
51 ).data.shipping_profile
52
53 location = (
54 await api.post(
55 `/admin/stock-locations`,
56 { name: "Test location" },
57 adminHeaders
58 )
59 ).data.stock_location
60
61 location = (
62 await api.post(
63 `/admin/stock-locations/${location.id}/fulfillment-sets?fields=*fulfillment_sets`,
64 { name: "Test", type: "test-type" },
65 adminHeaders
66 )
67 ).data.stock_location
68
69 fulfillmentSet = (
70 await api.post(
71 `/admin/fulfillment-sets/${location.fulfillment_sets[0].id}/service-zones`,
72 {
73 name: "Test",
74 geo_zones: [{ type: "country", country_code: "us" }],
75 },
76 adminHeaders
77 )
78 ).data.fulfillment_set
79
80 const inventoryItem = (
81 await api.get(`/admin/inventory-items?sku=test-variant`, adminHeaders)
82 ).data.inventory_items[0]
83
84 await api.post(
85 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
86 {
87 location_id: location.id,
88 stocked_quantity: 10,
89 },
90 adminHeaders
91 )
92
93 await api.post(
94 `/admin/stock-locations/${location.id}/fulfillment-providers`,
95 { add: [shippingProviderId] },
96 adminHeaders
97 )
98
99 const shippingOptionPayload = {
100 name: "Return shipping",
101 service_zone_id: fulfillmentSet.service_zones[0].id,
102 shipping_profile_id: shippingProfile.id,
103 provider_id: shippingProviderId,
104 price_type: "flat",
105 type: {
106 label: "Test type",
107 description: "Test description",
108 code: "test-code",
109 },
110 prices: [
111 {
112 currency_code: "usd",
113 amount: 15,
114 },
115 ],
116 rules: [
117 {
118 operator: RuleOperator.EQ,
119 attribute: "is_return",
120 value: "true",
121 },
122 ],
123 }
124
125 const outboundShippingOptionPayload = {
126 name: "Oubound shipping",
127 service_zone_id: fulfillmentSet.service_zones[0].id,
128 shipping_profile_id: shippingProfile.id,
129 provider_id: shippingProviderId,
130 price_type: "flat",
131 type: {
132 label: "Test type",
133 description: "Test description",
134 code: "test-code",
135 },
136 prices: [
137 {
138 currency_code: "usd",
139 amount: 0, // Free shipping
140 },
141 ],
142 rules: [
143 {
144 operator: RuleOperator.EQ,
145 attribute: "is_return",
146 value: "false",
147 },
148 {
149 operator: RuleOperator.EQ,
150 attribute: "enabled_in_store",
151 value: "true",
152 },
153 ],
154 }
155
156 returnShippingOption = (
157 await api.post(
158 "/admin/shipping-options",
159 shippingOptionPayload,
160 adminHeaders
161 )
162 ).data.shipping_option
163
164 outboundShippingOption = (
165 await api.post(
166 "/admin/shipping-options",
167 outboundShippingOptionPayload,
168 adminHeaders
169 )
170 ).data.shipping_option
171
172 item = order.items[0]
173
174 await dbUtils.snapshot()
175 })
176
177 describe("RMA Flows", () => {
178 it("should verify order summary at each level", async () => {
179 /* Case:
180 Purchased:
181 items: {
182 unit_price: 25,
183 qty: 2
184 tax_total: 0
185 total: 50
186 }
187 shipping_methods: {
188 unit_price: 10,
189 qty: 1
190 tax_total: 1
191 total: 11
192 }
193 */
194
195 // Fulfill any existing items
196 let orderResult = (
197 await api.get(`/admin/orders/${order.id}`, adminHeaders)
198 ).data.order
199
200 let fulfillableItem = orderResult.items.find(
201 (item) => item.detail.fulfilled_quantity < item.detail.quantity
202 )
203
204 await api.post(
205 `/admin/orders/${order.id}/fulfillments`,
206 {
207 location_id: location.id,
208 items: [
209 {
210 id: fulfillableItem.id,
211 quantity: item.detail.quantity - item.detail.fulfilled_quantity,
212 },
213 ],
214 },
215 adminHeaders
216 )
217
218 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
219 .data.order
220
221 fulfillableItem = orderResult.items.find(
222 (item) => item.detail.fulfilled_quantity < item.detail.quantity
223 )
224
225 // Ensure that there are no more fulfillable items
226 expect(fulfillableItem).toBeUndefined()
227
228 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
229 .data.order
230
231 expect(orderResult).toEqual(
232 expect.objectContaining({
233 total: 106,
234 subtotal: 100,
235 tax_total: 6,
236 summary: expect.objectContaining({
237 paid_total: 0,
238 refunded_total: 0,
239 transaction_total: 0,
240 pending_difference: 106,
241 current_order_total: 106,
242 original_order_total: 106,
243 }),
244 })
245 )
246
247 /*
248 Create a claim with a single outbound item
249 */
250 const singleOutboundClaim = (
251 await api.post(
252 "/admin/claims",
253 {
254 order_id: order.id,
255 type: ClaimType.REPLACE,
256 description: "Base claim",
257 },
258 adminHeaders
259 )
260 ).data.claim
261
262 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
263 .data.order
264
265 expect(orderResult).toEqual(
266 expect.objectContaining({
267 total: 106,
268 subtotal: 100,
269 tax_total: 6,
270 summary: expect.objectContaining({
271 paid_total: 0,
272 refunded_total: 0,
273 transaction_total: 0,
274 pending_difference: 106,
275 current_order_total: 106,
276 original_order_total: 106,
277 }),
278 })
279 )
280
281 await api.post(
282 `/admin/claims/${singleOutboundClaim.id}/outbound/items`,
283 {
284 items: [
285 {
286 title: "new ITEM",
287 variant_id: order.items[0].variant_id,
288 quantity: 1,
289 },
290 ],
291 },
292 adminHeaders
293 )
294
295 // If claim has outbound items, we need to set the outbound shipping method for reservation to be created
296 // TODO: change condition in confirm workfow to crate reservation depending on shipping since we can have
297 // reservations for inventory items that don't require shipping
298 await api.post(
299 `/admin/claims/${singleOutboundClaim.id}/outbound/shipping-method`,
300 { shipping_option_id: outboundShippingOption.id },
301 adminHeaders
302 )
303
304 await api.post(
305 `/admin/claims/${singleOutboundClaim.id}/request`,
306 {},
307 adminHeaders
308 )
309
310 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
311 .data.order
312
313 expect(orderResult).toEqual(
314 expect.objectContaining({
315 total: 212,
316 subtotal: 200,
317 tax_total: 12,
318 summary: expect.objectContaining({
319 paid_total: 0,
320 refunded_total: 0,
321 transaction_total: 0,
322 pending_difference: 212,
323 current_order_total: 212,
324 original_order_total: 106,
325 }),
326 })
327 )
328
329 let pendingPaymentCollection = orderResult.payment_collections.find(
330 (pc) => pc.status === "not_paid"
331 )
332
333 expect(pendingPaymentCollection).toEqual(
334 expect.objectContaining({
335 status: "not_paid",
336 amount: 212,
337 })
338 )
339
340 let paymentCollection = (
341 await api.post(
342 `/admin/payment-collections/${pendingPaymentCollection.id}/mark-as-paid`,
343 { order_id: order.id },
344 adminHeaders
345 )
346 ).data.payment_collection
347
348 expect(paymentCollection).toEqual(
349 expect.objectContaining({
350 amount: 212,
351 status: "completed",
352 payment_sessions: [
353 expect.objectContaining({
354 status: "authorized",
355 amount: 212,
356 }),
357 ],
358 payments: [
359 expect.objectContaining({
360 provider_id: "pp_system_default",
361 amount: 212,
362 }),
363 ],
364 })
365 )
366
367 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
368 .data.order
369
370 // Totals summarked as paidy after payment has been mar
371 expect(orderResult).toEqual(
372 expect.objectContaining({
373 total: 212,
374 subtotal: 200,
375 tax_total: 12,
376 summary: expect.objectContaining({
377 paid_total: 212,
378 refunded_total: 0,
379 transaction_total: 212,
380 pending_difference: 0,
381 current_order_total: 212,
382 original_order_total: 106,
383 }),
384 })
385 )
386
387 fulfillableItem = orderResult.items.find(
388 (item) => item.detail.fulfilled_quantity < item.detail.quantity
389 )
390
391 await api.post(
392 `/admin/orders/${order.id}/fulfillments`,
393 {
394 location_id: location.id,
395 items: [
396 {
397 id: fulfillableItem.id,
398 quantity: item.detail.quantity - item.detail.fulfilled_quantity,
399 },
400 ],
401 },
402 adminHeaders
403 )
404
405 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
406 .data.order
407
408 fulfillableItem = orderResult.items.find(
409 (item) => item.detail.fulfilled_quantity < item.detail.quantity
410 )
411
412 // Ensure that there are no more fulfillable items
413 expect(fulfillableItem).toBeUndefined()
414
415 // After fulfillment, the taxes seems to now be considered.
416 // The case now is that you need to now request additional payment from
417 // the customer
418 expect(orderResult).toEqual(
419 expect.objectContaining({
420 total: 212,
421 subtotal: 200,
422 tax_total: 12,
423 summary: expect.objectContaining({
424 paid_total: 212,
425 refunded_total: 0,
426 transaction_total: 212,
427 pending_difference: 0,
428 current_order_total: 212,
429 original_order_total: 212,
430 }),
431 })
432 )
433
434 /*
435 We can see that fulfillment affects the totals, so lets create and confirm 2 claims
436 and fulfill after
437 */
438
439 let claimWithInboundAndOutbound = (
440 await api.post(
441 "/admin/claims",
442 {
443 order_id: order.id,
444 type: ClaimType.REPLACE,
445 description: "Base claim",
446 },
447 adminHeaders
448 )
449 ).data.claim
450
451 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
452 .data.order
453
454 // Nothing changes from the previous expectation
455 expect(orderResult).toEqual(
456 expect.objectContaining({
457 total: 212,
458 subtotal: 200,
459 tax_total: 12,
460 summary: expect.objectContaining({
461 paid_total: 212,
462 refunded_total: 0,
463 transaction_total: 212,
464 pending_difference: 0,
465 current_order_total: 212,
466 original_order_total: 212,
467 }),
468 })
469 )
470
471 let inboundItem = orderResult.items[0]
472
473 await api.post(
474 `/admin/claims/${claimWithInboundAndOutbound.id}/inbound/items`,
475 { items: [{ id: inboundItem.id, quantity: 1 }] },
476 adminHeaders
477 )
478
479 await api.post(
480 `/admin/claims/${claimWithInboundAndOutbound.id}/inbound/shipping-method`,
481 { shipping_option_id: returnShippingOption.id },
482 adminHeaders
483 )
484
485 await api.post(
486 `/admin/claims/${claimWithInboundAndOutbound.id}/outbound/items`,
487 {
488 items: [
489 {
490 title: "Test item 2",
491 variant_id: order.items[0].variant_id,
492 quantity: 1,
493 },
494 ],
495 },
496 adminHeaders
497 )
498
499 await api.post(
500 `/admin/claims/${claimWithInboundAndOutbound.id}/request`,
501 {},
502 adminHeaders
503 )
504
505 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
506 .data.order
507
508 // Totals summary after all
509 expect(orderResult).toEqual(
510 expect.objectContaining({
511 // This now adds a shipping_tax_total, but the item_tax_total hasn't been updated
512 total: 333.9,
513 subtotal: 315,
514 tax_total: 18.9,
515 summary: expect.objectContaining({
516 paid_total: 212,
517 refunded_total: 0,
518 transaction_total: 212,
519 pending_difference: 15.9,
520 current_order_total: 333.9,
521 original_order_total: 212,
522 }),
523 })
524 )
525
526 // Lets create one more claim without fulfilling the previous one
527 claimWithInboundAndOutbound = (
528 await api.post(
529 "/admin/claims",
530 {
531 order_id: order.id,
532 type: ClaimType.REPLACE,
533 description: "Base claim",
534 },
535 adminHeaders
536 )
537 ).data.claim
538
539 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
540 .data.order
541
542 // Nothing changes from the previous expectation
543 expect(orderResult).toEqual(
544 expect.objectContaining({
545 total: 333.9,
546 subtotal: 315,
547 tax_total: 18.9,
548 summary: expect.objectContaining({
549 paid_total: 212,
550 refunded_total: 0,
551 transaction_total: 212,
552 pending_difference: 15.9,
553 current_order_total: 333.9,
554 original_order_total: 212,
555 }),
556 })
557 )
558
559 inboundItem = orderResult.items[1]
560
561 await api.post(
562 `/admin/claims/${claimWithInboundAndOutbound.id}/inbound/items`,
563 { items: [{ id: inboundItem.id, quantity: 1 }] },
564 adminHeaders
565 )
566
567 await api.post(
568 `/admin/claims/${claimWithInboundAndOutbound.id}/outbound/items`,
569 {
570 items: [
571 {
572 variant_id: order.items[0].variant_id,
573 quantity: 1,
574 },
575 ],
576 },
577 adminHeaders
578 )
579
580 await api.post(
581 `/admin/claims/${claimWithInboundAndOutbound.id}/request`,
582 {},
583 adminHeaders
584 )
585
586 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
587 .data.order
588
589 expect(orderResult).toEqual(
590 expect.objectContaining({
591 total: 439.9,
592 subtotal: 415,
593 tax_total: 24.9,
594 summary: expect.objectContaining({
595 paid_total: 212,
596 refunded_total: 0,
597 transaction_total: 212,
598 pending_difference: 15.9,
599 current_order_total: 439.9,
600 original_order_total: 333.9,
601 }),
602 })
603 )
604
605 pendingPaymentCollection = orderResult.payment_collections.find(
606 (pc) => pc.status === "not_paid"
607 )
608
609 expect(pendingPaymentCollection).toEqual(
610 expect.objectContaining({
611 status: "not_paid",
612 amount: 15.9,
613 })
614 )
615
616 paymentCollection = (
617 await api.post(
618 `/admin/payment-collections/${pendingPaymentCollection.id}/mark-as-paid`,
619 { order_id: order.id },
620 adminHeaders
621 )
622 ).data.payment_collection
623
624 expect(paymentCollection).toEqual(
625 expect.objectContaining({
626 amount: 15.9,
627 status: "completed",
628 payment_sessions: [
629 expect.objectContaining({
630 status: "authorized",
631 amount: 15.9,
632 }),
633 ],
634 payments: [
635 expect.objectContaining({
636 provider_id: "pp_system_default",
637 amount: 15.9,
638 }),
639 ],
640 })
641 )
642
643 orderResult = (await api.get(`/admin/orders/${order.id}`, adminHeaders))
644 .data.order
645
646 // Totals summary after payment has been marked as paid
647 expect(orderResult).toEqual(
648 expect.objectContaining({
649 total: 439.9,
650 subtotal: 415,
651 tax_total: 24.9,
652 summary: expect.objectContaining({
653 paid_total: 227.9,
654 refunded_total: 0,
655 transaction_total: 227.9,
656 pending_difference: 0,
657 current_order_total: 439.9,
658 original_order_total: 333.9,
659 }),
660 })
661 )
662 })
663 })
664 },
665 })