Feat(medusa, medusa-js, medusa-react): order edit item update (#2246)
**what** Support `updateLineItem` which does the following: - If no item change exist then create a new one and attaches the clone item with the adjustments and tax lines - if an item change exists then delete/create adjustments and tax lines and update the cloned item quantity **Tests** - Unit tests core + client - integration tests - When no item change already exists - When an item change already exists FIXES CORE-497
This commit is contained in:
1 parent
1807bff029
commit
474e97252c
24 files changed
+1247
-63
No files matched your search
@@ -1712,7 +1712,7 @@ export const adminHandlers = [
|
||||
order_edit: {
|
||||
...fixtures.get("order_edit"),
|
||||
requested_at: new Date(),
|
||||
status: "requested"
|
||||
status: "requested",
|
||||
},
|
||||
})
|
||||
)
|
||||
@@ -1742,6 +1742,22 @@ export const adminHandlers = [
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/order-edits/:id/items/:item_id", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
order_edit: {
|
||||
...fixtures.get("order_edit"),
|
||||
changes: [
|
||||
{
|
||||
quantity: (req.body as any).quantity,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.get("/admin/auth", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
AdminOrderEditDeleteRes,
|
||||
AdminOrderEditItemChangeDeleteRes,
|
||||
AdminOrderEditsRes,
|
||||
AdminPostOrderEditsEditLineItemsLineItemReq,
|
||||
AdminPostOrderEditsOrderEditReq,
|
||||
AdminPostOrderEditsReq,
|
||||
} from "@medusajs/medusa"
|
||||
@@ -68,6 +69,29 @@ export const useAdminDeleteOrderEditItemChange = (
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminOrderEditUpdateLineItem = (
|
||||
orderEditId: string,
|
||||
itemId: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminOrderEditsRes>,
|
||||
Error,
|
||||
AdminPostOrderEditsEditLineItemsLineItemReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostOrderEditsEditLineItemsLineItemReq) =>
|
||||
client.admin.orderEdits.updateLineItem(orderEditId, itemId, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminOrderEditsKeys.detail(orderEditId), adminOrderEditsKeys.lists()],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminUpdateOrderEdit = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
|
||||
@@ -1,15 +1,44 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import {
|
||||
useAdminCancelOrderEdit,
|
||||
useAdminCreateOrderEdit,
|
||||
useAdminDeleteOrderEdit,
|
||||
useAdminDeleteOrderEditItemChange,
|
||||
useAdminUpdateOrderEdit,
|
||||
useAdminOrderEditUpdateLineItem,
|
||||
useAdminRequestOrderEditConfirmation,
|
||||
useAdminCancelOrderEdit,
|
||||
useAdminUpdateOrderEdit,
|
||||
} from "../../../../src/"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { createWrapper } from "../../../utils"
|
||||
|
||||
describe("useAdminOrderEditUpdateLineItem hook", () => {
|
||||
test("Update line item of an order edit and create or update an item change", async () => {
|
||||
const id = "oe_1"
|
||||
const itemId = "item_1"
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminOrderEditUpdateLineItem(id, itemId),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({ quantity: 3 })
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.order_edit).toEqual(
|
||||
expect.objectContaining({
|
||||
...fixtures.get("order_edit"),
|
||||
changes: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
quantity: 3,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminDeleteOrderEditItemChange hook", () => {
|
||||
test("Deletes an order edit item change", async () => {
|
||||
const id = "oe_1"
|
||||
@@ -112,9 +141,12 @@ describe("useAdminCreateOrderEdit hook", () => {
|
||||
|
||||
describe("useAdminRequestOrderEditConfirmation hook", () => {
|
||||
test("Requests an order edit", async () => {
|
||||
const { result, waitFor } = renderHook(() => useAdminRequestOrderEditConfirmation(fixtures.get("order_edit").id), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminRequestOrderEditConfirmation(fixtures.get("order_edit").id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
@@ -123,9 +155,9 @@ describe("useAdminRequestOrderEditConfirmation hook", () => {
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data?.order_edit).toEqual(
|
||||
expect.objectContaining({
|
||||
...fixtures.get("order_edit"),
|
||||
requested_at: expect.any(String),
|
||||
status: 'requested'
|
||||
...fixtures.get("order_edit"),
|
||||
requested_at: expect.any(String),
|
||||
status: "requested",
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -133,10 +165,12 @@ describe("useAdminRequestOrderEditConfirmation hook", () => {
|
||||
|
||||
describe("useAdminCancelOrderEdit hook", () => {
|
||||
test("cancel an order edit", async () => {
|
||||
|
||||
const { result, waitFor } = renderHook(() => useAdminCancelOrderEdit(fixtures.get("order_edit").id), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminCancelOrderEdit(fixtures.get("order_edit").id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
@@ -148,7 +182,7 @@ describe("useAdminCancelOrderEdit hook", () => {
|
||||
order_edit: {
|
||||
...fixtures.get("order_edit"),
|
||||
canceled_at: expect.any(String),
|
||||
status: 'canceled'
|
||||
status: "canceled",
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user