fix: adds date filters on store collection + region list api (#1216)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user