feat(customer): admin CRUD endpoints (#6232)

**What**

- GET /customers/:id
- POST /customers/:id
- DELETE /customers/:id
- POST /customers

Including workflows for each.
This commit is contained in:
Sebastian Rindom
2024-01-30 11:43:30 +00:00
committed by GitHub
parent 328eb85a8b
commit 18ff739a94
21 changed files with 503 additions and 25 deletions
@@ -0,0 +1,60 @@
import {
updateCustomersWorkflow,
deleteCustomersWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CustomerUpdatableFields,
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 customer = await customerModuleService.retrieve(req.params.id, {
select: req.retrieveConfig.select,
relations: req.retrieveConfig.relations,
})
res.status(200).json({ customer })
}
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const updateCustomers = updateCustomersWorkflow(req.scope)
const { result, errors } = await updateCustomers.run({
input: {
selector: { id: req.params.id },
update: req.validatedBody as CustomerUpdatableFields,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ customer: result[0] })
}
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
const id = req.params.id
const deleteCustomers = deleteCustomersWorkflow(req.scope)
const { errors } = await deleteCustomers.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "customer",
deleted: true,
})
}
@@ -1,23 +1,14 @@
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 {
AdminGetCustomersParams,
AdminGetCustomersCustomerParams,
AdminPostCustomersReq,
AdminPostCustomersCustomerReq,
} from "./validators"
export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
{
matcher: "/admin/customers*",
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)],
},
{
method: ["GET"],
matcher: "/admin/customers",
@@ -28,6 +19,11 @@ export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["POST"],
matcher: "/admin/customers",
middlewares: [transformBody(AdminPostCustomersReq)],
},
{
method: ["GET"],
matcher: "/admin/customers/:id",
@@ -1,5 +1,6 @@
import { createCustomersWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import { CreateCustomerDTO, ICustomerModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
@@ -39,3 +40,24 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
limit,
})
}
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const createCustomers = createCustomersWorkflow(req.scope)
const customersData = [
{
...(req.validatedBody as CreateCustomerDTO),
created_by: req.user!.id,
},
]
const { result, errors } = await createCustomers.run({
input: { customersData },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ customer: result[0] })
}
@@ -121,6 +121,11 @@ export class AdminPostCustomersReq {
@IsString()
@IsOptional()
email?: string
@IsNotEmpty()
@IsString()
@IsOptional()
phone?: string
}
export class AdminPostCustomersCustomerReq {
@@ -143,4 +148,9 @@ export class AdminPostCustomersCustomerReq {
@IsString()
@IsOptional()
email?: string
@IsNotEmpty()
@IsString()
@IsOptional()
phone?: string
}