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 {
|
||||
|
||||
@@ -1,6 +1,36 @@
|
||||
import { EntityRepository, Repository } from "typeorm"
|
||||
import { ProductCollection } from "../models/product-collection"
|
||||
import { ProductCollection } from "../models"
|
||||
import { ExtendedFindConfig } from "../types/common"
|
||||
|
||||
@EntityRepository(ProductCollection)
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductCollectionRepository extends Repository<ProductCollection> {}
|
||||
export class ProductCollectionRepository extends Repository<ProductCollection> {
|
||||
async findAndCountByDiscountConditionId(
|
||||
conditionId: string,
|
||||
query: ExtendedFindConfig<ProductCollection, Partial<ProductCollection>>
|
||||
): Promise<[ProductCollection[], number]> {
|
||||
const qb = this.createQueryBuilder("pc")
|
||||
|
||||
if (query?.select) {
|
||||
qb.select(query.select.map((select) => `pc.${select}`))
|
||||
}
|
||||
|
||||
if (query.skip) {
|
||||
qb.skip(query.skip)
|
||||
}
|
||||
|
||||
if (query.take) {
|
||||
qb.take(query.take)
|
||||
}
|
||||
|
||||
return await qb
|
||||
.where(query.where)
|
||||
.innerJoin(
|
||||
"discount_condition_product_collection",
|
||||
"dc_pc",
|
||||
`dc_pc.product_collection_id = pc.id AND dc_pc.condition_id = :dcId`,
|
||||
{ dcId: conditionId }
|
||||
)
|
||||
.getManyAndCount()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ import { TransactionBaseService } from "../interfaces"
|
||||
import { ProductCollection } from "../models"
|
||||
import { ProductRepository } from "../repositories/product"
|
||||
import { ProductCollectionRepository } from "../repositories/product-collection"
|
||||
import { ExtendedFindConfig, FindConfig, QuerySelector } from "../types/common"
|
||||
import { FindConfig } from "../types/common"
|
||||
import {
|
||||
CreateProductCollection,
|
||||
UpdateProductCollection,
|
||||
} from "../types/product-collection"
|
||||
import { buildQuery, setMetadata } from "../utils"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import EventBusService from "./event-bus"
|
||||
|
||||
@@ -219,15 +219,14 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
* @return the result of the find operation
|
||||
*/
|
||||
async list(
|
||||
selector = {},
|
||||
selector: Partial<ProductCollection> & {
|
||||
q?: string
|
||||
discount_condition_id?: string
|
||||
} = {},
|
||||
config = { skip: 0, take: 20 }
|
||||
): Promise<ProductCollection[]> {
|
||||
const productCollectionRepo = this.manager_.getCustomRepository(
|
||||
this.productCollectionRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
return await productCollectionRepo.find(query)
|
||||
const [collections] = await this.listAndCount(selector, config)
|
||||
return collections
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,7 +236,10 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
* @return the result of the find operation
|
||||
*/
|
||||
async listAndCount(
|
||||
selector: QuerySelector<ProductCollection> = {},
|
||||
selector: Partial<ProductCollection> & {
|
||||
q?: string
|
||||
discount_condition_id?: string
|
||||
} = {},
|
||||
config: FindConfig<ProductCollection> = { skip: 0, take: 20 }
|
||||
): Promise<[ProductCollection[], number]> {
|
||||
const productCollectionRepo = this.manager_.getCustomRepository(
|
||||
@@ -245,17 +247,12 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
)
|
||||
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
if (isString(selector.q)) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query = buildQuery(
|
||||
selector,
|
||||
config
|
||||
) as ExtendedFindConfig<ProductCollection> & {
|
||||
where: (qb: any) => void
|
||||
}
|
||||
const query = buildQuery(selector, config)
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
@@ -278,6 +275,15 @@ class ProductCollectionService extends TransactionBaseService {
|
||||
}
|
||||
}
|
||||
|
||||
if (query.where.discount_condition_id) {
|
||||
const discountConditionId = query.where.discount_condition_id as string
|
||||
delete query.where.discount_condition_id
|
||||
return await productCollectionRepo.findAndCountByDiscountConditionId(
|
||||
discountConditionId,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
return await productCollectionRepo.findAndCount(query)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user