feat(customer): add customer group customer management (#6276)
**What** - GET /admin/customer-groups/:id/customers - POST /admin/customer-groups/:id/customers/batch - POST /admin/customer-groups/:id/customers/remove Workflows
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { createCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
|
||||
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
const { customer_ids } =
|
||||
req.validatedBody as AdminPostCustomerGroupsGroupCustomersBatchReq
|
||||
|
||||
const createCustomers = createCustomerGroupCustomersWorkflow(req.scope)
|
||||
|
||||
const { result, errors } = await createCustomers.run({
|
||||
input: {
|
||||
groupCustomers: customer_ids.map((c) => ({
|
||||
customer_id: c.id,
|
||||
customer_group_id: id,
|
||||
})),
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ customer_group_customers: result })
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { deleteCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
|
||||
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
const { customer_ids } =
|
||||
req.validatedBody as AdminPostCustomerGroupsGroupCustomersBatchReq
|
||||
|
||||
const deleteCustomers = deleteCustomerGroupCustomersWorkflow(req.scope)
|
||||
|
||||
const { errors } = await deleteCustomers.run({
|
||||
input: {
|
||||
groupCustomers: customer_ids.map((c) => ({
|
||||
customer_id: c.id,
|
||||
customer_group_id: id,
|
||||
})),
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
object: "customer_group_customers",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id } = req.params
|
||||
|
||||
const service = req.scope.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const [customers, count] = await service.listAndCount(
|
||||
{ ...req.filterableFields, groups: id },
|
||||
req.listConfig
|
||||
)
|
||||
|
||||
const { offset, limit } = req.validatedQuery
|
||||
|
||||
res.json({
|
||||
count,
|
||||
customers,
|
||||
offset,
|
||||
limit,
|
||||
})
|
||||
}
|
||||
@@ -1,24 +1,18 @@
|
||||
import { MedusaV2Flag } from "@medusajs/utils"
|
||||
|
||||
import {
|
||||
isFeatureFlagEnabled,
|
||||
transformBody,
|
||||
transformQuery,
|
||||
} from "../../../api/middlewares"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import { listTransformQueryConfig as customersListTransformQueryConfig } from "../customers/query-config"
|
||||
import {
|
||||
AdminGetCustomerGroupsParams,
|
||||
AdminGetCustomerGroupsGroupParams,
|
||||
AdminPostCustomerGroupsReq,
|
||||
AdminPostCustomerGroupsGroupReq,
|
||||
AdminGetCustomerGroupsGroupCustomersParams,
|
||||
AdminPostCustomerGroupsGroupCustomersBatchReq,
|
||||
AdminDeleteCustomerGroupsGroupCustomersBatchReq,
|
||||
} from "./validators"
|
||||
|
||||
export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
matcher: "/admin/customer-groups*",
|
||||
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/customer-groups",
|
||||
@@ -49,4 +43,26 @@ export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/admin/customer-groups/:id",
|
||||
middlewares: [transformBody(AdminPostCustomerGroupsGroupReq)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/customer-groups/:id/customers",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetCustomerGroupsGroupCustomersParams,
|
||||
customersListTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/customer-groups/:id/customers/batch",
|
||||
middlewares: [transformBody(AdminPostCustomerGroupsGroupCustomersBatchReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/customer-groups/:id/customers/remove",
|
||||
middlewares: [
|
||||
transformBody(AdminDeleteCustomerGroupsGroupCustomersBatchReq),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import {
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
import { IsType } from "../../../utils"
|
||||
|
||||
export class AdminGetCustomerGroupsGroupParams extends FindParams {}
|
||||
|
||||
@@ -112,3 +113,62 @@ export class AdminPostCustomerGroupsGroupReq {
|
||||
@IsOptional()
|
||||
name?: string
|
||||
}
|
||||
|
||||
export class AdminGetCustomerGroupsGroupCustomersParams extends extendedFindParamsMixin(
|
||||
{
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
}
|
||||
) {
|
||||
@IsOptional()
|
||||
@IsString({ each: true })
|
||||
id?: string | string[]
|
||||
|
||||
@IsOptional()
|
||||
@IsType([String, [String], OperatorMapValidator])
|
||||
email?: string | string[] | OperatorMap<string>
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ each: true })
|
||||
company_name?: string | string[] | OperatorMap<string> | null
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ each: true })
|
||||
first_name?: string | string[] | OperatorMap<string> | null
|
||||
|
||||
@IsOptional()
|
||||
@IsType([String, [String], OperatorMapValidator])
|
||||
@Transform(({ value }) => (value === "null" ? null : value))
|
||||
last_name?: string | string[] | OperatorMap<string> | null
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ each: true })
|
||||
created_by?: string | string[] | null
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
created_at?: OperatorMap<string>
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
updated_at?: OperatorMap<string>
|
||||
}
|
||||
|
||||
class CustomerGroupsBatchCustomer {
|
||||
@IsString()
|
||||
id: string
|
||||
}
|
||||
|
||||
export class AdminDeleteCustomerGroupsGroupCustomersBatchReq {
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => CustomerGroupsBatchCustomer)
|
||||
customer_ids: CustomerGroupsBatchCustomer[]
|
||||
}
|
||||
|
||||
export class AdminPostCustomerGroupsGroupCustomersBatchReq {
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => CustomerGroupsBatchCustomer)
|
||||
customer_ids: CustomerGroupsBatchCustomer[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user