feat(medusa): Implement premises of the creation flow of an order edit (#2187)

**What**
- Implements the admin create end point 
- Service implementation of the create method and the retrieveActive as well as the totals computation
- Improve compute line items
- client
  - medusa-js api
  - medusa-react mutations hooks

**Tests**
- Unit tests of the create end points
- Unit tests of the service create method
- Integration tests for admin that also take into account totals computations
- client
  - medusa-js tests
  - medusa-react hooks tests

FIXES CORE-491
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2022-09-16 08:29:40 +00:00
1 parent 6132711eef
commit f7177c9033
20 files changed
+745 -115

No files matched your search

@@ -1664,6 +1664,18 @@ export const adminHandlers = [
)
}),
rest.post("/admin/order-edits/", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
order_edit: {
...fixtures.get("order_edit"),
...(req.body as any),
},
})
)
}),
rest.get("/store/order-edits/:id", (req, res, ctx) => {
const { id } = req.params
return res(
@@ -1,11 +1,29 @@
import {
AdminOrderEditDeleteRes,
AdminOrderEditsRes,
AdminPostOrderEditsReq,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
import { adminOrderEditsKeys } from "."
import { useMedusa } from "../../../contexts/medusa"
import { buildOptions } from "../../utils/buildOptions"
import { useMedusa } from "../../../contexts"
export const useAdminCreateOrderEdit = (
options?: UseMutationOptions<
Response<AdminOrderEditsRes>,
Error,
AdminPostOrderEditsReq
>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
(payload: AdminPostOrderEditsReq) =>
client.admin.orderEdits.create(payload),
buildOptions(queryClient, adminOrderEditsKeys.lists(), options)
)
}
export const useAdminDeleteOrderEdit = (
id: string,
@@ -1,4 +1,4 @@
import { AdminOrdersEditsRes } from "@medusajs/medusa"
import { AdminOrderEditsRes } from "@medusajs/medusa"
import { queryKeysFactory } from "../../utils"
import { UseQueryOptionsWrapper } from "../../../types"
import { Response } from "@medusajs/medusa-js"
@@ -13,7 +13,7 @@ type OrderEditQueryKeys = typeof adminOrderEditsKeys
export const useAdminOrderEdit = (
id: string,
options?: UseQueryOptionsWrapper<
Response<AdminOrdersEditsRes>,
Response<AdminOrderEditsRes>,
Error,
ReturnType<OrderEditQueryKeys["detail"]>
>
@@ -1,7 +1,10 @@
import { useAdminDeleteOrderEdit } from "../../../../src/"
import {
useAdminCreateOrderEdit,
useAdminDeleteOrderEdit,
} from "../../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
import { fixtures } from "../../../../mocks/data"
describe("useAdminDelete hook", () => {
test("Deletes an order edit", async () => {
@@ -11,7 +14,6 @@ describe("useAdminDelete hook", () => {
})
result.current.mutate()
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
@@ -24,3 +26,30 @@ describe("useAdminDelete hook", () => {
)
})
})
describe("useAdminCreateOrderEdit hook", () => {
test("Created an order edit", async () => {
const { result, waitFor } = renderHook(() => useAdminCreateOrderEdit(), {
wrapper: createWrapper(),
})
const payload = {
order_id: "ord_1",
internal_note: "This is an internal note",
}
result.current.mutate(payload)
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"),
...payload,
},
})
)
})
})