feat(customer): admin addresses (#6235)

**What**
- GET /admin/customers/:id/addresses
- POST /admin/customers/:id/addresses
- POST /admin/customers/:id/addresses/:address_id
- DELETE /admin/customers/:id/addresses/:address_id
This commit is contained in:
Sebastian Rindom
2024-01-31 10:55:07 +01:00
committed by GitHub
parent ca0e0631af
commit 36ec3ea3aa
20 changed files with 990 additions and 34 deletions
@@ -0,0 +1,60 @@
import {
updateCustomerAddressesWorkflow,
deleteCustomerAddressesWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { CustomerAddressDTO, 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 [address] = await customerModuleService.listAddresses(
{ id: req.params.address_id, customer_id: req.params.id },
{
select: req.retrieveConfig.select,
relations: req.retrieveConfig.relations,
}
)
res.status(200).json({ address })
}
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const updateAddresses = updateCustomerAddressesWorkflow(req.scope)
const { result, errors } = await updateAddresses.run({
input: {
selector: { id: req.params.address_id, customer_id: req.params.id },
update: req.validatedBody as Partial<CustomerAddressDTO>,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ address: result[0] })
}
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
const id = req.params.address_id
const deleteAddress = deleteCustomerAddressesWorkflow(req.scope)
const { errors } = await deleteAddress.run({
input: { ids: [id] },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
id,
object: "address",
deleted: true,
})
}
@@ -0,0 +1,51 @@
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CreateCustomerAddressDTO,
ICustomerModuleService,
} from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const customerId = req.params.id
const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const [addresses, count] = await customerModuleService.listAndCountAddresses(
{ ...req.filterableFields, customer_id: customerId },
req.listConfig
)
const { offset, limit } = req.validatedQuery
res.json({
count,
addresses,
offset,
limit,
})
}
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const customerId = req.params.id
const createAddresses = createCustomerAddressesWorkflow(req.scope)
const addresses = [
{
...(req.validatedBody as CreateCustomerAddressDTO),
customer_id: customerId,
},
]
const { result, errors } = await createAddresses.run({
input: { addresses },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ address: result[0] })
}
@@ -6,6 +6,9 @@ import {
AdminGetCustomersCustomerParams,
AdminPostCustomersReq,
AdminPostCustomersCustomerReq,
AdminPostCustomersCustomerAddressesReq,
AdminGetCustomersCustomerAddressesParams,
AdminPostCustomersCustomerAddressesAddressReq,
} from "./validators"
export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
@@ -39,4 +42,24 @@ export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/admin/customers/:id",
middlewares: [transformBody(AdminPostCustomersCustomerReq)],
},
{
method: ["POST"],
matcher: "/admin/customers/:id/addresses",
middlewares: [transformBody(AdminPostCustomersCustomerAddressesReq)],
},
{
method: ["POST"],
matcher: "/admin/customers/:id/addresses/:address_id",
middlewares: [transformBody(AdminPostCustomersCustomerAddressesAddressReq)],
},
{
method: ["GET"],
matcher: "/admin/customers/:id/addresses",
middlewares: [
transformQuery(
AdminGetCustomersCustomerAddressesParams,
QueryConfig.listAddressesTransformQueryConfig
),
],
},
]
@@ -1,10 +1,5 @@
export const defaultAdminCustomerRelations = []
export const allowedAdminCustomerRelations = [
"groups",
"default_shipping_address",
"default_billing_address",
"addresses",
]
export const allowedAdminCustomerRelations = ["groups", "addresses"]
export const defaultAdminCustomerFields = [
"id",
"company_name",
@@ -27,3 +22,35 @@ export const listTransformQueryConfig = {
...retrieveTransformQueryConfig,
isList: true,
}
export const defaultAdminCustomerAddressRelations = []
export const allowedAdminCustomerAddressRelations = ["customer"]
export const defaultAdminCustomerAddressFields = [
"id",
"company",
"customer_id",
"first_name",
"last_name",
"address_1",
"address_2",
"city",
"province",
"postal_code",
"country_code",
"phone",
"metadata",
"created_at",
"updated_at",
]
export const retrieveAddressTransformQueryConfig = {
defaultFields: defaultAdminCustomerAddressFields,
defaultRelations: defaultAdminCustomerAddressRelations,
allowedRelations: allowedAdminCustomerAddressRelations,
isList: false,
}
export const listAddressesTransformQueryConfig = {
...retrieveAddressTransformQueryConfig,
isList: true,
}
@@ -1,6 +1,7 @@
import { OperatorMap } from "@medusajs/types"
import { Transform, Type } from "class-transformer"
import {
IsBoolean,
IsNotEmpty,
IsOptional,
IsString,
@@ -29,14 +30,6 @@ export class AdminGetCustomersParams extends extendedFindParamsMixin({
@Type(() => FilterableCustomerGroupPropsValidator)
groups?: FilterableCustomerGroupPropsValidator | string | string[]
@IsOptional()
@IsString({ each: true })
default_billing_address_id?: string | string[] | null
@IsOptional()
@IsString({ each: true })
default_shipping_address_id?: string | string[] | null
@IsOptional()
@IsString({ each: true })
company_name?: string | string[] | OperatorMap<string> | null
@@ -154,3 +147,207 @@ export class AdminPostCustomersCustomerReq {
@IsOptional()
phone?: string
}
export class AdminPostCustomersCustomerAddressesReq {
@IsNotEmpty()
@IsString()
@IsOptional()
address_name?: string
@IsBoolean()
@IsOptional()
is_default_shipping?: boolean
@IsBoolean()
@IsOptional()
is_default_billing?: boolean
@IsNotEmpty()
@IsString()
@IsOptional()
company?: string
@IsNotEmpty()
@IsString()
@IsOptional()
first_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
last_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
address_1?: string
@IsNotEmpty()
@IsString()
@IsOptional()
address_2?: string
@IsNotEmpty()
@IsString()
@IsOptional()
city?: string
@IsNotEmpty()
@IsString()
@IsOptional()
country_code?: string
@IsNotEmpty()
@IsString()
@IsOptional()
province?: string
@IsNotEmpty()
@IsString()
@IsOptional()
postal_code?: string
@IsNotEmpty()
@IsString()
@IsOptional()
phone?: string
@IsNotEmpty()
@IsString()
@IsOptional()
metadata?: Record<string, unknown>
}
export class AdminPostCustomersCustomerAddressesAddressReq {
@IsNotEmpty()
@IsString()
@IsOptional()
address_name?: string
@IsBoolean()
@IsOptional()
is_default_shipping?: boolean
@IsBoolean()
@IsOptional()
is_default_billing?: boolean
@IsNotEmpty()
@IsString()
@IsOptional()
company?: string
@IsNotEmpty()
@IsString()
@IsOptional()
first_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
last_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
address_1?: string
@IsNotEmpty()
@IsString()
@IsOptional()
address_2?: string
@IsNotEmpty()
@IsString()
@IsOptional()
city?: string
@IsNotEmpty()
@IsString()
@IsOptional()
country_code?: string
@IsNotEmpty()
@IsString()
@IsOptional()
province?: string
@IsNotEmpty()
@IsString()
@IsOptional()
postal_code?: string
@IsNotEmpty()
@IsString()
@IsOptional()
phone?: string
@IsNotEmpty()
@IsString()
@IsOptional()
metadata?: Record<string, unknown>
}
export class AdminGetCustomersCustomerAddressesParams extends extendedFindParamsMixin(
{
limit: 100,
offset: 0,
}
) {
@IsOptional()
@IsString({ each: true })
address_name?: string | string[] | OperatorMap<string>
@IsOptional()
@IsBoolean()
is_default_shipping?: boolean
@IsOptional()
@IsBoolean()
is_default_billing?: boolean
@IsOptional()
@IsString({ each: true })
company?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
first_name?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
last_name?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
address_1?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
address_2?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
city?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
country_code?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
province?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
postal_code?: string | string[] | OperatorMap<string> | null
@IsOptional()
@IsString({ each: true })
phone?: string | string[] | OperatorMap<string> | null
@IsOptional()
@ValidateNested()
@Type(() => OperatorMapValidator)
metadata?: OperatorMap<Record<string, unknown>>
}