feat(medusa): Allow to query product types by discount condition id (#2359)
This commit is contained in:
committed by
GitHub
parent
35df4962f8
commit
19ca18e71c
@@ -1,15 +1,25 @@
|
||||
import { Router } from "express"
|
||||
import { ProductType } from "../../../.."
|
||||
import { PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares from "../../../middlewares"
|
||||
import middlewares, { transformQuery } from "../../../middlewares"
|
||||
import "reflect-metadata"
|
||||
import { AdminGetProductTypesParams } from "./list-product-types"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app) => {
|
||||
app.use("/product-types", route)
|
||||
|
||||
route.get("/", middlewares.wrap(require("./list-product-types").default))
|
||||
route.get(
|
||||
"/",
|
||||
transformQuery(AdminGetProductTypesParams, {
|
||||
defaultFields: defaultAdminProductTypeFields,
|
||||
defaultRelations: defaultAdminProductTypeRelations,
|
||||
allowedFields: allowedAdminProductTypeFields,
|
||||
isList: true,
|
||||
}),
|
||||
middlewares.wrap(require("./list-product-types").default)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
import {
|
||||
DateComparisonOperator,
|
||||
FindConfig,
|
||||
StringComparisonOperator,
|
||||
} from "../../../../types/common"
|
||||
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import {
|
||||
allowedAdminProductTypeFields,
|
||||
defaultAdminProductTypeFields,
|
||||
defaultAdminProductTypeRelations,
|
||||
} from "."
|
||||
import { identity, omit, pickBy } from "lodash"
|
||||
import { identity, pickBy } from "lodash"
|
||||
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { ProductType } from "../../../../models/product-type"
|
||||
import ProductTypeService from "../../../../services/product-type"
|
||||
import { Type } from "class-transformer"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { isDefined } from "../../../../utils"
|
||||
|
||||
/**
|
||||
* @oas [get] /product-types
|
||||
@@ -29,6 +19,7 @@ import { isDefined } from "../../../../utils"
|
||||
* - (query) limit=10 {integer} The number of types to return.
|
||||
* - (query) offset=0 {integer} The number of items to skip before the results.
|
||||
* - (query) order {string} The field to sort items by.
|
||||
* - (query) discount_condition_id {string} The discount condition id on which to filter the product types.
|
||||
* - in: query
|
||||
* name: value
|
||||
* style: form
|
||||
@@ -145,37 +136,11 @@ import { isDefined } from "../../../../utils"
|
||||
* $ref: "#/components/responses/500_error"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const validated = await validator(AdminGetProductTypesParams, req.query)
|
||||
|
||||
const typeService: ProductTypeService =
|
||||
req.scope.resolve("productTypeService")
|
||||
|
||||
const listConfig: FindConfig<ProductType> = {
|
||||
select: defaultAdminProductTypeFields as (keyof ProductType)[],
|
||||
relations: defaultAdminProductTypeRelations,
|
||||
skip: validated.offset,
|
||||
take: validated.limit,
|
||||
}
|
||||
|
||||
if (isDefined(validated.order)) {
|
||||
let orderField = validated.order
|
||||
if (validated.order.startsWith("-")) {
|
||||
const [, field] = validated.order.split("-")
|
||||
orderField = field
|
||||
listConfig.order = { [field]: "DESC" }
|
||||
} else {
|
||||
listConfig.order = { [validated.order]: "ASC" }
|
||||
}
|
||||
|
||||
if (!allowedAdminProductTypeFields.includes(orderField)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Order field must be a valid product type field"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const filterableFields = omit(validated, ["limit", "offset"])
|
||||
const { listConfig, filterableFields } = req
|
||||
const { skip, take } = req.listConfig
|
||||
|
||||
const [types, count] = await typeService.listAndCount(
|
||||
pickBy(filterableFields, identity),
|
||||
@@ -185,8 +150,8 @@ export default async (req, res) => {
|
||||
res.status(200).json({
|
||||
product_types: types,
|
||||
count,
|
||||
offset: validated.offset,
|
||||
limit: validated.limit,
|
||||
offset: skip,
|
||||
limit: take,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -227,4 +192,8 @@ export class AdminGetProductTypesParams extends AdminGetProductTypesPaginationPa
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
order?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
discount_condition_id?: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user