feat: list customer groups (#1099)
This commit is contained in:
@@ -3,7 +3,7 @@ import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import _, { identity } from "lodash"
|
||||
import {
|
||||
defaultAdminCollectionsFields,
|
||||
defaultAdminCollectionsRelations
|
||||
defaultAdminCollectionsRelations,
|
||||
} from "."
|
||||
import ProductCollectionService from "../../../../services/product-collection"
|
||||
import { DateComparisonOperator } from "../../../../types/common"
|
||||
|
||||
@@ -4,7 +4,7 @@ import { validator } from "../../../../utils/validator"
|
||||
import { defaultAdminCustomerGroupsRelations } from "."
|
||||
|
||||
/**
|
||||
* @oas [get] /customer-group/{id}
|
||||
* @oas [get] /customer-groups/{id}
|
||||
* operationId: "GetCustomerGroupsGroup"
|
||||
* summary: "Retrieve a CustomerGroup"
|
||||
* description: "Retrieves a Customer Group."
|
||||
|
||||
@@ -8,6 +8,7 @@ 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(
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import { Type } from "class-transformer"
|
||||
import omit from "lodash/omit"
|
||||
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { CustomerGroupService } from "../../../../services"
|
||||
import { CustomerGroup } from "../../../../models/customer-group"
|
||||
import { FindConfig } from "../../../../types/common"
|
||||
import { defaultAdminCustomerGroupsRelations } from "."
|
||||
import { FilterableCustomerGroupProps } from "../../../../types/customer-groups"
|
||||
|
||||
/**
|
||||
* @oas [get] /customer-groups
|
||||
* operationId: "GetCustomerGroups"
|
||||
* summary: "Retrieve a list of customer groups"
|
||||
* description: "Retrieve a list of customer groups."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (query) q {string} Query used for searching user group names.
|
||||
* - (query) offset {string} How many groups to skip in the result.
|
||||
* - (query) id {string} Ids of the groups to search for.
|
||||
* - (query) order {string} to retrieve customer groups in.
|
||||
* - (query) created_at {DateComparisonOperator} Date comparison for when resulting customer group was created, i.e. less than, greater than etc.
|
||||
* - (query) updated_at {DateComparisonOperator} Date comparison for when resulting ustomer group was updated, i.e. less than, greater than etc.
|
||||
* - (query) offset {string} How many customer groups to skip in the result.
|
||||
* - (query) limit {string} Limit the number of customer groups returned.
|
||||
* - (query) expand {string} (Comma separated) Which fields should be expanded in each customer groups of the result.
|
||||
|
||||
* tags:
|
||||
* - CustomerGroup
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* customerGroup:
|
||||
* $ref: "#/components/schemas/customer_group"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const validated = await validator(AdminGetCustomerGroupsParams, req.query)
|
||||
|
||||
const customerGroupService: CustomerGroupService = req.scope.resolve(
|
||||
"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(
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
|
||||
res.json({
|
||||
count,
|
||||
customer_groups: data,
|
||||
offset: validated.offset,
|
||||
limit: validated.limit,
|
||||
})
|
||||
}
|
||||
|
||||
export class AdminGetCustomerGroupsParams extends FilterableCustomerGroupProps {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
order?: string
|
||||
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
offset?: number = 0
|
||||
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
limit?: number = 10
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
expand?: string
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { DeepPartial, EntityManager } from "typeorm"
|
||||
import { DeepPartial, EntityManager, ILike, SelectQueryBuilder } from "typeorm"
|
||||
import { CustomerService } from "."
|
||||
import { CustomerGroup } from ".."
|
||||
import { CustomerGroupRepository } from "../repositories/customer-group"
|
||||
import { FindConfig } from "../types/common"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
import {
|
||||
CustomerGroupUpdate,
|
||||
FilterableCustomerGroupProps,
|
||||
} from "../types/customer-groups"
|
||||
import { formatException } from "../utils/exception-formatter"
|
||||
|
||||
type CustomerGroupConstructorProps = {
|
||||
manager: EntityManager
|
||||
@@ -226,6 +226,41 @@ class CustomerGroupService extends BaseService {
|
||||
return await cgRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of customer groups and total count of records that match the query.
|
||||
*
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config to be used for find
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async listAndCount(
|
||||
selector: FilterableCustomerGroupProps = {},
|
||||
config: FindConfig<CustomerGroup>
|
||||
): Promise<[CustomerGroup[], number]> {
|
||||
const cgRepo: CustomerGroupRepository = this.manager_.getCustomRepository(
|
||||
this.customerGroupRepository_
|
||||
)
|
||||
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query = this.buildQuery_(selector, config)
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
|
||||
delete where.name
|
||||
|
||||
query.where = (qb: SelectQueryBuilder<CustomerGroup>): void => {
|
||||
qb.where(where).andWhere([{ name: ILike(`%${q}%`) }])
|
||||
}
|
||||
}
|
||||
return await cgRepo.findAndCount(query)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove list of customers from a customergroup
|
||||
*
|
||||
|
||||
@@ -1,12 +1,33 @@
|
||||
import { IsString, ValidateNested } from "class-validator"
|
||||
import { Type } from "class-transformer"
|
||||
import { IsArray, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import { IsType } from "../utils/validators/is-type"
|
||||
|
||||
import { StringComparisonOperator } from "./common"
|
||||
import { DateComparisonOperator, StringComparisonOperator } from "./common"
|
||||
|
||||
export class FilterableCustomerGroupProps {
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@IsType([String, [String], StringComparisonOperator])
|
||||
id?: string | string[] | StringComparisonOperator
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
q?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
name?: string[]
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
updated_at?: DateComparisonOperator
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
created_at?: DateComparisonOperator
|
||||
}
|
||||
|
||||
export class CustomerGroupsBatchCustomer {
|
||||
|
||||
Reference in New Issue
Block a user