feat(medusa): Update OrderEdit (#2220)

This commit is contained in:
Frane Polić
2022-09-19 13:29:12 +02:00
committed by GitHub
parent 5a2ac76762
commit e1fe5ed094
11 changed files with 337 additions and 12 deletions

View File

@@ -429,4 +429,95 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
) )
}) })
}) })
describe("POST /admin/order-edits/:id", () => {
const orderEditId = IdMap.getId("order-edit-1")
const prodId1 = IdMap.getId("prodId1")
const lineItemId1 = IdMap.getId("line-item-1")
const orderId1 = IdMap.getId("order-id-1")
beforeEach(async () => {
await adminSeeder(dbConnection)
const product1 = await simpleProductFactory(dbConnection, {
id: prodId1,
})
const order = await simpleOrderFactory(dbConnection, {
id: orderId1,
email: "test@testson.com",
tax_rate: null,
fulfillment_status: "fulfilled",
payment_status: "captured",
region: {
id: "test-region",
name: "Test region",
tax_rate: 12.5,
},
line_items: [
{
id: lineItemId1,
variant_id: product1.variants[0].id,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
unit_price: 1000,
},
],
})
await simpleOrderEditFactory(dbConnection, {
id: orderEditId,
order_id: order.id,
created_by: "admin_user",
internal_note: "test internal note",
})
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
it("updates an order edit", async () => {
const api = useApi()
const response = await api.post(
`/admin/order-edits/${orderEditId}`,
{ internal_note: "changed note" },
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.order_edit).toEqual(
expect.objectContaining({
id: orderEditId,
created_by: "admin_user",
requested_by: null,
canceled_by: null,
confirmed_by: null,
internal_note: "changed note",
/*
* Computed items are appended to the response
*/
items: [
expect.objectContaining({
id: lineItemId1,
order_id: orderId1,
}),
],
/*
* Computed totals are appended to the response
*/
discount_total: 0,
gift_card_total: 0,
gift_card_tax_total: 0,
shipping_total: 0,
subtotal: 1000,
tax_total: 0,
total: 1000,
})
)
})
})
}) })

View File

@@ -1,7 +1,8 @@
import { import {
AdminOrderEditDeleteRes,
AdminOrderEditsRes, AdminOrderEditsRes,
AdminPostOrderEditsReq, AdminPostOrderEditsReq,
AdminOrderEditDeleteRes,
AdminPostOrderEditsOrderEditReq,
} from "@medusajs/medusa" } from "@medusajs/medusa"
import { ResponsePromise } from "../../typings" import { ResponsePromise } from "../../typings"
import BaseResource from "../base" import BaseResource from "../base"
@@ -23,6 +24,15 @@ class AdminOrderEditsResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders) return this.client.request("POST", path, payload, {}, customHeaders)
} }
update(
id: string,
payload: AdminPostOrderEditsOrderEditReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrderEditsRes> {
const path = `/admin/order-edits/${id}`
return this.client.request("POST", path, payload, {}, customHeaders)
}
delete( delete(
id: string, id: string,
customHeaders: Record<string, any> = {} customHeaders: Record<string, any> = {}

View File

@@ -1687,6 +1687,15 @@ export const adminHandlers = [
) )
}), }),
rest.post("/admin/order-edits/:id", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
order_edit: { ...fixtures.get("order_edit"), ...(req.body as any) },
})
)
}),
rest.delete("/admin/order-edits/:id", (req, res, ctx) => { rest.delete("/admin/order-edits/:id", (req, res, ctx) => {
const { id } = req.params const { id } = req.params
return res( return res(

View File

@@ -1,13 +1,16 @@
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
import { Response } from "@medusajs/medusa-js"
import { import {
AdminOrderEditDeleteRes, AdminOrderEditDeleteRes,
AdminPostOrderEditsOrderEditReq,
AdminOrderEditsRes, AdminOrderEditsRes,
AdminPostOrderEditsReq, AdminPostOrderEditsReq,
} from "@medusajs/medusa" } from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
import { adminOrderEditsKeys } from "."
import { buildOptions } from "../../utils/buildOptions" import { buildOptions } from "../../utils/buildOptions"
import { useMedusa } from "../../../contexts" import { useMedusa } from "../../../contexts"
import { adminOrderEditsKeys } from "."
export const useAdminCreateOrderEdit = ( export const useAdminCreateOrderEdit = (
options?: UseMutationOptions< options?: UseMutationOptions<
@@ -41,3 +44,25 @@ export const useAdminDeleteOrderEdit = (
) )
) )
} }
export const useAdminUpdateOrderEdit = (
id: string,
options?: UseMutationOptions<
Response<AdminOrderEditsRes>,
Error,
AdminPostOrderEditsOrderEditReq
>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
(payload: AdminPostOrderEditsOrderEditReq) =>
client.admin.orderEdits.update(id, payload),
buildOptions(
queryClient,
[adminOrderEditsKeys.lists(), adminOrderEditsKeys.detail(id)],
options
)
)
}

View File

@@ -1,10 +1,13 @@
import { renderHook } from "@testing-library/react-hooks"
import { import {
useAdminCreateOrderEdit, useAdminCreateOrderEdit,
useAdminUpdateOrderEdit,
useAdminDeleteOrderEdit, useAdminDeleteOrderEdit,
} from "../../../../src/" } from "../../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { createWrapper } from "../../../utils"
import { fixtures } from "../../../../mocks/data" import { fixtures } from "../../../../mocks/data"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminDelete hook", () => { describe("useAdminDelete hook", () => {
test("Deletes an order edit", async () => { test("Deletes an order edit", async () => {
@@ -27,6 +30,33 @@ describe("useAdminDelete hook", () => {
}) })
}) })
describe("useAdminUpdateOrderEdit hook", () => {
test("updates an order edit and returns it", async () => {
const orderEdit = {
internal_note: "changed note",
}
const { result, waitFor } = renderHook(
() => useAdminUpdateOrderEdit(fixtures.get("order_edit").id),
{
wrapper: createWrapper(),
}
)
result.current.mutate(orderEdit)
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"),
...orderEdit,
})
)
})
})
describe("useAdminCreateOrderEdit hook", () => { describe("useAdminCreateOrderEdit hook", () => {
test("Created an order edit", async () => { test("Created an order edit", async () => {
const { result, waitFor } = renderHook(() => useAdminCreateOrderEdit(), { const { result, waitFor } = renderHook(() => useAdminCreateOrderEdit(), {

View File

@@ -1,4 +1,5 @@
import { Router } from "express" import { Router } from "express"
import middlewares, { import middlewares, {
transformBody, transformBody,
transformQuery, transformQuery,
@@ -11,6 +12,7 @@ import {
defaultOrderEditRelations, defaultOrderEditRelations,
} from "../../../../types/order-edit" } from "../../../../types/order-edit"
import { OrderEdit } from "../../../../models" import { OrderEdit } from "../../../../models"
import { AdminPostOrderEditsOrderEditReq } from "./update-order-edit"
import { AdminPostOrderEditsReq } from "./create-order-edit" import { AdminPostOrderEditsReq } from "./create-order-edit"
const route = Router() const route = Router()
@@ -38,6 +40,12 @@ export default (app) => {
middlewares.wrap(require("./get-order-edit").default) middlewares.wrap(require("./get-order-edit").default)
) )
route.post(
"/:id",
transformBody(AdminPostOrderEditsOrderEditReq),
middlewares.wrap(require("./update-order-edit").default)
)
route.delete("/:id", middlewares.wrap(require("./delete-order-edit").default)) route.delete("/:id", middlewares.wrap(require("./delete-order-edit").default))
return app return app
@@ -48,4 +56,6 @@ export type AdminOrderEditsRes = {
} }
export type AdminOrderEditDeleteRes = DeleteResponse export type AdminOrderEditDeleteRes = DeleteResponse
export * from "./update-order-edit"
export * from "./create-order-edit" export * from "./create-order-edit"

View File

@@ -0,0 +1,102 @@
import { IsOptional, IsString } from "class-validator"
import { Request, Response } from "express"
import { EntityManager } from "typeorm"
import { OrderEditService } from "../../../../services"
/**
* @oas [post] /order-edits/{id}
* operationId: "PostOrderEditsOrderEdit"
* summary: "Updates an OrderEdit"
* description: "Updates a OrderEdit."
* x-authenticated: true
* parameters:
* - (path) id=* {string} The ID of the OrderEdit.
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* const params = {internal_note: "internal reason XY"}
* medusa.admin.orderEdit.update(orderEditId, params)
* .then(({ order_edit }) => {
* console.log(order_edit.id);
* });
* - lang: Shell
* label: cURL
* source: |
* curl --location --request POST 'https://medusa-url.com/admin/order-edits/{id}' \
* --header 'Authorization: Bearer {api_token}'
* --header 'Content-Type: application/json' \
* --data-raw '{
* "internal_note": "internal reason XY"
* }'
* security:
* - api_token: []
* - cookie_auth: []
* tags:
* - OrderEdit
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* order_edit:
* $ref: "#/components/schemas/order_edit"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
* $ref: "#/components/responses/unauthorized"
* "404":
* $ref: "#/components/responses/not_found_error"
* "409":
* $ref: "#/components/responses/invalid_state_error"
* "422":
* $ref: "#/components/responses/invalid_request_error"
* "500":
* $ref: "#/components/responses/500_error"
*/
export default async (req: Request, res: Response) => {
const { id } = req.params
const { validatedBody } = req as {
validatedBody: AdminPostOrderEditsOrderEditReq
}
const orderEditService: OrderEditService =
req.scope.resolve("orderEditService")
const manager: EntityManager = req.scope.resolve("manager")
const orderEdit = await manager.transaction(async (transactionManager) => {
const orderEditServiceTx =
orderEditService.withTransaction(transactionManager)
const _orderEdit = await orderEditServiceTx.update(id, validatedBody)
const { items } = await orderEditServiceTx.computeLineItems(_orderEdit.id)
_orderEdit.items = items
const totals = await orderEditServiceTx.getTotals(_orderEdit.id)
_orderEdit.discount_total = totals.discount_total
_orderEdit.gift_card_total = totals.gift_card_total
_orderEdit.gift_card_tax_total = totals.gift_card_tax_total
_orderEdit.shipping_total = totals.shipping_total
_orderEdit.subtotal = totals.subtotal
_orderEdit.tax_total = totals.tax_total
_orderEdit.total = totals.total
return _orderEdit
})
res.status(200).json({ order_edit: orderEdit })
}
export class AdminPostOrderEditsOrderEditReq {
@IsOptional()
@IsString()
internal_note?: string
}

View File

@@ -34,11 +34,6 @@ export default (app) => {
"/", "/",
middlewares.wrap(require("./get-sales-channel").default) middlewares.wrap(require("./get-sales-channel").default)
) )
salesChannelRouter.post(
"/",
transformBody(AdminPostSalesChannelsSalesChannelReq),
middlewares.wrap(require("./update-sales-channel").default)
)
salesChannelRouter.delete( salesChannelRouter.delete(
"/", "/",
middlewares.wrap(require("./delete-sales-channel").default) middlewares.wrap(require("./delete-sales-channel").default)

View File

@@ -12,6 +12,10 @@ import { EventBusServiceMock } from "../__mocks__/event-bus"
import { LineItemServiceMock } from "../__mocks__/line-item" import { LineItemServiceMock } from "../__mocks__/line-item"
import { TotalsServiceMock } from "../__mocks__/totals" import { TotalsServiceMock } from "../__mocks__/totals"
const orderEditToUpdate = {
id: IdMap.getId("order-edit-to-update"),
}
const orderEditWithChanges = { const orderEditWithChanges = {
id: IdMap.getId("order-edit-with-changes"), id: IdMap.getId("order-edit-with-changes"),
order: { order: {
@@ -77,6 +81,10 @@ const lineItemServiceMock = {
} }
describe("OrderEditService", () => { describe("OrderEditService", () => {
afterEach(() => {
jest.clearAllMocks()
})
const orderEditRepository = MockRepository({ const orderEditRepository = MockRepository({
findOneWithRelations: (relations, query) => { findOneWithRelations: (relations, query) => {
if (query?.where?.id === IdMap.getId("order-edit-with-changes")) { if (query?.where?.id === IdMap.getId("order-edit-with-changes")) {
@@ -113,6 +121,16 @@ describe("OrderEditService", () => {
) )
}) })
it("should update an order edit with the right arguments", async () => {
await orderEditService.update(IdMap.getId("order-edit-to-update"), {
internal_note: "test note",
})
expect(orderEditRepository.save).toHaveBeenCalledTimes(1)
expect(orderEditRepository.save).toHaveBeenCalledWith({
internal_note: "test note",
})
})
it("should compute the items from the changes and attach them to the orderEdit", async () => { it("should compute the items from the changes and attach them to the orderEdit", async () => {
const { items, removedItems } = await orderEditService.computeLineItems( const { items, removedItems } = await orderEditService.computeLineItems(
IdMap.getId("order-edit-with-changes") IdMap.getId("order-edit-with-changes")

View File

@@ -1,6 +1,6 @@
import { EntityManager } from "typeorm" import { EntityManager } from "typeorm"
import { FindConfig } from "../types/common" import { FindConfig } from "../types/common"
import { buildQuery } from "../utils" import { buildQuery, isDefined } from "../utils"
import { MedusaError } from "medusa-core-utils" import { MedusaError } from "medusa-core-utils"
import { OrderEditRepository } from "../repositories/order-edit" import { OrderEditRepository } from "../repositories/order-edit"
import { import {
@@ -18,6 +18,7 @@ import {
TotalsService, TotalsService,
} from "./index" } from "./index"
import { CreateOrderEditInput } from "../types/order-edit" import { CreateOrderEditInput } from "../types/order-edit"
import { UpdateOrderEditInput } from "../types/order-edit"
type InjectedDependencies = { type InjectedDependencies = {
manager: EntityManager manager: EntityManager
@@ -31,6 +32,7 @@ type InjectedDependencies = {
export default class OrderEditService extends TransactionBaseService { export default class OrderEditService extends TransactionBaseService {
static readonly Events = { static readonly Events = {
CREATED: "order-edit.created", CREATED: "order-edit.created",
UPDATED: "order-edit.updated",
} }
protected transactionManager_: EntityManager | undefined protected transactionManager_: EntityManager | undefined
@@ -259,6 +261,35 @@ export default class OrderEditService extends TransactionBaseService {
}) })
} }
async update(
orderEditId: string,
data: UpdateOrderEditInput
): Promise<OrderEdit> {
return await this.atomicPhase_(async (manager) => {
const orderEditRepo = manager.getCustomRepository(
this.orderEditRepository_
)
const orderEdit = await this.retrieve(orderEditId)
for (const key of Object.keys(data)) {
if (isDefined(data[key])) {
orderEdit[key] = data[key]
}
}
const result = await orderEditRepo.save(orderEdit)
await this.eventBusService_
.withTransaction(manager)
.emit(OrderEditService.Events.UPDATED, {
id: result.id,
})
return result
})
}
async delete(orderEditId: string): Promise<void> { async delete(orderEditId: string): Promise<void> {
return await this.atomicPhase_(async (manager) => { return await this.atomicPhase_(async (manager) => {
const orderEditRepo = manager.getCustomRepository( const orderEditRepo = manager.getCustomRepository(

View File

@@ -1,5 +1,9 @@
import { OrderEdit } from "../models" import { OrderEdit } from "../models"
export type UpdateOrderEditInput = {
internal_note?: string
}
export const defaultOrderEditRelations: string[] = [ export const defaultOrderEditRelations: string[] = [
"changes", "changes",
"changes.line_item", "changes.line_item",