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 { IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import { Customer } from "../../../.."
|
||||
import CustomerService from "../../../../services/customer"
|
||||
import { FindConfig } from "../../../../types/common"
|
||||
import customerController from "../../../../controllers/customers"
|
||||
import { AdminListCustomerSelector } from "../../../../types/customers"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
/**
|
||||
* @oas [get] /customers
|
||||
* operationId: "GetCustomers"
|
||||
@@ -24,38 +21,13 @@ import { validator } from "../../../../utils/validator"
|
||||
* $ref: "#/components/schemas/customer"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const validated = await validator(AdminGetCustomersParams, req.query)
|
||||
|
||||
const customerService: CustomerService = req.scope.resolve("customerService")
|
||||
|
||||
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
|
||||
const result = await customerController.listAndCount(
|
||||
req.scope,
|
||||
req.query,
|
||||
req.body
|
||||
)
|
||||
|
||||
res.json({
|
||||
customers,
|
||||
count,
|
||||
offset: validated.offset,
|
||||
limit: validated.limit,
|
||||
})
|
||||
res.json(result)
|
||||
}
|
||||
|
||||
export class AdminGetCustomersParams extends AdminListCustomerSelector {
|
||||
|
||||
Reference in New Issue
Block a user