feat(medusa): Apply query transformers to Collection and CustomerGroups (#1667)

This commit is contained in:
Adrien de Peretti
2022-07-02 13:01:52 +02:00
committed by GitHub
parent fee0f88a62
commit e53c06eab8
21 changed files with 200 additions and 206 deletions
@@ -189,20 +189,20 @@ Object {
"collections": Array [ "collections": Array [
Object { Object {
"created_at": Any<String>, "created_at": Any<String>,
"handle": "test-collection", "handle": "test-collection2",
"id": "test-collection", "id": "test-collection2",
"products": Array [ "products": Array [
Object { Object {
"collection_id": "test-collection", "collection_id": "test-collection2",
"created_at": Any<String>, "created_at": Any<String>,
"deleted_at": null, "deleted_at": null,
"description": "test-product-description", "description": "test-product-description",
"discountable": true, "discountable": true,
"external_id": null, "external_id": null,
"handle": "test-product", "handle": "test-product_filtering_2",
"height": null, "height": null,
"hs_code": null, "hs_code": null,
"id": "test-product", "id": "test-product_filtering_2",
"is_giftcard": false, "is_giftcard": false,
"length": null, "length": null,
"material": null, "material": null,
@@ -210,44 +210,17 @@ Object {
"mid_code": null, "mid_code": null,
"origin_country": null, "origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/, "profile_id": StringMatching /\\^sp_\\*/,
"status": "draft", "status": "published",
"subtitle": null, "subtitle": null,
"thumbnail": null, "thumbnail": null,
"title": "Test product", "title": "Test product filtering 2",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"collection_id": "test-collection",
"created_at": Any<String>,
"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", "type_id": "test-type",
"updated_at": Any<String>, "updated_at": Any<String>,
"weight": null, "weight": null,
"width": null, "width": null,
}, },
], ],
"title": "Test collection", "title": "Test collection 2",
"updated_at": Any<String>, "updated_at": Any<String>,
}, },
Object { Object {
@@ -315,20 +288,20 @@ Object {
}, },
Object { Object {
"created_at": Any<String>, "created_at": Any<String>,
"handle": "test-collection2", "handle": "test-collection",
"id": "test-collection2", "id": "test-collection",
"products": Array [ "products": Array [
Object { Object {
"collection_id": "test-collection2", "collection_id": "test-collection",
"created_at": Any<String>, "created_at": Any<String>,
"deleted_at": null, "deleted_at": null,
"description": "test-product-description", "description": "test-product-description",
"discountable": true, "discountable": true,
"external_id": null, "external_id": null,
"handle": "test-product_filtering_2", "handle": "test-product",
"height": null, "height": null,
"hs_code": null, "hs_code": null,
"id": "test-product_filtering_2", "id": "test-product",
"is_giftcard": false, "is_giftcard": false,
"length": null, "length": null,
"material": null, "material": null,
@@ -336,17 +309,44 @@ Object {
"mid_code": null, "mid_code": null,
"origin_country": null, "origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/, "profile_id": StringMatching /\\^sp_\\*/,
"status": "published", "status": "draft",
"subtitle": null, "subtitle": null,
"thumbnail": null, "thumbnail": null,
"title": "Test product filtering 2", "title": "Test product",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"collection_id": "test-collection",
"created_at": Any<String>,
"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", "type_id": "test-type",
"updated_at": Any<String>, "updated_at": Any<String>,
"weight": null, "weight": null,
"width": null, "width": null,
}, },
], ],
"title": "Test collection 2", "title": "Test collection",
"updated_at": Any<String>, "updated_at": Any<String>,
}, },
], ],
@@ -176,20 +176,14 @@ describe("/admin/collections", () => {
expect(response.data).toMatchSnapshot({ expect(response.data).toMatchSnapshot({
collections: [ collections: [
{ {
id: "test-collection", id: "test-collection2",
handle: "test-collection", handle: "test-collection2",
title: "Test collection", title: "Test collection 2",
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
products: [ products: [
{ {
collection_id: "test-collection", collection_id: "test-collection2",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
},
{
collection_id: "test-collection",
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/), profile_id: expect.stringMatching(/^sp_*/),
@@ -218,14 +212,20 @@ describe("/admin/collections", () => {
], ],
}, },
{ {
id: "test-collection2", id: "test-collection",
handle: "test-collection2", handle: "test-collection",
title: "Test collection 2", title: "Test collection",
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
products: [ 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), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/), profile_id: expect.stringMatching(/^sp_*/),
@@ -1,6 +1,7 @@
import { ArrayNotEmpty, IsString } from "class-validator" import { ArrayNotEmpty, IsString } from "class-validator"
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
/** /**
* @oas [post] /collections/{id}/products/batch * @oas [post] /collections/{id}/products/batch
* operationId: "PostProductsToCollection" * operationId: "PostProductsToCollection"
@@ -28,10 +29,9 @@ import { validator } from "../../../../utils/validator"
* "200": * "200":
* description: OK * description: OK
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const { validatedBody } = req as { validatedBody: AdminPostProductsToCollectionReq }
const validated = await validator(AdminPostProductsToCollectionReq, req.body)
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
"productCollectionService" "productCollectionService"
@@ -39,7 +39,7 @@ export default async (req, res) => {
const collection = await productCollectionService.addProducts( const collection = await productCollectionService.addProducts(
id, id,
validated.product_ids validatedBody.product_ids
) )
res.status(200).json({ collection }) res.status(200).json({ collection })
@@ -1,6 +1,7 @@
import { IsNotEmpty, IsObject, IsOptional, IsString } from "class-validator" import { IsNotEmpty, IsObject, IsOptional, IsString } from "class-validator"
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
/** /**
* @oas [post] /collections * @oas [post] /collections
* operationId: "PostCollections" * operationId: "PostCollections"
@@ -35,14 +36,14 @@ import { validator } from "../../../../utils/validator"
* collection: * collection:
* $ref: "#/components/schemas/product_collection" * $ref: "#/components/schemas/product_collection"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const validated = await validator(AdminPostCollectionsReq, req.body) const { validatedBody } = req
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
"productCollectionService" "productCollectionService"
) )
const created = await productCollectionService.create(validated) const created = await productCollectionService.create(validatedBody)
const collection = await productCollectionService.retrieve(created.id) const collection = await productCollectionService.retrieve(created.id)
res.status(200).json({ collection }) res.status(200).json({ collection })
@@ -1,4 +1,5 @@
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { Request, Response } from "express"
/** /**
* @oas [delete] /collections/{id} * @oas [delete] /collections/{id}
@@ -26,7 +27,7 @@ import ProductCollectionService from "../../../../services/product-collection"
* deleted: * deleted:
* type: boolean * type: boolean
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
@@ -1,5 +1,6 @@
import { defaultAdminCollectionsRelations } from "." import { defaultAdminCollectionsRelations } from "."
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { Request, Response } from "express"
/** /**
* @oas [get] /collections/{id} * @oas [get] /collections/{id}
* operationId: "GetCollectionsCollection" * operationId: "GetCollectionsCollection"
@@ -20,17 +21,15 @@ import ProductCollectionService from "../../../../services/product-collection"
* collection: * collection:
* $ref: "#/components/schemas/product_collection" * $ref: "#/components/schemas/product_collection"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const retrieveConfig = {
relations: defaultAdminCollectionsRelations,
}
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
"productCollectionService" "productCollectionService"
) )
const collection = await productCollectionService.retrieve(id, retrieveConfig) const collection = await productCollectionService.retrieve(id, {
relations: defaultAdminCollectionsRelations,
})
res.status(200).json({ collection }) res.status(200).json({ collection })
} }
@@ -2,27 +2,58 @@ import { Router } from "express"
import "reflect-metadata" import "reflect-metadata"
import { ProductCollection } from "../../../.." import { ProductCollection } from "../../../.."
import { DeleteResponse, PaginatedResponse } from "../../../../types/common" import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
import middlewares from "../../../middlewares" import middlewares, { transformBody, transformQuery } from "../../../middlewares"
import { AdminGetCollectionsParams } from "./list-collections"
const route = Router() import { AdminPostCollectionsReq } from "./create-collection"
import { AdminPostCollectionsCollectionReq } from "./update-collection"
import { AdminPostProductsToCollectionReq } from "./add-products"
import { AdminDeleteProductsFromCollectionReq } from "./remove-products"
export default (app) => { export default (app) => {
const route = Router()
app.use("/collections", route) 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( 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) middlewares.wrap(require("./add-products").default)
) )
route.delete( collectionRouter.delete(
"/:id/products/batch", "/products/batch",
transformBody(AdminDeleteProductsFromCollectionReq),
middlewares.wrap(require("./remove-products").default) middlewares.wrap(require("./remove-products").default)
) )
@@ -1,13 +1,10 @@
import { Type } from "class-transformer" import { Type } from "class-transformer"
import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator" import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator"
import _, { identity } from "lodash" import _, { identity } from "lodash"
import {
defaultAdminCollectionsFields,
defaultAdminCollectionsRelations,
} from "."
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { DateComparisonOperator } from "../../../../types/common" import { DateComparisonOperator } from "../../../../types/common"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
/** /**
* @oas [get] /collections * @oas [get] /collections
* operationId: "GetCollections" * operationId: "GetCollections"
@@ -34,21 +31,16 @@ import { validator } from "../../../../utils/validator"
* collection: * collection:
* $ref: "#/components/schemas/product_collection" * $ref: "#/components/schemas/product_collection"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const validated = await validator(AdminGetCollectionsParams, req.query)
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
"productCollectionService" "productCollectionService"
) )
const listConfig = { const {
select: defaultAdminCollectionsFields, validatedQuery: { limit, offset },
relations: defaultAdminCollectionsRelations, filterableFields,
skip: validated.offset, listConfig
take: validated.limit, } = req
}
const filterableFields = _.omit(validated, ["limit", "offset"])
const [collections, count] = await productCollectionService.listAndCount( const [collections, count] = await productCollectionService.listAndCount(
_.pickBy(filterableFields, identity), _.pickBy(filterableFields, identity),
@@ -58,8 +50,8 @@ export default async (req, res) => {
res.status(200).json({ res.status(200).json({
collections, collections,
count, count,
offset: validated.offset, offset,
limit: validated.limit, limit,
}) })
} }
@@ -1,6 +1,7 @@
import { ArrayNotEmpty, IsString } from "class-validator" import { ArrayNotEmpty, IsString } from "class-validator"
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
/** /**
* @oas [delete] /collections/{id}/products/batch * @oas [delete] /collections/{id}/products/batch
* operationId: "DeleteProductsFromCollection" * operationId: "DeleteProductsFromCollection"
@@ -28,24 +29,20 @@ import { validator } from "../../../../utils/validator"
* "200": * "200":
* description: OK * description: OK
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const { validatedBody } = req as { validatedBody: AdminDeleteProductsFromCollectionReq }
const validated = await validator(
AdminDeleteProductsFromCollectionReq,
req.body
)
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
"productCollectionService" "productCollectionService"
) )
await productCollectionService.removeProducts(id, validated.product_ids) await productCollectionService.removeProducts(id, validatedBody.product_ids)
res.json({ res.json({
id, id,
object: "product-collection", object: "product-collection",
removed_products: validated.product_ids, removed_products: validatedBody.product_ids,
}) })
} }
@@ -1,6 +1,6 @@
import { IsObject, IsOptional, IsString } from "class-validator" import { IsObject, IsOptional, IsString } from "class-validator"
import ProductCollectionService from "../../../../services/product-collection" import ProductCollectionService from "../../../../services/product-collection"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
/** /**
* @oas [post] /collections/{id} * @oas [post] /collections/{id}
* operationId: "PostCollectionsCollection" * operationId: "PostCollectionsCollection"
@@ -35,15 +35,15 @@ import { validator } from "../../../../utils/validator"
* collection: * collection:
* $ref: "#/components/schemas/product_collection" * $ref: "#/components/schemas/product_collection"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const { validatedBody } = req
const validated = await validator(AdminPostCollectionsCollectionReq, req.body)
const productCollectionService: ProductCollectionService = req.scope.resolve( const productCollectionService: ProductCollectionService = req.scope.resolve(
"productCollectionService" "productCollectionService"
) )
const updated = await productCollectionService.update(id, validated) const updated = await productCollectionService.update(id, validatedBody)
const collection = await productCollectionService.retrieve(updated.id) const collection = await productCollectionService.retrieve(updated.id)
res.status(200).json({ collection }) res.status(200).json({ collection })
@@ -3,6 +3,7 @@ import { ValidateNested } from "class-validator"
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups" import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups"
import { validator } from "../../../../utils/validator" import { validator } from "../../../../utils/validator"
import { Request, Response } from "express"
/** /**
* @oas [post] /customer-groups/{id}/customers/batch * @oas [post] /customer-groups/{id}/customers/batch
@@ -26,7 +27,7 @@ import { validator } from "../../../../utils/validator"
* $ref: "#/components/schemas/customergroup" * $ref: "#/components/schemas/customergroup"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const validated = await validator( const validated = await validator(
AdminPostCustomerGroupsGroupCustomersBatchReq, AdminPostCustomerGroupsGroupCustomersBatchReq,
@@ -1,6 +1,7 @@
import { IsObject, IsOptional, IsString } from "class-validator" import { IsObject, IsOptional, IsString } from "class-validator"
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { validator } from "../../../../utils/validator" import { validator } from "../../../../utils/validator"
import { Request, Response } from "express"
/** /**
* @oas [post] /customer-groups * @oas [post] /customer-groups
@@ -24,7 +25,7 @@ import { validator } from "../../../../utils/validator"
* $ref: "#/components/schemas/customer_group" * $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 validated = await validator(AdminPostCustomerGroupsReq, req.body)
const customerGroupService: CustomerGroupService = req.scope.resolve( const customerGroupService: CustomerGroupService = req.scope.resolve(
@@ -1,4 +1,5 @@
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { Request, Response } from "express"
/** /**
* @oas [delete] /customer-groups/{id} * @oas [delete] /customer-groups/{id}
@@ -27,7 +28,7 @@ import { CustomerGroupService } from "../../../../services"
* type: boolean * type: boolean
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const customerGroupService: CustomerGroupService = req.scope.resolve( const customerGroupService: CustomerGroupService = req.scope.resolve(
@@ -3,6 +3,7 @@ import { ValidateNested } from "class-validator"
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups" import { CustomerGroupsBatchCustomer } from "../../../../types/customer-groups"
import { validator } from "../../../../utils/validator" import { validator } from "../../../../utils/validator"
import { Request, Response } from "express"
/** /**
* @oas [delete] /customer-groups/{id}/customers/batch * @oas [delete] /customer-groups/{id}/customers/batch
@@ -26,7 +27,7 @@ import { validator } from "../../../../utils/validator"
* $ref: "#/components/schemas/customergroup" * $ref: "#/components/schemas/customergroup"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const validated = await validator( const validated = await validator(
AdminDeleteCustomerGroupsGroupCustomerBatchReq, AdminDeleteCustomerGroupsGroupCustomerBatchReq,
@@ -1,4 +1,5 @@
import CustomerController from "../../../../controllers/customers" import CustomerController from "../../../../controllers/customers"
import { Request, Response } from "express"
/** /**
* @oas [get] /customer-groups/{id}/customers * @oas [get] /customer-groups/{id}/customers
@@ -18,7 +19,7 @@ import CustomerController from "../../../../controllers/customers"
* customer: * customer:
* $ref: "#/components/schemas/customer" * $ref: "#/components/schemas/customer"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
req.query.groups = [id] req.query.groups = [id]
@@ -1,7 +1,6 @@
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { FindParams } from "../../../../types/common" import { FindParams } from "../../../../types/common"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
import { defaultAdminCustomerGroupsRelations } from "."
/** /**
* @oas [get] /customer-groups/{id} * @oas [get] /customer-groups/{id}
@@ -23,30 +22,17 @@ import { defaultAdminCustomerGroupsRelations } from "."
* customer_group: * customer_group:
* $ref: "#/components/schemas/customer_group" * $ref: "#/components/schemas/customer_group"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const validated = await validator(
AdminGetCustomerGroupsGroupParams,
req.query
)
const customerGroupService: CustomerGroupService = req.scope.resolve( const customerGroupService: CustomerGroupService = req.scope.resolve(
"customerGroupService" "customerGroupService"
) )
let expandFields: string[] = [] const customerGroup = await customerGroupService.retrieve(
if (validated.expand) { id,
expandFields = validated.expand.split(",") req.retrieveConfig
} )
const findConfig = {
relations: expandFields.length
? expandFields
: defaultAdminCustomerGroupsRelations,
}
const customerGroup = await customerGroupService.retrieve(id, findConfig)
res.json({ customer_group: customerGroup }) res.json({ customer_group: customerGroup })
} }
@@ -1,38 +1,53 @@
import { Router } from "express" import { Router } from "express"
import { CustomerGroup } from "../../../.." import { CustomerGroup } from "../../../.."
import { DeleteResponse, PaginatedResponse } from "../../../../types/common" 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() const route = Router()
export default (app) => { export default (app) => {
app.use("/customer-groups", route) 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("/", middlewares.wrap(require("./create-customer-group").default))
route.post( route.get(
"/:id/customers/batch", "/",
middlewares.wrap(require("./add-customers-batch").default) transformQuery(AdminGetCustomerGroupsParams, {
) defaultRelations: defaultAdminCustomerGroupsRelations,
route.delete( isList: true,
"/:id/customers/batch", }),
middlewares.wrap(require("./delete-customers-batch").default) middlewares.wrap(require("./list-customer-groups").default)
) )
route.delete( const customerGroupRouter = Router({ mergeParams: true })
"/:id", 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) middlewares.wrap(require("./delete-customer-group").default)
) )
customerGroupRouter.post(
route.get( "/",
"/:id/customers", middlewares.wrap(require("./update-customer-group").default)
)
customerGroupRouter.get(
"/customers",
middlewares.wrap(require("./get-customer-group-customers").default) middlewares.wrap(require("./get-customer-group-customers").default)
) )
customerGroupRouter.post(
route.post( "/customers/batch",
"/:id", middlewares.wrap(require("./add-customers-batch").default)
middlewares.wrap(require("./update-customer-group").default) )
customerGroupRouter.delete(
"/customers/batch",
middlewares.wrap(require("./delete-customers-batch").default)
) )
return app return app
@@ -1,12 +1,8 @@
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 omit from "lodash/omit"
import { defaultAdminCustomerGroupsRelations } from "."
import { CustomerGroup } from "../../../../models/customer-group"
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { FindConfig } from "../../../../types/common"
import { FilterableCustomerGroupProps } from "../../../../types/customer-groups" import { FilterableCustomerGroupProps } from "../../../../types/customer-groups"
import { validator } from "../../../../utils/validator" import { Request, Response } from "express"
/** /**
* @oas [get] /customer-groups * @oas [get] /customer-groups
@@ -37,53 +33,22 @@ import { validator } from "../../../../utils/validator"
* customerGroup: * customerGroup:
* $ref: "#/components/schemas/customer_group" * $ref: "#/components/schemas/customer_group"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const validated = await validator(AdminGetCustomerGroupsParams, req.query)
const customerGroupService: CustomerGroupService = req.scope.resolve( const customerGroupService: CustomerGroupService = req.scope.resolve(
"customerGroupService" "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( const [data, count] = await customerGroupService.listAndCount(
filterableFields, req.filterableFields,
listConfig req.listConfig
) )
const { limit, offset } = req.validatedQuery
res.json({ res.json({
count, count,
customer_groups: data, customer_groups: data,
offset: validated.offset, offset,
limit: validated.limit, limit,
}) })
} }
@@ -4,6 +4,7 @@ import { defaultAdminCustomerGroupsRelations } from "."
import { CustomerGroupService } from "../../../../services" import { CustomerGroupService } from "../../../../services"
import { FindParams } from "../../../../types/common" import { FindParams } from "../../../../types/common"
import { validator } from "../../../../utils/validator" import { validator } from "../../../../utils/validator"
import { Request, Response } from "express"
/** /**
* @oas [post] /customer-groups/{id} * @oas [post] /customer-groups/{id}
@@ -28,7 +29,7 @@ import { validator } from "../../../../utils/validator"
* $ref: "#/components/schemas/customer_group" * $ref: "#/components/schemas/customer_group"
*/ */
export default async (req, res) => { export default async (req: Request, res: Response) => {
const { id } = req.params const { id } = req.params
const validatedBody = await validator( const validatedBody = await validator(
+1
View File
@@ -19,6 +19,7 @@ declare global {
} }
} }
export type ClassConstructor<T> = { export type ClassConstructor<T> = {
new (...args: unknown[]): T new (...args: unknown[]): T
} }
@@ -136,7 +136,7 @@ export function prepareRetrieveQuery<
expandRelations = expand.split(",") expandRelations = expand.split(",")
} }
let expandFields: (keyof TEntity)[] = [] let expandFields: (keyof TEntity)[] | undefined = undefined
if (fields) { if (fields) {
expandFields = fields.split(",") as (keyof TEntity)[] expandFields = fields.split(",") as (keyof TEntity)[]
} }