feat(medusa): Allow to filter collections by discount condition id (#2411)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import { Request, Response } from "express"
|
||||
import _, { identity } from "lodash"
|
||||
|
||||
import { DateComparisonOperator } from "../../../../types/common"
|
||||
import ProductCollectionService from "../../../../services/product-collection"
|
||||
@@ -18,6 +17,7 @@ import { Type } from "class-transformer"
|
||||
* - (query) title {string} The title of collections to return.
|
||||
* - (query) handle {string} The handle of collections to return.
|
||||
* - (query) q {string} a search term to search titles and handles.
|
||||
* - (query) discount_condition_id {string} The discount condition id on which to filter the product collections.
|
||||
* - in: query
|
||||
* name: created_at
|
||||
* description: Date comparison for when resulting collections were created.
|
||||
@@ -143,22 +143,19 @@ export default async (req: Request, res: Response) => {
|
||||
"productCollectionService"
|
||||
)
|
||||
|
||||
const {
|
||||
validatedQuery: { limit, offset },
|
||||
filterableFields,
|
||||
listConfig,
|
||||
} = req
|
||||
const { filterableFields, listConfig } = req
|
||||
const { skip, take } = listConfig
|
||||
|
||||
const [collections, count] = await productCollectionService.listAndCount(
|
||||
_.pickBy(filterableFields, identity),
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
|
||||
res.status(200).json({
|
||||
collections,
|
||||
count,
|
||||
offset,
|
||||
limit,
|
||||
offset: skip,
|
||||
limit: take,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -202,4 +199,8 @@ export class AdminGetCollectionsParams extends AdminGetCollectionsPaginationPara
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
q?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
discount_condition_id?: string
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
StringComparisonOperator,
|
||||
} from "../../../../types/common"
|
||||
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import { identity, pickBy } from "lodash"
|
||||
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import ProductTypeService from "../../../../services/product-type"
|
||||
@@ -143,7 +142,7 @@ export default async (req, res) => {
|
||||
const { skip, take } = req.listConfig
|
||||
|
||||
const [types, count] = await typeService.listAndCount(
|
||||
pickBy(filterableFields, identity),
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
|
||||
|
||||
@@ -36,6 +36,11 @@ describe("GET /store/collections", () => {
|
||||
expect(ProductCollectionServiceMock.listAndCount).toHaveBeenCalledWith(
|
||||
{},
|
||||
{
|
||||
order: {
|
||||
created_at: "DESC",
|
||||
},
|
||||
relations: [],
|
||||
select: undefined,
|
||||
skip: 0,
|
||||
take: 5,
|
||||
}
|
||||
@@ -60,6 +65,11 @@ describe("GET /store/collections", () => {
|
||||
expect(ProductCollectionServiceMock.listAndCount).toHaveBeenCalledWith(
|
||||
{},
|
||||
{
|
||||
order: {
|
||||
created_at: "DESC",
|
||||
},
|
||||
relations: [],
|
||||
select: undefined,
|
||||
skip: 10,
|
||||
take: 10,
|
||||
}
|
||||
@@ -84,6 +94,11 @@ describe("GET /store/collections", () => {
|
||||
expect(ProductCollectionServiceMock.listAndCount).toHaveBeenCalledWith(
|
||||
{},
|
||||
{
|
||||
order: {
|
||||
created_at: "DESC",
|
||||
},
|
||||
relations: [],
|
||||
select: undefined,
|
||||
skip: 10,
|
||||
take: 20,
|
||||
}
|
||||
|
||||
@@ -1,21 +1,38 @@
|
||||
import { Router } from "express"
|
||||
import { PaginatedResponse } from "./../../../../types/common"
|
||||
import { PaginatedResponse } from "../../../../types/common"
|
||||
import { ProductCollection } from "../../../../"
|
||||
import middlewares from "../../../middlewares"
|
||||
import middlewares, { transformQuery } from "../../../middlewares"
|
||||
import { StoreGetCollectionsParams } from "./list-collections"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app) => {
|
||||
app.use("/collections", route)
|
||||
|
||||
route.get("/", middlewares.wrap(require("./list-collections").default))
|
||||
route.get(
|
||||
"/",
|
||||
transformQuery(StoreGetCollectionsParams, {
|
||||
allowedFields,
|
||||
isList: true,
|
||||
}),
|
||||
middlewares.wrap(require("./list-collections").default)
|
||||
)
|
||||
route.get("/:id", middlewares.wrap(require("./get-collection").default))
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
export const defaultStoreCollectionFields = ["id", "title", "handle"]
|
||||
export const defaultStoreCollectionRelations = ["products"]
|
||||
export const allowedFields = [
|
||||
"id",
|
||||
"title",
|
||||
"handle",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
...defaultStoreCollectionRelations,
|
||||
]
|
||||
|
||||
export type StoreCollectionsListRes = PaginatedResponse & {
|
||||
collections: ProductCollection[]
|
||||
|
||||
@@ -3,7 +3,6 @@ import { IsInt, IsOptional, ValidateNested } from "class-validator"
|
||||
import { DateComparisonOperator } from "../../../../types/common"
|
||||
import ProductCollectionService from "../../../../services/product-collection"
|
||||
import { Type } from "class-transformer"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
|
||||
/**
|
||||
* @oas [get] /collections
|
||||
@@ -105,24 +104,19 @@ import { validator } from "../../../../utils/validator"
|
||||
* $ref: "#/components/responses/500_error"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const validated = await validator(StoreGetCollectionsParams, req.query)
|
||||
const { limit, offset, ...filterableFields } = validated
|
||||
|
||||
const productCollectionService: ProductCollectionService = req.scope.resolve(
|
||||
"productCollectionService"
|
||||
)
|
||||
|
||||
const listConfig = {
|
||||
skip: offset,
|
||||
take: limit,
|
||||
}
|
||||
const { listConfig, filterableFields } = req
|
||||
const { skip, take } = req.listConfig
|
||||
|
||||
const [collections, count] = await productCollectionService.listAndCount(
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
|
||||
res.status(200).json({ collections, count, limit, offset })
|
||||
res.status(200).json({ collections, count, limit: take, offset: skip })
|
||||
}
|
||||
|
||||
export class StoreGetCollectionsParams {
|
||||
|
||||
Reference in New Issue
Block a user