feat(customer): add customer group management apis (#6233)
**What** ``` POST /admin/customer-groups POST /admin/customer-groups/:id GET /admin/customer-groups/:id DELETE /admin/customer-groups/:id ``` - Workflows
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
updateCustomerGroupsWorkflow,
|
||||
deleteCustomerGroupsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CustomerGroupUpdatableFields,
|
||||
ICustomerModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const customerModuleService = req.scope.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const group = await customerModuleService.retrieveCustomerGroup(
|
||||
req.params.id,
|
||||
{
|
||||
select: req.retrieveConfig.select,
|
||||
relations: req.retrieveConfig.relations,
|
||||
}
|
||||
)
|
||||
|
||||
res.status(200).json({ customer_group: group })
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const updateGroups = updateCustomerGroupsWorkflow(req.scope)
|
||||
const { result, errors } = await updateGroups.run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody as CustomerGroupUpdatableFields,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ customer_group: result[0] })
|
||||
}
|
||||
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const id = req.params.id
|
||||
const deleteCustomerGroups = deleteCustomerGroupsWorkflow(req.scope)
|
||||
|
||||
const { errors } = await deleteCustomerGroups.run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "customer_group",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createCustomerGroupsWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { CreateCustomerGroupDTO, ICustomerModuleService } from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
@@ -17,8 +18,29 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
|
||||
res.json({
|
||||
count,
|
||||
groups,
|
||||
customer_groups: groups,
|
||||
offset,
|
||||
limit,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const createGroups = createCustomerGroupsWorkflow(req.scope)
|
||||
const customersData = [
|
||||
{
|
||||
...(req.validatedBody as CreateCustomerGroupDTO),
|
||||
created_by: req.user!.id,
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createGroups.run({
|
||||
input: { customersData },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ customer_group: result[0] })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user