Feat(medusa, medusa-js, medusa-react): order edit confirmation (#2264)

**what**

Support confirm of an order edit:

Upon confirmation, the items of the original order are detached and the items from the order edit are attached to the order.
The order total is recomputed with the correct total which can defer from the paid_total and refundable_amount (based on the paid_total)


**Tests**

- Unit tests medusa-js and medusa-react as well as the core
- Integration test of the confirmation flow which check that the order edit is properly confirmed and can be confirmed idempotently. Also validate the totals and that the order items correspond to the order edit items. Also validate the order totals.

FIXES CORE-498
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2022-09-29 08:00:48 +00:00
1 parent 87ad29dda4
commit 2be00007b2
22 files changed
+736 -198

No files matched your search

+19 -2
View File
@@ -1695,12 +1695,29 @@ export const adminHandlers = [
})
)
}),
rest.post("/admin/order-edits/:id/cancel", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
order_edit: { ...fixtures.get("order_edit"), canceled_at: new Date(), status: 'canceled' },
order_edit: {
...fixtures.get("order_edit"),
canceled_at: new Date(),
status: "canceled",
},
})
)
}),
rest.post("/admin/order-edits/:id/confirm", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
order_edit: {
...fixtures.get("order_edit"),
confirmed_at: new Date(),
status: "confirmed",
},
})
)
}),
@@ -153,24 +153,36 @@ export const useAdminRequestOrderEditConfirmation = (
)
}
export const useAdminCancelOrderEdit = (
id: string,
options?: UseMutationOptions<
Response<AdminOrderEditsRes>,
Error
>
options?: UseMutationOptions<Response<AdminOrderEditsRes>, Error>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
() =>
client.admin.orderEdits.cancel(id),
() => client.admin.orderEdits.cancel(id),
buildOptions(
queryClient,
[adminOrderEditsKeys.lists(), adminOrderEditsKeys.detail(id)],
options
)
)
}
}
export const useAdminConfirmOrderEdit = (
id: string,
options?: UseMutationOptions<Response<AdminOrderEditsRes>, Error>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
() => client.admin.orderEdits.confirm(id),
buildOptions(
queryClient,
[adminOrderEditsKeys.lists(), adminOrderEditsKeys.detail(id)],
options
)
)
}
@@ -1,6 +1,7 @@
import { renderHook } from "@testing-library/react-hooks"
import {
useAdminCancelOrderEdit,
useAdminConfirmOrderEdit,
useAdminCreateOrderEdit,
useAdminDeleteOrderEdit,
useAdminDeleteOrderEditItemChange,
@@ -220,3 +221,29 @@ describe("useAdminCancelOrderEdit hook", () => {
)
})
})
describe("useAdminConfirmOrderEdit hook", () => {
test("confirm an order edit", async () => {
const { result, waitFor } = renderHook(
() => useAdminConfirmOrderEdit(fixtures.get("order_edit").id),
{
wrapper: createWrapper(),
}
)
result.current.mutate()
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data).toEqual(
expect.objectContaining({
order_edit: {
...fixtures.get("order_edit"),
confirmed_at: expect.any(String),
status: "confirmed",
},
})
)
})
})