From 1e0149ad080be1eeaecce242940b09367e200b90 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:33:43 +0200 Subject: [PATCH] fix(core-flows): Unsetting reason on return items (#8470) --- .../http/__tests__/returns/returns.spec.ts | 67 +++++++++++++++++++ .../return-create-form/return-create-form.tsx | 54 ++++++++------- .../return-create-form/return-item.tsx | 4 +- .../return/update-request-item-return.ts | 10 +-- 4 files changed, 103 insertions(+), 32 deletions(-) diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index 6fdb346f28..bb8dbac7d2 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -985,6 +985,73 @@ medusaIntegrationTestRunner({ expect(inventoryLevel[0].stocked_quantity).toEqual(3) }) }) + + describe("POST /admin/returns/:id/request-items/:action_id", () => { + let returnId + let itemChange + + 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 unset reason and note", 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 + + itemChange = result.items[0].actions[0] + + expect(result.items[0].actions[0]).toEqual( + expect.objectContaining({ + details: expect.objectContaining({ + reason_id: returnReason.id, + }), + internal_note: "Test note", + }) + ) + + result = ( + await api.post( + `/admin/returns/${returnId}/request-items/${itemChange.id}`, + { quantity: 1, reason_id: null, internal_note: null }, + adminHeaders + ) + ).data.order_preview + + expect(result.items[0].actions[0]).toEqual( + expect.objectContaining({ + details: expect.objectContaining({ + reason_id: null, + }), + internal_note: null, + }) + ) + }) + }) }) }, }) diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx index ee89fcd6ed..6bfa8dd4e0 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx @@ -436,34 +436,36 @@ export const ReturnCreateForm = ({ className="bg-ui-bg-field mt-4 block h-[56px] w-full rounded-lg border border-dashed" /> )} - {items.map((item, index) => ( - { - const actionId = previewItems - .find((i) => i.id === item.item_id) - ?.actions?.find((a) => a.action === "RETURN_ITEM")?.id + {items + .filter((item) => !!previewItemsMap.get(item.item_id)) + .map((item, index) => ( + { + const actionId = previewItems + .find((i) => i.id === item.item_id) + ?.actions?.find((a) => a.action === "RETURN_ITEM")?.id - if (actionId) { - removeReturnItem(actionId) - } - }} - onUpdate={(payload) => { - const actionId = previewItems - .find((i) => i.id === item.item_id) - ?.actions?.find((a) => a.action === "RETURN_ITEM")?.id + if (actionId) { + removeReturnItem(actionId) + } + }} + onUpdate={(payload) => { + const actionId = previewItems + .find((i) => i.id === item.item_id) + ?.actions?.find((a) => a.action === "RETURN_ITEM")?.id - if (actionId) { - updateReturnItem({ ...payload, actionId }) - } - }} - index={index} - /> - ))} + if (actionId) { + updateReturnItem({ ...payload, actionId }) + } + }} + index={index} + /> + ))} {!showPlaceholder && (
{/*LOCATION*/} diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-item.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-item.tsx index b7a5d61421..6962a713ac 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-item.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-item.tsx @@ -177,8 +177,8 @@ function ReturnItem({ className="flex-shrink" variant="transparent" onClick={() => { - onUpdate({ reason_id: null }) // TODO BE: we should be able to set to unset reason here - form.setValue(`items.${index}.reason_id`, "") + onUpdate({ reason_id: null }) + form.setValue(`items.${index}.reason_id`, null) }} > diff --git a/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts b/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts index 7621cbc30d..2218e77ce5 100644 --- a/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts +++ b/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts @@ -5,13 +5,13 @@ import { OrderWorkflow, ReturnDTO, } from "@medusajs/types" -import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" +import { ChangeActionType, isDefined, OrderChangeStatus } from "@medusajs/utils" import { - WorkflowData, - WorkflowResponse, createStep, createWorkflow, transform, + WorkflowData, + WorkflowResponse, } from "@medusajs/workflows-sdk" import { useRemoteQueryStep } from "../../../common" import { @@ -119,7 +119,9 @@ export const updateRequestItemReturnWorkflow = createWorkflow( id: input.action_id, details: { quantity: data.quantity ?? originalAction.details?.quantity, - reason_id: data.reason_id ?? originalAction.details?.reason_id, + reason_id: isDefined(data.reason_id) + ? data.reason_id + : originalAction.details?.reason_id, }, internal_note: data.internal_note, }