Feat: Add customer search controller (#1111)
* add controllers directory and loaders * move controllers to pure functions * controller refactor * update imports
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
import { IsNumber, IsOptional, IsString } from "class-validator"
|
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||||
import { Customer } from "../../../.."
|
import customerController from "../../../../controllers/customers"
|
||||||
import CustomerService from "../../../../services/customer"
|
|
||||||
import { FindConfig } from "../../../../types/common"
|
|
||||||
import { AdminListCustomerSelector } from "../../../../types/customers"
|
import { AdminListCustomerSelector } from "../../../../types/customers"
|
||||||
import { validator } from "../../../../utils/validator"
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /customers
|
* @oas [get] /customers
|
||||||
* operationId: "GetCustomers"
|
* operationId: "GetCustomers"
|
||||||
@@ -24,38 +21,13 @@ import { validator } from "../../../../utils/validator"
|
|||||||
* $ref: "#/components/schemas/customer"
|
* $ref: "#/components/schemas/customer"
|
||||||
*/
|
*/
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const validated = await validator(AdminGetCustomersParams, req.query)
|
const result = await customerController.listAndCount(
|
||||||
|
req.scope,
|
||||||
const customerService: CustomerService = req.scope.resolve("customerService")
|
req.query,
|
||||||
|
req.body
|
||||||
const selector: AdminListCustomerSelector = {}
|
|
||||||
|
|
||||||
if (validated.q) {
|
|
||||||
selector.q = validated.q
|
|
||||||
}
|
|
||||||
|
|
||||||
let expandFields: string[] = []
|
|
||||||
if (validated.expand) {
|
|
||||||
expandFields = validated.expand.split(",")
|
|
||||||
}
|
|
||||||
|
|
||||||
const listConfig: FindConfig<Customer> = {
|
|
||||||
relations: expandFields,
|
|
||||||
skip: validated.offset,
|
|
||||||
take: validated.limit,
|
|
||||||
}
|
|
||||||
|
|
||||||
const [customers, count] = await customerService.listAndCount(
|
|
||||||
selector,
|
|
||||||
listConfig
|
|
||||||
)
|
)
|
||||||
|
|
||||||
res.json({
|
res.json(result)
|
||||||
customers,
|
|
||||||
count,
|
|
||||||
offset: validated.offset,
|
|
||||||
limit: validated.limit,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AdminGetCustomersParams extends AdminListCustomerSelector {
|
export class AdminGetCustomersParams extends AdminListCustomerSelector {
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
import listAndCount from "./list-customers"
|
||||||
|
export default { listAndCount }
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import { omit, pickBy } from "lodash"
|
||||||
|
import { AdminGetCustomersParams } from "../../api/routes/admin/customers/list-customers"
|
||||||
|
import { AdminCustomersListRes } from "../../api"
|
||||||
|
import { CustomerService } from "../../services"
|
||||||
|
import { FindConfig } from "../../types/common"
|
||||||
|
import { validator } from "../../utils/validator"
|
||||||
|
import { Customer } from "../../models/customer"
|
||||||
|
|
||||||
|
const listAndCount = async (
|
||||||
|
scope,
|
||||||
|
query,
|
||||||
|
body,
|
||||||
|
context = {}
|
||||||
|
): Promise<AdminCustomersListRes> => {
|
||||||
|
const validatedQueryParams = await validator(AdminGetCustomersParams, query)
|
||||||
|
|
||||||
|
const customerService: CustomerService = scope.resolve("customerService")
|
||||||
|
|
||||||
|
let expandFields: string[] = []
|
||||||
|
if (validatedQueryParams.expand) {
|
||||||
|
expandFields = validatedQueryParams.expand.split(",")
|
||||||
|
}
|
||||||
|
|
||||||
|
const listConfig: FindConfig<Customer> = {
|
||||||
|
relations: expandFields,
|
||||||
|
skip: validatedQueryParams.offset,
|
||||||
|
take: validatedQueryParams.limit,
|
||||||
|
}
|
||||||
|
|
||||||
|
const filterableFields = omit(validatedQueryParams, [
|
||||||
|
"limit",
|
||||||
|
"offset",
|
||||||
|
"expand",
|
||||||
|
])
|
||||||
|
|
||||||
|
const [customers, count] = await customerService.listAndCount(
|
||||||
|
pickBy(filterableFields, (val) => typeof val !== "undefined"),
|
||||||
|
listConfig
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
customers,
|
||||||
|
count,
|
||||||
|
offset: validatedQueryParams.offset,
|
||||||
|
limit: validatedQueryParams.limit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default listAndCount
|
||||||
Reference in New Issue
Block a user