fix: adds date filters on store collection + region list api (#1216)

This commit is contained in:
Sebastian Rindom authored and GitHub committed 2022-03-18 11:02:40 +01:00
1 parent 36bfdfe6e1
commit 3cd4108915
3 files changed
+47 -32

No files matched your search

@@ -1,7 +1,9 @@
import { Type } from "class-transformer" 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 ProductCollectionService from "../../../../services/product-collection"
import { validator } from "../../../../utils/validator" import { validator } from "../../../../utils/validator"
import { DateComparisonOperator } from "../../../../types/common"
/** /**
* @oas [get] /collections * @oas [get] /collections
* operationId: "GetCollections" * operationId: "GetCollections"
@@ -23,30 +25,24 @@ import { validator } from "../../../../utils/validator"
* $ref: "#/components/schemas/product_collection" * $ref: "#/components/schemas/product_collection"
*/ */
export default async (req, res) => { export default async (req, res) => {
try { const validated = await validator(StoreGetCollectionsParams, req.query)
const { limit, offset } = await validator( const { limit, offset, ...filterableFields } = validated
StoreGetCollectionsParams,
req.query
)
const selector = {}
const productCollectionService: ProductCollectionService = const productCollectionService: ProductCollectionService = req.scope.resolve(
req.scope.resolve("productCollectionService") "productCollectionService"
)
const listConfig = { const listConfig = {
skip: offset, skip: offset,
take: limit, take: limit,
}
const [collections, count] = await productCollectionService.listAndCount(
selector,
listConfig
)
res.status(200).json({ collections, count, limit, offset })
} catch (err) {
console.log(err)
} }
const [collections, count] = await productCollectionService.listAndCount(
filterableFields,
listConfig
)
res.status(200).json({ collections, count, limit, offset })
} }
export class StoreGetCollectionsParams { export class StoreGetCollectionsParams {
@@ -59,4 +55,14 @@ export class StoreGetCollectionsParams {
@IsInt() @IsInt()
@Type(() => Number) @Type(() => Number)
offset?: number = 0 offset?: number = 0
@IsOptional()
@ValidateNested()
@Type(() => DateComparisonOperator)
created_at?: DateComparisonOperator
@IsOptional()
@ValidateNested()
@Type(() => DateComparisonOperator)
updated_at?: DateComparisonOperator
} }
@@ -7,7 +7,7 @@ import {
IsString, IsString,
ValidateNested, ValidateNested,
} from "class-validator" } from "class-validator"
import { omit, pickBy, identity } from "lodash" import { omit, pickBy } from "lodash"
import { defaultStoreProductsRelations } from "." import { defaultStoreProductsRelations } from "."
import { ProductService } from "../../../../services" import { ProductService } from "../../../../services"
import { DateComparisonOperator } from "../../../../types/common" import { DateComparisonOperator } from "../../../../types/common"
@@ -174,9 +174,4 @@ export class StoreGetProductsParams extends StoreGetProductsPaginationParams {
@ValidateNested() @ValidateNested()
@Type(() => DateComparisonOperator) @Type(() => DateComparisonOperator)
updated_at?: DateComparisonOperator updated_at?: DateComparisonOperator
@ValidateNested()
@IsOptional()
@Type(() => DateComparisonOperator)
deleted_at?: DateComparisonOperator
} }
@@ -1,7 +1,10 @@
import { Type } from "class-transformer" 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 RegionService from "../../../../services/region"
import { validator } from "../../../../utils/validator" import { validator } from "../../../../utils/validator"
import { DateComparisonOperator } from "../../../../types/common"
/** /**
* @oas [get] /regions * @oas [get] /regions
* operationId: GetRegions * operationId: GetRegions
@@ -31,11 +34,12 @@ import { validator } from "../../../../utils/validator"
* $ref: "#/components/schemas/region" * $ref: "#/components/schemas/region"
*/ */
export default async (req, res) => { 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 regionService: RegionService = req.scope.resolve("regionService")
const selector = {} const filterableFields = omit(validated, ["limit", "offset"])
const listConfig = { const listConfig = {
relations: ["countries", "payment_providers", "fulfillment_providers"], relations: ["countries", "payment_providers", "fulfillment_providers"],
@@ -43,7 +47,7 @@ export default async (req, res) => {
take: limit, take: limit,
} }
const regions = await regionService.list(selector, listConfig) const regions = await regionService.list(filterableFields, listConfig)
res.json({ regions }) res.json({ regions })
} }
@@ -58,4 +62,14 @@ export class StoreGetRegionsParams {
@IsInt() @IsInt()
@Type(() => Number) @Type(() => Number)
offset?: number = 0 offset?: number = 0
@IsOptional()
@ValidateNested()
@Type(() => DateComparisonOperator)
created_at?: DateComparisonOperator
@IsOptional()
@ValidateNested()
@Type(() => DateComparisonOperator)
updated_at?: DateComparisonOperator
} }