From e53c06eab8a37579f33c547d0373edc082cc308b Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Sat, 2 Jul 2022 13:01:52 +0200 Subject: [PATCH] feat(medusa): Apply query transformers to Collection and CustomerGroups (#1667) --- .../admin/__snapshots__/colllections.js.snap | 86 +++++++++---------- .../api/__tests__/admin/colllections.js | 28 +++--- .../routes/admin/collections/add-products.ts | 10 +-- .../admin/collections/create-collection.ts | 9 +- .../admin/collections/delete-collection.ts | 3 +- .../admin/collections/get-collection.ts | 11 ++- .../src/api/routes/admin/collections/index.ts | 59 ++++++++++--- .../admin/collections/list-collections.ts | 28 +++--- .../admin/collections/remove-products.ts | 15 ++-- .../admin/collections/update-collection.ts | 8 +- .../customer-groups/add-customers-batch.ts | 3 +- .../customer-groups/create-customer-group.ts | 3 +- .../customer-groups/delete-customer-group.ts | 3 +- .../customer-groups/delete-customers-batch.ts | 3 +- .../get-customer-group-customers.ts | 3 +- .../customer-groups/get-customer-group.ts | 26 ++---- .../api/routes/admin/customer-groups/index.ts | 53 ++++++++---- .../customer-groups/list-customer-groups.ts | 49 ++--------- .../customer-groups/update-customer-group.ts | 3 +- packages/medusa/src/types/global.ts | 1 + packages/medusa/src/utils/get-query-config.ts | 2 +- 21 files changed, 200 insertions(+), 206 deletions(-) diff --git a/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap b/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap index 10891cc07a..f544c24ece 100644 --- a/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap +++ b/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap @@ -189,20 +189,20 @@ Object { "collections": Array [ Object { "created_at": Any, - "handle": "test-collection", - "id": "test-collection", + "handle": "test-collection2", + "id": "test-collection2", "products": Array [ Object { - "collection_id": "test-collection", + "collection_id": "test-collection2", "created_at": Any, "deleted_at": null, "description": "test-product-description", "discountable": true, "external_id": null, - "handle": "test-product", + "handle": "test-product_filtering_2", "height": null, "hs_code": null, - "id": "test-product", + "id": "test-product_filtering_2", "is_giftcard": false, "length": null, "material": null, @@ -210,44 +210,17 @@ Object { "mid_code": null, "origin_country": null, "profile_id": StringMatching /\\^sp_\\*/, - "status": "draft", + "status": "published", "subtitle": null, "thumbnail": null, - "title": "Test product", - "type_id": "test-type", - "updated_at": Any, - "weight": null, - "width": null, - }, - Object { - "collection_id": "test-collection", - "created_at": Any, - "deleted_at": null, - "description": "test-product-description1", - "discountable": true, - "external_id": null, - "handle": "test-product1", - "height": null, - "hs_code": null, - "id": "test-product1", - "is_giftcard": false, - "length": null, - "material": null, - "metadata": null, - "mid_code": null, - "origin_country": null, - "profile_id": StringMatching /\\^sp_\\*/, - "status": "draft", - "subtitle": null, - "thumbnail": null, - "title": "Test product1", + "title": "Test product filtering 2", "type_id": "test-type", "updated_at": Any, "weight": null, "width": null, }, ], - "title": "Test collection", + "title": "Test collection 2", "updated_at": Any, }, Object { @@ -315,20 +288,20 @@ Object { }, Object { "created_at": Any, - "handle": "test-collection2", - "id": "test-collection2", + "handle": "test-collection", + "id": "test-collection", "products": Array [ Object { - "collection_id": "test-collection2", + "collection_id": "test-collection", "created_at": Any, "deleted_at": null, "description": "test-product-description", "discountable": true, "external_id": null, - "handle": "test-product_filtering_2", + "handle": "test-product", "height": null, "hs_code": null, - "id": "test-product_filtering_2", + "id": "test-product", "is_giftcard": false, "length": null, "material": null, @@ -336,17 +309,44 @@ Object { "mid_code": null, "origin_country": null, "profile_id": StringMatching /\\^sp_\\*/, - "status": "published", + "status": "draft", "subtitle": null, "thumbnail": null, - "title": "Test product filtering 2", + "title": "Test product", + "type_id": "test-type", + "updated_at": Any, + "weight": null, + "width": null, + }, + Object { + "collection_id": "test-collection", + "created_at": Any, + "deleted_at": null, + "description": "test-product-description1", + "discountable": true, + "external_id": null, + "handle": "test-product1", + "height": null, + "hs_code": null, + "id": "test-product1", + "is_giftcard": false, + "length": null, + "material": null, + "metadata": null, + "mid_code": null, + "origin_country": null, + "profile_id": StringMatching /\\^sp_\\*/, + "status": "draft", + "subtitle": null, + "thumbnail": null, + "title": "Test product1", "type_id": "test-type", "updated_at": Any, "weight": null, "width": null, }, ], - "title": "Test collection 2", + "title": "Test collection", "updated_at": Any, }, ], diff --git a/integration-tests/api/__tests__/admin/colllections.js b/integration-tests/api/__tests__/admin/colllections.js index dd6ede0bb4..4f47d1efa2 100644 --- a/integration-tests/api/__tests__/admin/colllections.js +++ b/integration-tests/api/__tests__/admin/colllections.js @@ -176,20 +176,14 @@ describe("/admin/collections", () => { expect(response.data).toMatchSnapshot({ collections: [ { - id: "test-collection", - handle: "test-collection", - title: "Test collection", + id: "test-collection2", + handle: "test-collection2", + title: "Test collection 2", created_at: expect.any(String), updated_at: expect.any(String), products: [ { - collection_id: "test-collection", - created_at: expect.any(String), - updated_at: expect.any(String), - profile_id: expect.stringMatching(/^sp_*/), - }, - { - collection_id: "test-collection", + collection_id: "test-collection2", created_at: expect.any(String), updated_at: expect.any(String), profile_id: expect.stringMatching(/^sp_*/), @@ -218,14 +212,20 @@ describe("/admin/collections", () => { ], }, { - id: "test-collection2", - handle: "test-collection2", - title: "Test collection 2", + id: "test-collection", + handle: "test-collection", + title: "Test collection", created_at: expect.any(String), updated_at: expect.any(String), products: [ { - collection_id: "test-collection2", + collection_id: "test-collection", + created_at: expect.any(String), + updated_at: expect.any(String), + profile_id: expect.stringMatching(/^sp_*/), + }, + { + collection_id: "test-collection", created_at: expect.any(String), updated_at: expect.any(String), profile_id: expect.stringMatching(/^sp_*/), diff --git a/packages/medusa/src/api/routes/admin/collections/add-products.ts b/packages/medusa/src/api/routes/admin/collections/add-products.ts index 15f8171f5b..65eedaf59c 100644 --- a/packages/medusa/src/api/routes/admin/collections/add-products.ts +++ b/packages/medusa/src/api/routes/admin/collections/add-products.ts @@ -1,6 +1,7 @@ import { ArrayNotEmpty, IsString } from "class-validator" import ProductCollectionService from "../../../../services/product-collection" -import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" + /** * @oas [post] /collections/{id}/products/batch * operationId: "PostProductsToCollection" @@ -28,10 +29,9 @@ import { validator } from "../../../../utils/validator" * "200": * description: OK */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params - - const validated = await validator(AdminPostProductsToCollectionReq, req.body) + const { validatedBody } = req as { validatedBody: AdminPostProductsToCollectionReq } const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" @@ -39,7 +39,7 @@ export default async (req, res) => { const collection = await productCollectionService.addProducts( id, - validated.product_ids + validatedBody.product_ids ) res.status(200).json({ collection }) diff --git a/packages/medusa/src/api/routes/admin/collections/create-collection.ts b/packages/medusa/src/api/routes/admin/collections/create-collection.ts index 5cd23ed468..509cdd909c 100644 --- a/packages/medusa/src/api/routes/admin/collections/create-collection.ts +++ b/packages/medusa/src/api/routes/admin/collections/create-collection.ts @@ -1,6 +1,7 @@ import { IsNotEmpty, IsObject, IsOptional, IsString } from "class-validator" import ProductCollectionService from "../../../../services/product-collection" -import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" + /** * @oas [post] /collections * operationId: "PostCollections" @@ -35,14 +36,14 @@ import { validator } from "../../../../utils/validator" * collection: * $ref: "#/components/schemas/product_collection" */ -export default async (req, res) => { - const validated = await validator(AdminPostCollectionsReq, req.body) +export default async (req: Request, res: Response) => { + const { validatedBody } = req const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" ) - const created = await productCollectionService.create(validated) + const created = await productCollectionService.create(validatedBody) const collection = await productCollectionService.retrieve(created.id) res.status(200).json({ collection }) diff --git a/packages/medusa/src/api/routes/admin/collections/delete-collection.ts b/packages/medusa/src/api/routes/admin/collections/delete-collection.ts index fc8fc6de50..60cbd7d412 100644 --- a/packages/medusa/src/api/routes/admin/collections/delete-collection.ts +++ b/packages/medusa/src/api/routes/admin/collections/delete-collection.ts @@ -1,4 +1,5 @@ import ProductCollectionService from "../../../../services/product-collection" +import { Request, Response } from "express" /** * @oas [delete] /collections/{id} @@ -26,7 +27,7 @@ import ProductCollectionService from "../../../../services/product-collection" * deleted: * type: boolean */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params const productCollectionService: ProductCollectionService = req.scope.resolve( diff --git a/packages/medusa/src/api/routes/admin/collections/get-collection.ts b/packages/medusa/src/api/routes/admin/collections/get-collection.ts index cb0a449c35..2b7c298808 100644 --- a/packages/medusa/src/api/routes/admin/collections/get-collection.ts +++ b/packages/medusa/src/api/routes/admin/collections/get-collection.ts @@ -1,5 +1,6 @@ import { defaultAdminCollectionsRelations } from "." import ProductCollectionService from "../../../../services/product-collection" +import { Request, Response } from "express" /** * @oas [get] /collections/{id} * operationId: "GetCollectionsCollection" @@ -20,17 +21,15 @@ import ProductCollectionService from "../../../../services/product-collection" * collection: * $ref: "#/components/schemas/product_collection" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params - const retrieveConfig = { - relations: defaultAdminCollectionsRelations, - } - const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" ) - const collection = await productCollectionService.retrieve(id, retrieveConfig) + const collection = await productCollectionService.retrieve(id, { + relations: defaultAdminCollectionsRelations, + }) res.status(200).json({ collection }) } diff --git a/packages/medusa/src/api/routes/admin/collections/index.ts b/packages/medusa/src/api/routes/admin/collections/index.ts index c1e33a1089..81bb985ff5 100644 --- a/packages/medusa/src/api/routes/admin/collections/index.ts +++ b/packages/medusa/src/api/routes/admin/collections/index.ts @@ -2,27 +2,58 @@ import { Router } from "express" import "reflect-metadata" import { ProductCollection } from "../../../.." import { DeleteResponse, PaginatedResponse } from "../../../../types/common" -import middlewares from "../../../middlewares" - -const route = Router() +import middlewares, { transformBody, transformQuery } from "../../../middlewares" +import { AdminGetCollectionsParams } from "./list-collections" +import { AdminPostCollectionsReq } from "./create-collection" +import { AdminPostCollectionsCollectionReq } from "./update-collection" +import { AdminPostProductsToCollectionReq } from "./add-products" +import { AdminDeleteProductsFromCollectionReq } from "./remove-products" export default (app) => { + const route = Router() app.use("/collections", route) - route.post("/", middlewares.wrap(require("./create-collection").default)) - route.post("/:id", middlewares.wrap(require("./update-collection").default)) - - route.delete("/:id", middlewares.wrap(require("./delete-collection").default)) - - route.get("/:id", middlewares.wrap(require("./get-collection").default)) - route.get("/", middlewares.wrap(require("./list-collections").default)) - route.post( - "/:id/products/batch", + "/", + transformBody(AdminPostCollectionsReq), + middlewares.wrap(require("./create-collection").default) + ) + route.get( + "/", + transformQuery( + AdminGetCollectionsParams, + { + defaultRelations: defaultAdminCollectionsRelations, + defaultFields: defaultAdminCollectionsFields, + isList: true, + } + ), + middlewares.wrap(require("./list-collections").default) + ) + + const collectionRouter = Router({ mergeParams: true }) + route.use("/:id", collectionRouter) + collectionRouter.post( + "/", + transformBody(AdminPostCollectionsCollectionReq), + middlewares.wrap(require("./update-collection").default) + ) + collectionRouter.get( + "/", + middlewares.wrap(require("./get-collection").default) + ) + collectionRouter.delete( + "/", + middlewares.wrap(require("./delete-collection").default) + ) + collectionRouter.post( + "/products/batch", + transformBody(AdminPostProductsToCollectionReq), middlewares.wrap(require("./add-products").default) ) - route.delete( - "/:id/products/batch", + collectionRouter.delete( + "/products/batch", + transformBody(AdminDeleteProductsFromCollectionReq), middlewares.wrap(require("./remove-products").default) ) diff --git a/packages/medusa/src/api/routes/admin/collections/list-collections.ts b/packages/medusa/src/api/routes/admin/collections/list-collections.ts index a9dc443d03..67550b2527 100644 --- a/packages/medusa/src/api/routes/admin/collections/list-collections.ts +++ b/packages/medusa/src/api/routes/admin/collections/list-collections.ts @@ -1,13 +1,10 @@ import { Type } from "class-transformer" import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator" import _, { identity } from "lodash" -import { - defaultAdminCollectionsFields, - defaultAdminCollectionsRelations, -} from "." import ProductCollectionService from "../../../../services/product-collection" import { DateComparisonOperator } from "../../../../types/common" -import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" + /** * @oas [get] /collections * operationId: "GetCollections" @@ -34,21 +31,16 @@ import { validator } from "../../../../utils/validator" * collection: * $ref: "#/components/schemas/product_collection" */ -export default async (req, res) => { - const validated = await validator(AdminGetCollectionsParams, req.query) - +export default async (req: Request, res: Response) => { const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" ) - const listConfig = { - select: defaultAdminCollectionsFields, - relations: defaultAdminCollectionsRelations, - skip: validated.offset, - take: validated.limit, - } - - const filterableFields = _.omit(validated, ["limit", "offset"]) + const { + validatedQuery: { limit, offset }, + filterableFields, + listConfig + } = req const [collections, count] = await productCollectionService.listAndCount( _.pickBy(filterableFields, identity), @@ -58,8 +50,8 @@ export default async (req, res) => { res.status(200).json({ collections, count, - offset: validated.offset, - limit: validated.limit, + offset, + limit, }) } diff --git a/packages/medusa/src/api/routes/admin/collections/remove-products.ts b/packages/medusa/src/api/routes/admin/collections/remove-products.ts index 54b9d7f4af..862b7c77eb 100644 --- a/packages/medusa/src/api/routes/admin/collections/remove-products.ts +++ b/packages/medusa/src/api/routes/admin/collections/remove-products.ts @@ -1,6 +1,7 @@ import { ArrayNotEmpty, IsString } from "class-validator" import ProductCollectionService from "../../../../services/product-collection" -import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" + /** * @oas [delete] /collections/{id}/products/batch * operationId: "DeleteProductsFromCollection" @@ -28,24 +29,20 @@ import { validator } from "../../../../utils/validator" * "200": * description: OK */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params - - const validated = await validator( - AdminDeleteProductsFromCollectionReq, - req.body - ) + const { validatedBody } = req as { validatedBody: AdminDeleteProductsFromCollectionReq } const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" ) - await productCollectionService.removeProducts(id, validated.product_ids) + await productCollectionService.removeProducts(id, validatedBody.product_ids) res.json({ id, object: "product-collection", - removed_products: validated.product_ids, + removed_products: validatedBody.product_ids, }) } diff --git a/packages/medusa/src/api/routes/admin/collections/update-collection.ts b/packages/medusa/src/api/routes/admin/collections/update-collection.ts index 5adcdd46a3..072d8c22d2 100644 --- a/packages/medusa/src/api/routes/admin/collections/update-collection.ts +++ b/packages/medusa/src/api/routes/admin/collections/update-collection.ts @@ -1,6 +1,6 @@ import { IsObject, IsOptional, IsString } from "class-validator" import ProductCollectionService from "../../../../services/product-collection" -import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" /** * @oas [post] /collections/{id} * operationId: "PostCollectionsCollection" @@ -35,15 +35,15 @@ import { validator } from "../../../../utils/validator" * collection: * $ref: "#/components/schemas/product_collection" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params + const { validatedBody } = req - const validated = await validator(AdminPostCollectionsCollectionReq, req.body) const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" ) - const updated = await productCollectionService.update(id, validated) + const updated = await productCollectionService.update(id, validatedBody) const collection = await productCollectionService.retrieve(updated.id) res.status(200).json({ collection }) diff --git a/packages/medusa/src/api/routes/admin/customer-groups/add-customers-batch.ts b/packages/medusa/src/api/routes/admin/customer-groups/add-customers-batch.ts index 8778f7ac72..96aa4b5ead 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/add-customers-batch.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/add-customers-batch.ts @@ -3,6 +3,7 @@ import { ValidateNested } from "class-validator" import { CustomerGroupService } from "../../../../services" import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups" import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" /** * @oas [post] /customer-groups/{id}/customers/batch @@ -26,7 +27,7 @@ import { validator } from "../../../../utils/validator" * $ref: "#/components/schemas/customergroup" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params const validated = await validator( AdminPostCustomerGroupsGroupCustomersBatchReq, diff --git a/packages/medusa/src/api/routes/admin/customer-groups/create-customer-group.ts b/packages/medusa/src/api/routes/admin/customer-groups/create-customer-group.ts index 8be4b65270..633bd94e2a 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/create-customer-group.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/create-customer-group.ts @@ -1,6 +1,7 @@ import { IsObject, IsOptional, IsString } from "class-validator" import { CustomerGroupService } from "../../../../services" import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" /** * @oas [post] /customer-groups @@ -24,7 +25,7 @@ import { validator } from "../../../../utils/validator" * $ref: "#/components/schemas/customer_group" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const validated = await validator(AdminPostCustomerGroupsReq, req.body) const customerGroupService: CustomerGroupService = req.scope.resolve( diff --git a/packages/medusa/src/api/routes/admin/customer-groups/delete-customer-group.ts b/packages/medusa/src/api/routes/admin/customer-groups/delete-customer-group.ts index cc6f580ebb..4c2ba7f2ef 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/delete-customer-group.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/delete-customer-group.ts @@ -1,4 +1,5 @@ import { CustomerGroupService } from "../../../../services" +import { Request, Response } from "express" /** * @oas [delete] /customer-groups/{id} @@ -27,7 +28,7 @@ import { CustomerGroupService } from "../../../../services" * type: boolean */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params const customerGroupService: CustomerGroupService = req.scope.resolve( diff --git a/packages/medusa/src/api/routes/admin/customer-groups/delete-customers-batch.ts b/packages/medusa/src/api/routes/admin/customer-groups/delete-customers-batch.ts index 1ea51f6ca4..fbca982372 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/delete-customers-batch.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/delete-customers-batch.ts @@ -3,6 +3,7 @@ import { ValidateNested } from "class-validator" import { CustomerGroupService } from "../../../../services" import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups" import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" /** * @oas [delete] /customer-groups/{id}/customers/batch @@ -26,7 +27,7 @@ import { validator } from "../../../../utils/validator" * $ref: "#/components/schemas/customergroup" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params const validated = await validator( AdminDeleteCustomerGroupsGroupCustomerBatchReq, diff --git a/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group-customers.ts b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group-customers.ts index b8bbda49bb..265a81f490 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group-customers.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group-customers.ts @@ -1,4 +1,5 @@ import CustomerController from "../../../../controllers/customers" +import { Request, Response } from "express" /** * @oas [get] /customer-groups/{id}/customers @@ -18,7 +19,7 @@ import CustomerController from "../../../../controllers/customers" * customer: * $ref: "#/components/schemas/customer" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params req.query.groups = [id] diff --git a/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts index dd77de94e0..764b9b752b 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/get-customer-group.ts @@ -1,7 +1,6 @@ import { CustomerGroupService } from "../../../../services" import { FindParams } from "../../../../types/common" -import { validator } from "../../../../utils/validator" -import { defaultAdminCustomerGroupsRelations } from "." +import { Request, Response } from "express" /** * @oas [get] /customer-groups/{id} @@ -23,30 +22,17 @@ import { defaultAdminCustomerGroupsRelations } from "." * customer_group: * $ref: "#/components/schemas/customer_group" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params - const validated = await validator( - AdminGetCustomerGroupsGroupParams, - req.query - ) - const customerGroupService: CustomerGroupService = req.scope.resolve( "customerGroupService" ) - let expandFields: string[] = [] - if (validated.expand) { - expandFields = validated.expand.split(",") - } - - const findConfig = { - relations: expandFields.length - ? expandFields - : defaultAdminCustomerGroupsRelations, - } - - const customerGroup = await customerGroupService.retrieve(id, findConfig) + const customerGroup = await customerGroupService.retrieve( + id, + req.retrieveConfig + ) res.json({ customer_group: customerGroup }) } diff --git a/packages/medusa/src/api/routes/admin/customer-groups/index.ts b/packages/medusa/src/api/routes/admin/customer-groups/index.ts index f6418e78f3..a01bfd42b0 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/index.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/index.ts @@ -1,38 +1,53 @@ import { Router } from "express" import { CustomerGroup } from "../../../.." import { DeleteResponse, PaginatedResponse } from "../../../../types/common" -import middlewares from "../../../middlewares" +import middlewares, { transformQuery } from "../../../middlewares" +import { AdminGetCustomerGroupsGroupParams } from "./get-customer-group" +import { AdminGetCustomerGroupsParams } from "./list-customer-groups" 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( - "/:id/customers/batch", - middlewares.wrap(require("./add-customers-batch").default) - ) - route.delete( - "/:id/customers/batch", - middlewares.wrap(require("./delete-customers-batch").default) + route.get( + "/", + transformQuery(AdminGetCustomerGroupsParams, { + defaultRelations: defaultAdminCustomerGroupsRelations, + isList: true, + }), + middlewares.wrap(require("./list-customer-groups").default) ) - route.delete( - "/:id", + const customerGroupRouter = Router({ mergeParams: true }) + route.use("/:id", customerGroupRouter) + customerGroupRouter.get( + "/", + transformQuery(AdminGetCustomerGroupsGroupParams, { + defaultRelations: defaultAdminCustomerGroupsRelations, + }), + middlewares.wrap(require("./get-customer-group").default) + ) + customerGroupRouter.delete( + "/", middlewares.wrap(require("./delete-customer-group").default) ) - - route.get( - "/:id/customers", + customerGroupRouter.post( + "/", + middlewares.wrap(require("./update-customer-group").default) + ) + customerGroupRouter.get( + "/customers", middlewares.wrap(require("./get-customer-group-customers").default) ) - - route.post( - "/:id", - middlewares.wrap(require("./update-customer-group").default) + customerGroupRouter.post( + "/customers/batch", + middlewares.wrap(require("./add-customers-batch").default) + ) + customerGroupRouter.delete( + "/customers/batch", + middlewares.wrap(require("./delete-customers-batch").default) ) return app diff --git a/packages/medusa/src/api/routes/admin/customer-groups/list-customer-groups.ts b/packages/medusa/src/api/routes/admin/customer-groups/list-customer-groups.ts index 1685f3c2df..130460524e 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/list-customer-groups.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/list-customer-groups.ts @@ -1,12 +1,8 @@ import { Type } from "class-transformer" import { IsNumber, IsOptional, IsString } from "class-validator" -import omit from "lodash/omit" -import { defaultAdminCustomerGroupsRelations } from "." -import { CustomerGroup } from "../../../../models/customer-group" import { CustomerGroupService } from "../../../../services" -import { FindConfig } from "../../../../types/common" import { FilterableCustomerGroupProps } from "../../../../types/customer-groups" -import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" /** * @oas [get] /customer-groups @@ -37,53 +33,22 @@ import { validator } from "../../../../utils/validator" * customerGroup: * $ref: "#/components/schemas/customer_group" */ -export default async (req, res) => { - const validated = await validator(AdminGetCustomerGroupsParams, req.query) - +export default async (req: Request, res: Response) => { const customerGroupService: CustomerGroupService = req.scope.resolve( "customerGroupService" ) - let expandFields: string[] = [] - if (validated.expand) { - expandFields = validated.expand.split(",") - } - - const listConfig: FindConfig = { - 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 + req.filterableFields, + req.listConfig ) + const { limit, offset } = req.validatedQuery res.json({ count, customer_groups: data, - offset: validated.offset, - limit: validated.limit, + offset, + limit, }) } diff --git a/packages/medusa/src/api/routes/admin/customer-groups/update-customer-group.ts b/packages/medusa/src/api/routes/admin/customer-groups/update-customer-group.ts index c89497f630..1d6a5e5889 100644 --- a/packages/medusa/src/api/routes/admin/customer-groups/update-customer-group.ts +++ b/packages/medusa/src/api/routes/admin/customer-groups/update-customer-group.ts @@ -4,6 +4,7 @@ import { defaultAdminCustomerGroupsRelations } from "." import { CustomerGroupService } from "../../../../services" import { FindParams } from "../../../../types/common" import { validator } from "../../../../utils/validator" +import { Request, Response } from "express" /** * @oas [post] /customer-groups/{id} @@ -28,7 +29,7 @@ import { validator } from "../../../../utils/validator" * $ref: "#/components/schemas/customer_group" */ -export default async (req, res) => { +export default async (req: Request, res: Response) => { const { id } = req.params const validatedBody = await validator( diff --git a/packages/medusa/src/types/global.ts b/packages/medusa/src/types/global.ts index 50436d4ea3..bb91cf9921 100644 --- a/packages/medusa/src/types/global.ts +++ b/packages/medusa/src/types/global.ts @@ -19,6 +19,7 @@ declare global { } } + export type ClassConstructor = { new (...args: unknown[]): T } diff --git a/packages/medusa/src/utils/get-query-config.ts b/packages/medusa/src/utils/get-query-config.ts index 16398c1a8e..c876a05245 100644 --- a/packages/medusa/src/utils/get-query-config.ts +++ b/packages/medusa/src/utils/get-query-config.ts @@ -136,7 +136,7 @@ export function prepareRetrieveQuery< expandRelations = expand.split(",") } - let expandFields: (keyof TEntity)[] = [] + let expandFields: (keyof TEntity)[] | undefined = undefined if (fields) { expandFields = fields.split(",") as (keyof TEntity)[] }