From 78eb45748798e7f92306f2da80df185f69f49459 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:48:14 +0200 Subject: [PATCH] fix(core-flows): Allow return requests with no shipping (#8472) Closes CC-265 --- .../http/__tests__/returns/returns.spec.ts | 195 ++++++++++++++++-- .../return/confirm-return-request.ts | 11 +- .../src/services/actions/receive-return.ts | 3 + 3 files changed, 188 insertions(+), 21 deletions(-) diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index bb8dbac7d2..9443e7ff33 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -986,9 +986,8 @@ medusaIntegrationTestRunner({ }) }) - describe("POST /admin/returns/:id/request-items/:action_id", () => { + describe("POST /admin/returns/:id/request", () => { let returnId - let itemChange beforeEach(async () => { let result = await api.post( @@ -1004,7 +1003,7 @@ medusaIntegrationTestRunner({ returnId = result.data.return.id }) - it("should unset reason and note", async () => { + it("should confirm return request", async () => { const item = order.items[0] let result = ( await api.post( @@ -1023,31 +1022,195 @@ medusaIntegrationTestRunner({ ) ).data.order_preview - itemChange = result.items[0].actions[0] + result = ( + await api.post( + `/admin/returns/${returnId}/request`, + {}, + adminHeaders + ) + ).data.return - expect(result.items[0].actions[0]).toEqual( + expect(result).toEqual( expect.objectContaining({ - details: expect.objectContaining({ - reason_id: returnReason.id, - }), - internal_note: "Test note", + id: returnId, + status: "requested", + requested_at: expect.any(String), + }) + ) + }) + + it("should confirm return request with shipping", async () => { + const item = order.items[0] + let result = ( + await api.post( + `/admin/returns/${returnId}/request-items`, + { + items: [ + { + id: item.id, + quantity: 2, + reason_id: returnReason.id, + internal_note: "Test note", + }, + ], + }, + adminHeaders + ) + ).data.order_preview + + result = ( + await api.post( + `/admin/returns/${returnId}/shipping-method`, + { + shipping_option_id: returnShippingOption.id, + }, + adminHeaders + ) + ).data.order_preview + + expect(result).toEqual( + expect.objectContaining({ + id: order.id, + shipping_methods: expect.arrayContaining([ + expect.objectContaining({ + id: expect.any(String), + name: "Return shipping", + amount: 1000, + subtotal: 1000, + total: 1000, + }), + ]), }) ) result = ( await api.post( - `/admin/returns/${returnId}/request-items/${itemChange.id}`, - { quantity: 1, reason_id: null, internal_note: null }, + `/admin/returns/${returnId}/request`, + {}, + adminHeaders + ) + ).data.return + + expect(result).toEqual( + expect.objectContaining({ + id: returnId, + status: "requested", + requested_at: expect.any(String), + }) + ) + }) + }) + + describe("POST /admin/returns/:id/request", () => { + let returnId + + beforeEach(async () => { + let result = await api.post( + "/admin/returns", + { + order_id: order.id, + description: "Test", + location_id: location.id, + }, + adminHeaders + ) + + returnId = result.data.return.id + }) + + it("should confirm return request", async () => { + const item = order.items[0] + let result = ( + await api.post( + `/admin/returns/${returnId}/request-items`, + { + items: [ + { + id: item.id, + quantity: 2, + reason_id: returnReason.id, + internal_note: "Test note", + }, + ], + }, adminHeaders ) ).data.order_preview - expect(result.items[0].actions[0]).toEqual( + result = ( + await api.post( + `/admin/returns/${returnId}/request`, + {}, + adminHeaders + ) + ).data.return + + expect(result).toEqual( expect.objectContaining({ - details: expect.objectContaining({ - reason_id: null, - }), - internal_note: null, + id: returnId, + status: "requested", + requested_at: expect.any(String), + }) + ) + }) + + it("should confirm return request with shipping", async () => { + const item = order.items[0] + let result = ( + await api.post( + `/admin/returns/${returnId}/request-items`, + { + items: [ + { + id: item.id, + quantity: 2, + reason_id: returnReason.id, + internal_note: "Test note", + }, + ], + }, + adminHeaders + ) + ).data.order_preview + + result = ( + await api.post( + `/admin/returns/${returnId}/shipping-method`, + { + shipping_option_id: returnShippingOption.id, + }, + adminHeaders + ) + ).data.order_preview + + expect(result).toEqual( + expect.objectContaining({ + id: order.id, + shipping_methods: expect.arrayContaining([ + expect.objectContaining({ + id: expect.any(String), + name: "Return shipping", + amount: 1000, + subtotal: 1000, + total: 1000, + }), + ]), + }) + ) + + result = ( + await api.post( + `/admin/returns/${returnId}/request`, + {}, + adminHeaders + ) + ).data.return + + expect(result).toEqual( + expect.objectContaining({ + id: returnId, + status: "requested", + requested_at: expect.any(String), }) ) }) diff --git a/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts b/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts index 84cfd35a7a..253fb4944f 100644 --- a/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts +++ b/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts @@ -106,12 +106,8 @@ function prepareFulfillmentData({ } function extractReturnShippingOptionId({ orderPreview, orderReturn }) { - if (!orderPreview.shipping_methods?.length) { - return - } - let returnShippingMethod - for (const shippingMethod of orderPreview.shipping_methods) { + for (const shippingMethod of orderPreview.shipping_methods ?? []) { const modifiedShippingMethod_ = shippingMethod as any if (!modifiedShippingMethod_.actions) { continue @@ -124,6 +120,11 @@ function extractReturnShippingOptionId({ orderPreview, orderReturn }) { ) }) } + + if (!returnShippingMethod) { + return null + } + return returnShippingMethod.shipping_option_id } diff --git a/packages/modules/order/src/services/actions/receive-return.ts b/packages/modules/order/src/services/actions/receive-return.ts index 2d77780d2a..8fcef5684a 100644 --- a/packages/modules/order/src/services/actions/receive-return.ts +++ b/packages/modules/order/src/services/actions/receive-return.ts @@ -50,6 +50,9 @@ function updateReturnItems(returnEntry, items) { const data = items.find((i) => i.details.reference_id === item.item_id) if (!data) return + console.log("ITEM: ", item) + console.log("DATA: ", data) + const receivedQuantity = MathBN.add( item.received_quantity || 0, data.details.quantity