feat: list customer groups (#1099)

This commit is contained in:
Frane Polić
2022-03-02 11:38:44 +01:00
committed by GitHub
parent e844f4a5b7
commit a514d84ccf
11 changed files with 294 additions and 65 deletions
@@ -3,7 +3,7 @@ import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator"
import _, { identity } from "lodash"
import {
defaultAdminCollectionsFields,
defaultAdminCollectionsRelations
defaultAdminCollectionsRelations,
} from "."
import ProductCollectionService from "../../../../services/product-collection"
import { DateComparisonOperator } from "../../../../types/common"
@@ -4,7 +4,7 @@ import { validator } from "../../../../utils/validator"
import { defaultAdminCustomerGroupsRelations } from "."
/**
* @oas [get] /customer-group/{id}
* @oas [get] /customer-groups/{id}
* operationId: "GetCustomerGroupsGroup"
* summary: "Retrieve a CustomerGroup"
* description: "Retrieves a Customer Group."
@@ -8,6 +8,7 @@ const route = Router()
export default (app) => {
app.use("/customer-groups", route)
route.get("/", middlewares.wrap(require("./list-customer-groups").default))
route.get("/:id", middlewares.wrap(require("./get-customer-group").default))
route.post("/", middlewares.wrap(require("./create-customer-group").default))
route.post(
@@ -0,0 +1,109 @@
import { IsNumber, IsOptional, IsString } from "class-validator"
import { Type } from "class-transformer"
import omit from "lodash/omit"
import { validator } from "../../../../utils/validator"
import { CustomerGroupService } from "../../../../services"
import { CustomerGroup } from "../../../../models/customer-group"
import { FindConfig } from "../../../../types/common"
import { defaultAdminCustomerGroupsRelations } from "."
import { FilterableCustomerGroupProps } from "../../../../types/customer-groups"
/**
* @oas [get] /customer-groups
* operationId: "GetCustomerGroups"
* summary: "Retrieve a list of customer groups"
* description: "Retrieve a list of customer groups."
* x-authenticated: true
* parameters:
* - (query) q {string} Query used for searching user group names.
* - (query) offset {string} How many groups to skip in the result.
* - (query) id {string} Ids of the groups to search for.
* - (query) order {string} to retrieve customer groups in.
* - (query) created_at {DateComparisonOperator} Date comparison for when resulting customer group was created, i.e. less than, greater than etc.
* - (query) updated_at {DateComparisonOperator} Date comparison for when resulting ustomer group was updated, i.e. less than, greater than etc.
* - (query) offset {string} How many customer groups to skip in the result.
* - (query) limit {string} Limit the number of customer groups returned.
* - (query) expand {string} (Comma separated) Which fields should be expanded in each customer groups of the result.
* tags:
* - CustomerGroup
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* customerGroup:
* $ref: "#/components/schemas/customer_group"
*/
export default async (req, res) => {
const validated = await validator(AdminGetCustomerGroupsParams, req.query)
const customerGroupService: CustomerGroupService = req.scope.resolve(
"customerGroupService"
)
let expandFields: string[] = []
if (validated.expand) {
expandFields = validated.expand.split(",")
}
const listConfig: FindConfig<CustomerGroup> = {
relations: expandFields.length
? expandFields
: defaultAdminCustomerGroupsRelations,
skip: validated.offset,
take: validated.limit,
order: { created_at: "DESC" } as { [k: string]: "DESC" },
}
if (typeof validated.order !== "undefined") {
if (validated.order.startsWith("-")) {
const [, field] = validated.order.split("-")
listConfig.order = { [field]: "DESC" }
} else {
listConfig.order = { [validated.order]: "ASC" }
}
}
const filterableFields = omit(validated, [
"limit",
"offset",
"expand",
"order",
])
const [data, count] = await customerGroupService.listAndCount(
filterableFields,
listConfig
)
res.json({
count,
customer_groups: data,
offset: validated.offset,
limit: validated.limit,
})
}
export class AdminGetCustomerGroupsParams extends FilterableCustomerGroupProps {
@IsString()
@IsOptional()
order?: string
@IsNumber()
@IsOptional()
@Type(() => Number)
offset?: number = 0
@IsNumber()
@IsOptional()
@Type(() => Number)
limit?: number = 10
@IsString()
@IsOptional()
expand?: string
}