feat(customer): store addresses (#6283)
- GET /customers/me/addresses - POST /customers/me/addresses - GET /customers/me/addresses/:address_id - POST /customers/me/addresses/:address_id - DELETE /customers/me/addresses/:address_id
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import {
|
||||
updateCustomerAddressesWorkflow,
|
||||
deleteCustomerAddressesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CustomerAddressDTO, ICustomerModuleService } from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const id = req.auth_user!.app_metadata.customer_id
|
||||
|
||||
const customerModuleService = req.scope.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const [address] = await customerModuleService.listAddresses(
|
||||
{ id: req.params.address_id, customer_id: id },
|
||||
{
|
||||
select: req.retrieveConfig.select,
|
||||
relations: req.retrieveConfig.relations,
|
||||
}
|
||||
)
|
||||
|
||||
res.status(200).json({ address })
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const id = req.auth_user!.app_metadata.customer_id
|
||||
const service = req.scope.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await validateCustomerAddress(service, id, req.params.address_id)
|
||||
|
||||
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.auth_user!.app_metadata.customer_id
|
||||
|
||||
const service = req.scope.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await validateCustomerAddress(service, id, req.params.address_id)
|
||||
const deleteAddress = deleteCustomerAddressesWorkflow(req.scope)
|
||||
|
||||
const { errors } = await deleteAddress.run({
|
||||
input: { ids: [req.params.address_id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "address",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
|
||||
const validateCustomerAddress = async (
|
||||
customerModuleService: ICustomerModuleService,
|
||||
customerId: string,
|
||||
addressId: string
|
||||
) => {
|
||||
const [address] = await customerModuleService.listAddresses(
|
||||
{ id: addressId, customer_id: customerId },
|
||||
{ select: ["id"] }
|
||||
)
|
||||
|
||||
if (!address) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Address with id: ${addressId} was not found`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
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.auth_user!.app_metadata.customer_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.auth_user!.app_metadata.customer_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] })
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { StorePostCustomersReq, StoreGetCustomersMeParams } from "./validators"
|
||||
import {
|
||||
StorePostCustomersReq,
|
||||
StoreGetCustomersMeParams,
|
||||
StorePostCustomersMeAddressesReq,
|
||||
StorePostCustomersMeAddressesAddressReq,
|
||||
StoreGetCustomersMeAddressesParams,
|
||||
} from "./validators"
|
||||
import authenticate from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
@@ -25,4 +31,24 @@ export const storeCustomerRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/customers/me/addresses",
|
||||
middlewares: [transformBody(StorePostCustomersMeAddressesReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/customers/me/addresses/:address_id",
|
||||
middlewares: [transformBody(StorePostCustomersMeAddressesAddressReq)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/customers/me/addresses",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
StoreGetCustomersMeAddressesParams,
|
||||
QueryConfig.listAddressesTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -22,3 +22,35 @@ export const retrieveTransformQueryConfig = {
|
||||
allowedRelations: allowedStoreCustomersRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const defaultStoreCustomerAddressRelations = []
|
||||
export const allowedStoreCustomerAddressRelations = ["customer"]
|
||||
export const defaultStoreCustomerAddressFields = [
|
||||
"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: defaultStoreCustomerAddressFields,
|
||||
defaultRelations: defaultStoreCustomerAddressRelations,
|
||||
allowedRelations: allowedStoreCustomerAddressRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listAddressesTransformQueryConfig = {
|
||||
...retrieveAddressTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
import { IsEmail, IsObject, IsOptional, IsString } from "class-validator"
|
||||
import { FindParams } from "../../../types/common"
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsBoolean,
|
||||
IsEmail,
|
||||
IsNotEmpty,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { extendedFindParamsMixin, FindParams } from "../../../types/common"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
|
||||
export class StoreGetCustomersMeParams extends FindParams {}
|
||||
|
||||
@@ -27,3 +38,207 @@ export class StorePostCustomersReq {
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class StorePostCustomersMeAddressesReq {
|
||||
@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 StorePostCustomersMeAddressesAddressReq {
|
||||
@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 StoreGetCustomersMeAddressesParams 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>>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user