feat(medusa, medusa-js, medusa-react): Support listing order edits (#2436)
* feat(medusa, medusa-js, medusa-react): Support listing order edits * feat(medusa): Update OAS * fix oas Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
1 parent
69e579758f
commit
143f8543e5
12 files changed
+433
-14
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { orderEditServiceMock } from "../../../../../services/__mocks__/order-edit"
|
||||
import OrderEditingFeatureFlag from "../../../../../loaders/feature-flags/order-editing"
|
||||
import {
|
||||
defaultOrderEditFields,
|
||||
defaultOrderEditRelations,
|
||||
} from "../../../../../types/order-edit"
|
||||
|
||||
describe("GET /admin/order-edits", () => {
|
||||
describe("successfully list order edits", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request("GET", `/admin/order-edits`, {
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
flags: [OrderEditingFeatureFlag],
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("calls orderService listAndCount", () => {
|
||||
expect(orderEditServiceMock.listAndCount).toHaveBeenCalledTimes(1)
|
||||
expect(orderEditServiceMock.listAndCount).toHaveBeenCalledWith(
|
||||
{},
|
||||
{
|
||||
select: defaultOrderEditFields,
|
||||
relations: defaultOrderEditRelations,
|
||||
order: { created_at: "DESC" },
|
||||
skip: 0,
|
||||
take: 20,
|
||||
}
|
||||
)
|
||||
expect(orderEditServiceMock.decorateTotals).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Request, Response } from "express"
|
||||
import { OrderEditService } from "../../../../services"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
|
||||
/**
|
||||
* @oas [get] /order-edits/{id}
|
||||
@@ -9,6 +10,8 @@ import { OrderEditService } from "../../../../services"
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) id=* {string} The ID of the OrderEdit.
|
||||
* - (query) expand {string} Comma separated list of relations to include in the results.
|
||||
* - (query) fields {string} Comma separated list of fields to include in the results.
|
||||
* x-codeSamples:
|
||||
* - lang: JavaScript
|
||||
* label: JS Client
|
||||
@@ -64,3 +67,5 @@ export default async (req: Request, res: Response) => {
|
||||
|
||||
return res.json({ order_edit: orderEdit })
|
||||
}
|
||||
|
||||
export class GetOrderEditsOrderEditParams extends FindParams {}
|
||||
@@ -4,7 +4,7 @@ import middlewares, {
|
||||
transformBody,
|
||||
transformQuery,
|
||||
} from "../../../middlewares"
|
||||
import { DeleteResponse, FindParams } from "../../../../types/common"
|
||||
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
||||
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
|
||||
import OrderEditingFeatureFlag from "../../../../loaders/feature-flags/order-editing"
|
||||
import {
|
||||
@@ -16,6 +16,8 @@ import { AdminPostOrderEditsOrderEditReq } from "./update-order-edit"
|
||||
import { AdminPostOrderEditsReq } from "./create-order-edit"
|
||||
import { AdminPostOrderEditsEditLineItemsReq } from "./add-line-item"
|
||||
import { AdminPostOrderEditsEditLineItemsLineItemReq } from "./update-order-edit-line-item"
|
||||
import { GetOrderEditsParams } from "./list-order-edit"
|
||||
import { GetOrderEditsOrderEditParams } from "./get-order-edit"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -32,9 +34,19 @@ export default (app) => {
|
||||
middlewares.wrap(require("./create-order-edit").default)
|
||||
)
|
||||
|
||||
route.get(
|
||||
"/",
|
||||
transformQuery(GetOrderEditsParams, {
|
||||
defaultFields: defaultOrderEditFields,
|
||||
defaultRelations: defaultOrderEditRelations,
|
||||
isList: true,
|
||||
}),
|
||||
middlewares.wrap(require("./list-order-edit").default)
|
||||
)
|
||||
|
||||
route.get(
|
||||
"/:id",
|
||||
transformQuery(FindParams, {
|
||||
transformQuery(GetOrderEditsOrderEditParams, {
|
||||
defaultRelations: defaultOrderEditRelations,
|
||||
defaultFields: defaultOrderEditFields,
|
||||
isList: false,
|
||||
@@ -93,6 +105,9 @@ export default (app) => {
|
||||
export type AdminOrderEditsRes = {
|
||||
order_edit: OrderEdit
|
||||
}
|
||||
export type AdminOrderEditsListRes = PaginatedResponse & {
|
||||
order_edits: OrderEdit[]
|
||||
}
|
||||
export type AdminOrderEditDeleteRes = DeleteResponse
|
||||
export type AdminOrderEditItemChangeDeleteRes = {
|
||||
id: string
|
||||
@@ -103,5 +118,6 @@ export type AdminOrderEditItemChangeDeleteRes = {
|
||||
export * from "./update-order-edit"
|
||||
export * from "./update-order-edit-line-item"
|
||||
export * from "./create-order-edit"
|
||||
|
||||
export * from "./get-order-edit"
|
||||
export * from "./list-order-edit"
|
||||
export * from "./add-line-item"
|
||||
@@ -0,0 +1,93 @@
|
||||
import { Request, Response } from "express"
|
||||
import { OrderEditService } from "../../../../services"
|
||||
import { extendedFindParamsMixin } from "../../../../types/common"
|
||||
import { IsOptional, IsString } from "class-validator"
|
||||
|
||||
/**
|
||||
* @oas [get] /order-edits
|
||||
* operationId: "GetOrderEdits"
|
||||
* summary: "List an OrderEdit"
|
||||
* description: "List a OrderEdit."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (query) q {string} Query used for searching order edit internal note.
|
||||
* - (query) limit=20 {number} The number of items in the response
|
||||
* - (query) offset=0 {number} The offset of items in response
|
||||
* - (query) expand {string} Comma separated list of relations to include in the results.
|
||||
* - (query) fields {string} Comma separated list of fields to include in the results.
|
||||
* 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
|
||||
* medusa.admin.orderEdit.list()
|
||||
* .then(({ order_edits }) => {
|
||||
* console.log(order_edits)
|
||||
* })
|
||||
* - lang: Shell
|
||||
* label: cURL
|
||||
* source: |
|
||||
* curl --location --request GET 'https://medusa-url.com/admin/order-edits' \
|
||||
* --header 'Authorization: Bearer {api_token}'
|
||||
* security:
|
||||
* - api_token: []
|
||||
* - cookie_auth: []
|
||||
* tags:
|
||||
* - OrderEdit
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* order_edits:
|
||||
* type: array
|
||||
* $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 orderEditService: OrderEditService =
|
||||
req.scope.resolve("orderEditService")
|
||||
|
||||
const { filterableFields, listConfig } = req
|
||||
const { skip, take } = listConfig
|
||||
|
||||
const [orderEdits, orderEditCount] = await orderEditService.listAndCount(
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
|
||||
for (let orderEdit of orderEdits) {
|
||||
orderEdit = await orderEditService.decorateTotals(orderEdit)
|
||||
}
|
||||
|
||||
return res.json({
|
||||
order_edits: orderEdits,
|
||||
count: orderEditCount,
|
||||
limit: take,
|
||||
offset: skip,
|
||||
})
|
||||
}
|
||||
|
||||
export class GetOrderEditsParams extends extendedFindParamsMixin({
|
||||
limit: 20,
|
||||
offset: 0,
|
||||
}) {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
q?: string
|
||||
}
|
||||
@@ -93,6 +93,9 @@ export const orderEditServiceMock = {
|
||||
}
|
||||
return Promise.resolve(undefined)
|
||||
}),
|
||||
listAndCount: jest.fn().mockImplementation(() => {
|
||||
return Promise.resolve([[orderEdit], 1])
|
||||
}),
|
||||
computeLineItems: jest.fn().mockImplementation((orderEdit) => {
|
||||
return Promise.resolve(computeLineItems(orderEdit))
|
||||
}),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { DeepPartial, EntityManager, IsNull } from "typeorm"
|
||||
import { DeepPartial, EntityManager, ILike, IsNull } from "typeorm"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
|
||||
import { FindConfig } from "../types/common"
|
||||
import { buildQuery, isDefined } from "../utils"
|
||||
import { FindConfig, Selector } from "../types/common"
|
||||
import { buildQuery, isDefined, isString } from "../utils"
|
||||
import { OrderEditRepository } from "../repositories/order-edit"
|
||||
import {
|
||||
Cart,
|
||||
@@ -109,6 +109,38 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
return orderEdit
|
||||
}
|
||||
|
||||
async listAndCount(
|
||||
selector: Selector<OrderEdit> & { q?: string },
|
||||
config?: FindConfig<OrderEdit>
|
||||
): Promise<[OrderEdit[], number]> {
|
||||
const manager = this.transactionManager_ ?? this.manager_
|
||||
const orderEditRepository = manager.getCustomRepository(
|
||||
this.orderEditRepository_
|
||||
)
|
||||
|
||||
let q
|
||||
if (isString(selector.q)) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
|
||||
if (q) {
|
||||
query.where.internal_note = ILike(`%${q}%`)
|
||||
}
|
||||
|
||||
return await orderEditRepository.findAndCount(query)
|
||||
}
|
||||
|
||||
async list(
|
||||
selector: Selector<OrderEdit>,
|
||||
config?: FindConfig<OrderEdit>
|
||||
): Promise<OrderEdit[]> {
|
||||
const [orderEdits] = await this.listAndCount(selector, config)
|
||||
return orderEdits
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute and return the different totals from the order edit id
|
||||
* @param orderEditId
|
||||
|
||||
@@ -34,6 +34,7 @@ export const defaultOrderEditFields: (keyof OrderEdit)[] = [
|
||||
"changes",
|
||||
"order_id",
|
||||
"created_by",
|
||||
"created_at",
|
||||
"requested_by",
|
||||
"requested_at",
|
||||
"confirmed_by",
|
||||
|
||||
Reference in new issue
Block a user