From 3cd4108915bca2889550c18c5c2fc95b8300d520 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 18 Mar 2022 11:02:40 +0100 Subject: [PATCH] fix: adds date filters on store collection + region list api (#1216) --- .../store/collections/list-collections.ts | 50 +++++++++++-------- .../routes/store/products/list-products.ts | 7 +-- .../api/routes/store/regions/list-regions.ts | 22 ++++++-- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/packages/medusa/src/api/routes/store/collections/list-collections.ts b/packages/medusa/src/api/routes/store/collections/list-collections.ts index b0402dc0f1..f4e94f4758 100644 --- a/packages/medusa/src/api/routes/store/collections/list-collections.ts +++ b/packages/medusa/src/api/routes/store/collections/list-collections.ts @@ -1,7 +1,9 @@ import { Type } from "class-transformer" -import { IsOptional, IsInt } from "class-validator" +import { ValidateNested, IsOptional, IsInt } from "class-validator" import ProductCollectionService from "../../../../services/product-collection" import { validator } from "../../../../utils/validator" +import { DateComparisonOperator } from "../../../../types/common" + /** * @oas [get] /collections * operationId: "GetCollections" @@ -23,30 +25,24 @@ import { validator } from "../../../../utils/validator" * $ref: "#/components/schemas/product_collection" */ export default async (req, res) => { - try { - const { limit, offset } = await validator( - StoreGetCollectionsParams, - req.query - ) - const selector = {} + const validated = await validator(StoreGetCollectionsParams, req.query) + const { limit, offset, ...filterableFields } = validated - const productCollectionService: ProductCollectionService = - req.scope.resolve("productCollectionService") + const productCollectionService: ProductCollectionService = req.scope.resolve( + "productCollectionService" + ) - const listConfig = { - skip: offset, - take: limit, - } - - const [collections, count] = await productCollectionService.listAndCount( - selector, - listConfig - ) - - res.status(200).json({ collections, count, limit, offset }) - } catch (err) { - console.log(err) + const listConfig = { + skip: offset, + take: limit, } + + const [collections, count] = await productCollectionService.listAndCount( + filterableFields, + listConfig + ) + + res.status(200).json({ collections, count, limit, offset }) } export class StoreGetCollectionsParams { @@ -59,4 +55,14 @@ export class StoreGetCollectionsParams { @IsInt() @Type(() => Number) offset?: number = 0 + + @IsOptional() + @ValidateNested() + @Type(() => DateComparisonOperator) + created_at?: DateComparisonOperator + + @IsOptional() + @ValidateNested() + @Type(() => DateComparisonOperator) + updated_at?: DateComparisonOperator } diff --git a/packages/medusa/src/api/routes/store/products/list-products.ts b/packages/medusa/src/api/routes/store/products/list-products.ts index 55b8ea99c3..29c398a564 100644 --- a/packages/medusa/src/api/routes/store/products/list-products.ts +++ b/packages/medusa/src/api/routes/store/products/list-products.ts @@ -7,7 +7,7 @@ import { IsString, ValidateNested, } from "class-validator" -import { omit, pickBy, identity } from "lodash" +import { omit, pickBy } from "lodash" import { defaultStoreProductsRelations } from "." import { ProductService } from "../../../../services" import { DateComparisonOperator } from "../../../../types/common" @@ -174,9 +174,4 @@ export class StoreGetProductsParams extends StoreGetProductsPaginationParams { @ValidateNested() @Type(() => DateComparisonOperator) updated_at?: DateComparisonOperator - - @ValidateNested() - @IsOptional() - @Type(() => DateComparisonOperator) - deleted_at?: DateComparisonOperator } diff --git a/packages/medusa/src/api/routes/store/regions/list-regions.ts b/packages/medusa/src/api/routes/store/regions/list-regions.ts index bfd824bd8f..2bb2a69a7e 100644 --- a/packages/medusa/src/api/routes/store/regions/list-regions.ts +++ b/packages/medusa/src/api/routes/store/regions/list-regions.ts @@ -1,7 +1,10 @@ import { Type } from "class-transformer" -import { IsInt, IsOptional } from "class-validator" +import { omit } from "lodash" +import { ValidateNested, IsInt, IsOptional } from "class-validator" import RegionService from "../../../../services/region" import { validator } from "../../../../utils/validator" +import { DateComparisonOperator } from "../../../../types/common" + /** * @oas [get] /regions * operationId: GetRegions @@ -31,11 +34,12 @@ import { validator } from "../../../../utils/validator" * $ref: "#/components/schemas/region" */ export default async (req, res) => { - const { limit, offset } = await validator(StoreGetRegionsParams, req.query) + const validated = await validator(StoreGetRegionsParams, req.query) + const { limit, offset } = validated const regionService: RegionService = req.scope.resolve("regionService") - const selector = {} + const filterableFields = omit(validated, ["limit", "offset"]) const listConfig = { relations: ["countries", "payment_providers", "fulfillment_providers"], @@ -43,7 +47,7 @@ export default async (req, res) => { take: limit, } - const regions = await regionService.list(selector, listConfig) + const regions = await regionService.list(filterableFields, listConfig) res.json({ regions }) } @@ -58,4 +62,14 @@ export class StoreGetRegionsParams { @IsInt() @Type(() => Number) offset?: number = 0 + + @IsOptional() + @ValidateNested() + @Type(() => DateComparisonOperator) + created_at?: DateComparisonOperator + + @IsOptional() + @ValidateNested() + @Type(() => DateComparisonOperator) + updated_at?: DateComparisonOperator }