fix(core-flows): Unsetting reason on return items (#8470)

This commit is contained in:
Oli Juhl
2024-08-07 09:33:43 +02:00
committed by GitHub
parent 3d7dce2bec
commit 1e0149ad08
4 changed files with 103 additions and 32 deletions
@@ -985,6 +985,73 @@ medusaIntegrationTestRunner({
expect(inventoryLevel[0].stocked_quantity).toEqual(3) 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,
})
)
})
})
}) })
}, },
}) })
@@ -436,34 +436,36 @@ export const ReturnCreateForm = ({
className="bg-ui-bg-field mt-4 block h-[56px] w-full rounded-lg border border-dashed" className="bg-ui-bg-field mt-4 block h-[56px] w-full rounded-lg border border-dashed"
/> />
)} )}
{items.map((item, index) => ( {items
<ReturnItem .filter((item) => !!previewItemsMap.get(item.item_id))
key={item.id} .map((item, index) => (
item={itemsMap.get(item.item_id)!} <ReturnItem
previewItem={previewItemsMap.get(item.item_id)!} key={item.id}
currencyCode={order.currency_code} item={itemsMap.get(item.item_id)!}
form={form} previewItem={previewItemsMap.get(item.item_id)}
onRemove={() => { currencyCode={order.currency_code}
const actionId = previewItems form={form}
.find((i) => i.id === item.item_id) onRemove={() => {
?.actions?.find((a) => a.action === "RETURN_ITEM")?.id const actionId = previewItems
.find((i) => i.id === item.item_id)
?.actions?.find((a) => a.action === "RETURN_ITEM")?.id
if (actionId) { if (actionId) {
removeReturnItem(actionId) removeReturnItem(actionId)
} }
}} }}
onUpdate={(payload) => { onUpdate={(payload) => {
const actionId = previewItems const actionId = previewItems
.find((i) => i.id === item.item_id) .find((i) => i.id === item.item_id)
?.actions?.find((a) => a.action === "RETURN_ITEM")?.id ?.actions?.find((a) => a.action === "RETURN_ITEM")?.id
if (actionId) { if (actionId) {
updateReturnItem({ ...payload, actionId }) updateReturnItem({ ...payload, actionId })
} }
}} }}
index={index} index={index}
/> />
))} ))}
{!showPlaceholder && ( {!showPlaceholder && (
<div className="mt-8 flex flex-col gap-y-4"> <div className="mt-8 flex flex-col gap-y-4">
{/*LOCATION*/} {/*LOCATION*/}
@@ -177,8 +177,8 @@ function ReturnItem({
className="flex-shrink" className="flex-shrink"
variant="transparent" variant="transparent"
onClick={() => { onClick={() => {
onUpdate({ reason_id: null }) // TODO BE: we should be able to set to unset reason here onUpdate({ reason_id: null })
form.setValue(`items.${index}.reason_id`, "") form.setValue(`items.${index}.reason_id`, null)
}} }}
> >
<XMark className="text-ui-fg-muted" /> <XMark className="text-ui-fg-muted" />
@@ -5,13 +5,13 @@ import {
OrderWorkflow, OrderWorkflow,
ReturnDTO, ReturnDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" import { ChangeActionType, isDefined, OrderChangeStatus } from "@medusajs/utils"
import { import {
WorkflowData,
WorkflowResponse,
createStep, createStep,
createWorkflow, createWorkflow,
transform, transform,
WorkflowData,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { useRemoteQueryStep } from "../../../common"
import { import {
@@ -119,7 +119,9 @@ export const updateRequestItemReturnWorkflow = createWorkflow(
id: input.action_id, id: input.action_id,
details: { details: {
quantity: data.quantity ?? originalAction.details?.quantity, 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, internal_note: data.internal_note,
} }