From 5f5a2af30b4c8ddfca0db11ba4d50c5185335fd2 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Wed, 9 Mar 2022 10:55:05 +0100 Subject: [PATCH] Feat: Add customer search controller (#1111) * add controllers directory and loaders * move controllers to pure functions * controller refactor * update imports --- .../routes/admin/customers/list-customers.ts | 40 +++------------ .../src/controllers/__mocks__/customers.ts | 0 .../medusa/src/controllers/customers/index.ts | 2 + .../controllers/customers/list-customers.ts | 49 +++++++++++++++++++ 4 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 packages/medusa/src/controllers/__mocks__/customers.ts create mode 100644 packages/medusa/src/controllers/customers/index.ts create mode 100644 packages/medusa/src/controllers/customers/list-customers.ts diff --git a/packages/medusa/src/api/routes/admin/customers/list-customers.ts b/packages/medusa/src/api/routes/admin/customers/list-customers.ts index b3f34cc662..947bdd7c45 100644 --- a/packages/medusa/src/api/routes/admin/customers/list-customers.ts +++ b/packages/medusa/src/api/routes/admin/customers/list-customers.ts @@ -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 = { - 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 { diff --git a/packages/medusa/src/controllers/__mocks__/customers.ts b/packages/medusa/src/controllers/__mocks__/customers.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/medusa/src/controllers/customers/index.ts b/packages/medusa/src/controllers/customers/index.ts new file mode 100644 index 0000000000..7b4ac3e2ce --- /dev/null +++ b/packages/medusa/src/controllers/customers/index.ts @@ -0,0 +1,2 @@ +import listAndCount from "./list-customers" +export default { listAndCount } diff --git a/packages/medusa/src/controllers/customers/list-customers.ts b/packages/medusa/src/controllers/customers/list-customers.ts new file mode 100644 index 0000000000..973abde558 --- /dev/null +++ b/packages/medusa/src/controllers/customers/list-customers.ts @@ -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 => { + 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 = { + 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