Skip to content

Commit

Permalink
fix(core-flows): Unsetting reason on return items
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Aug 6, 2024
1 parent 04044c9 commit b40a8a9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 34 deletions.
67 changes: 67 additions & 0 deletions integration-tests/http/__tests__/returns/returns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
)
})
})
})
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<ReturnItem
key={item.id}
item={itemsMap.get(item.item_id)!}
previewItem={previewItemsMap.get(item.item_id)!}
currencyCode={order.currency_code}
form={form}
onRemove={() => {
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}
/>
))}
{items
.filter((item) => !!previewItemsMap.get(item.item_id))
.map((item, index) => (
<ReturnItem
key={item.id}
item={itemsMap.get(item.item_id)!}
previewItem={previewItemsMap.get(item.item_id)}
currencyCode={order.currency_code}
form={form}
onRemove={() => {
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}
/>
))}
{!showPlaceholder && (
<div className="mt-8 flex flex-col gap-y-4">
{/*LOCATION*/}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}}
>
<XMark className="text-ui-fg-muted" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit b40a8a9

Please sign in to comment.