feat: Admin V2 Customers (#6998)

This commit is contained in:
Oli Juhl
2024-04-07 21:38:50 +02:00
committed by GitHub
parent f132929c7e
commit 4f88743591
53 changed files with 619 additions and 724 deletions
@@ -1,14 +1,13 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CreateCustomerAddressDTO,
ICustomerModuleService,
} from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -1,21 +1,21 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import {
CustomerUpdatableFields,
ICustomerModuleService,
} from "@medusajs/types"
import {
deleteCustomersWorkflow,
updateCustomersWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
AdminCustomerResponse,
CustomerUpdatableFields,
ICustomerModuleService,
} from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
res: MedusaResponse<AdminCustomerResponse>
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
@@ -26,12 +26,12 @@ export const GET = async (
relations: req.retrieveConfig.relations,
})
res.status(200).json({ customer })
res.status(200).json({ customer: customer as AdminCustomerResponse["customer"] })
}
export const POST = async (
req: AuthenticatedMedusaRequest<CustomerUpdatableFields>,
res: MedusaResponse
res: MedusaResponse<AdminCustomerResponse>
) => {
const updateCustomers = updateCustomersWorkflow(req.scope)
const { result, errors } = await updateCustomers.run({
@@ -46,7 +46,7 @@ export const POST = async (
throw errors[0].error
}
res.status(200).json({ customer: result[0] })
res.status(200).json({ customer: result[0] as AdminCustomerResponse["customer"] })
}
export const DELETE = async (
@@ -1,18 +1,19 @@
import * as QueryConfig from "./query-config"
import {
AdminGetCustomersCustomerAddressesParams,
AdminGetCustomersCustomerParams,
AdminGetCustomersParams,
AdminPostCustomersCustomerAddressesAddressReq,
AdminPostCustomersCustomerAddressesReq,
AdminPostCustomersCustomerReq,
AdminPostCustomersReq,
AdminCreateCustomer,
AdminCreateCustomerAddress,
AdminCustomerAdressesParams,
AdminCustomerParams,
AdminCustomersParams,
AdminUpdateCustomer,
AdminUpdateCustomerAddress,
} from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { validateAndTransformBody } from "../../utils/validate-body"
import { validateAndTransformQuery } from "../../utils/validate-query"
export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -24,8 +25,8 @@ export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
method: ["GET"],
matcher: "/admin/customers",
middlewares: [
transformQuery(
AdminGetCustomersParams,
validateAndTransformQuery(
AdminCustomersParams,
QueryConfig.listTransformQueryConfig
),
],
@@ -33,39 +34,51 @@ export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
{
method: ["POST"],
matcher: "/admin/customers",
middlewares: [transformBody(AdminPostCustomersReq)],
middlewares: [
validateAndTransformBody(AdminCreateCustomer),
validateAndTransformQuery(
AdminCustomerParams,
QueryConfig.listTransformQueryConfig
),
],
},
{
method: ["GET"],
matcher: "/admin/customers/:id",
middlewares: [
transformQuery(
AdminGetCustomersCustomerParams,
QueryConfig.retrieveTransformQueryConfig
validateAndTransformQuery(
AdminCustomerParams,
QueryConfig.listTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/customers/:id",
middlewares: [transformBody(AdminPostCustomersCustomerReq)],
middlewares: [
validateAndTransformBody(AdminUpdateCustomer),
validateAndTransformQuery(
AdminCustomerParams,
QueryConfig.listTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/customers/:id/addresses",
middlewares: [transformBody(AdminPostCustomersCustomerAddressesReq)],
middlewares: [validateAndTransformBody(AdminCreateCustomerAddress)],
},
{
method: ["POST"],
matcher: "/admin/customers/:id/addresses/:address_id",
middlewares: [transformBody(AdminPostCustomersCustomerAddressesAddressReq)],
middlewares: [validateAndTransformBody(AdminUpdateCustomerAddress)],
},
{
method: ["GET"],
matcher: "/admin/customers/:id/addresses",
middlewares: [
transformQuery(
AdminGetCustomersCustomerAddressesParams,
validateAndTransformQuery(
AdminCustomerAdressesParams,
QueryConfig.listAddressesTransformQueryConfig
),
],
@@ -1,56 +1,45 @@
import { createCustomersWorkflow } from "@medusajs/core-flows"
import {
AdminCustomerListResponse,
AdminCustomerResponse,
} from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { CreateCustomerDTO, ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createCustomersWorkflow } from "@medusajs/core-flows"
import { AdminCreateCustomerType } from "./validators"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
res: MedusaResponse<AdminCustomerListResponse>
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const remoteQuery = req.scope.resolve("remoteQuery")
const [customers, count] = await customerModuleService.listAndCount(
req.filterableFields,
req.listConfig
)
const variables = {
filters: req.filterableFields,
...req.remoteQueryConfig.pagination,
}
const { offset, limit } = req.validatedQuery
const query = remoteQueryObjectFromString({
entryPoint: "customers",
variables,
fields: req.remoteQueryConfig.fields,
})
// TODO: Replace with remote query
//const remoteQuery = req.scope.resolve("remoteQuery")
//const variables = {
// filters: req.filterableFields,
// order: req.listConfig.order,
// skip: req.listConfig.skip,
// take: req.listConfig.take,
//}
//const query = remoteQueryObjectFromString({
// entryPoint: "customer",
// variables,
// fields: [...req.listConfig.select!, ...req.listConfig.relations!],
//})
//const results = await remoteQuery(query)
const { rows: customers, metadata } = await remoteQuery(query)
res.json({
count,
customers,
offset,
limit,
count: metadata.count,
offset: metadata.skip,
limit: metadata.take,
})
}
export const POST = async (
req: AuthenticatedMedusaRequest<CreateCustomerDTO>,
res: MedusaResponse
req: AuthenticatedMedusaRequest<AdminCreateCustomerType>,
res: MedusaResponse<AdminCustomerResponse>
) => {
const createCustomers = createCustomersWorkflow(req.scope)
@@ -70,5 +59,5 @@ export const POST = async (
throw errors[0].error
}
res.status(200).json({ customer: result[0] })
res.status(200).json({ customer: result[0] as AdminCustomerResponse["customer"] })
}
@@ -1,353 +1,107 @@
import { OperatorMap } from "@medusajs/types"
import { Transform, Type } from "class-transformer"
import { z } from "zod"
import {
IsBoolean,
IsNotEmpty,
IsOptional,
IsString,
ValidateNested,
} from "class-validator"
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
import { OperatorMapValidator } from "../../../types/validators/operator-map"
import { IsType } from "../../../utils"
createFindParams,
createOperatorMap,
createSelectParams,
} from "../../utils/validators"
export class AdminGetCustomersCustomerParams extends FindParams {}
export const AdminCustomerParams = createSelectParams()
export const AdminCustomerGroupParams = createSelectParams()
export class AdminGetCustomersParams extends extendedFindParamsMixin({
limit: 100,
export const AdminCustomerGroupInCustomerParams = z.object({
id: z.union([z.string(), z.array(z.string())]).optional(),
name: z.union([z.string(), z.array(z.string())]).optional(),
created_at: createOperatorMap().optional(),
updated_at: createOperatorMap().optional(),
deleted_at: createOperatorMap().optional(),
})
export const AdminCustomersParams = createFindParams({
limit: 50,
offset: 0,
}) {
@IsOptional()
@IsString({ each: true })
id?: string | string[]
@IsOptional()
@IsType([String, [String], OperatorMapValidator])
email?: string | string[] | OperatorMap<string>
@IsOptional()
@ValidateNested()
@Type(() => FilterableCustomerGroupPropsValidator)
groups?: FilterableCustomerGroupPropsValidator | string | 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>
// Additional filters from BaseFilterable
@IsOptional()
@ValidateNested({ each: true })
@Type(() => AdminGetCustomersParams)
$and?: AdminGetCustomersParams[]
@IsOptional()
@ValidateNested({ each: true })
@Type(() => AdminGetCustomersParams)
$or?: AdminGetCustomersParams[]
}
class FilterableCustomerGroupPropsValidator {
@IsOptional()
@IsString({ each: true })
id?: string | string[]
@IsOptional()
@ValidateNested({ each: true })
@Type(() => OperatorMapValidator)
name?: string | OperatorMap<string>
@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>
}
export class AdminPostCustomersReq {
@IsNotEmpty()
@IsString()
@IsOptional()
company_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
first_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
last_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
email?: string
@IsNotEmpty()
@IsString()
@IsOptional()
phone?: string
}
export class AdminPostCustomersCustomerReq {
@IsNotEmpty()
@IsString()
@IsOptional()
company_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
first_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
last_name?: string
@IsNotEmpty()
@IsString()
@IsOptional()
email?: string
@IsNotEmpty()
@IsString()
@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>>
}
}).merge(
z.object({
q: z.string().optional(),
id: z.union([z.string(), z.array(z.string())]).optional(),
email: z.union([z.string(), z.array(z.string())]).optional(),
groups: z.union([
AdminCustomerGroupInCustomerParams,
z.string(),
z.array(z.string()),
]).optional(),
company_name: z.union([z.string(), z.array(z.string())]).optional(),
first_name: z.union([z.string(), z.array(z.string())]).optional(),
last_name: z.union([z.string(), z.array(z.string())]).optional(),
created_by: z.union([z.string(), z.array(z.string())]).optional(),
created_at: createOperatorMap().optional().optional(),
updated_at: createOperatorMap().optional().optional(),
deleted_at: createOperatorMap().optional().optional(),
$and: z.lazy(() => AdminCustomersParams.array()).optional(),
$or: z.lazy(() => AdminCustomersParams.array()).optional(),
})
)
export const AdminCreateCustomer = z.object({
company_name: z.string().optional(),
first_name: z.string().optional(),
last_name: z.string().optional(),
email: z.string().optional(),
phone: z.string().optional(),
})
export const AdminUpdateCustomer = AdminCreateCustomer
export const AdminCreateCustomerAddress = z.object({
address_name: z.string().optional(),
is_default_shipping: z.boolean().optional(),
is_default_billing: z.boolean().optional(),
company: z.string().optional(),
first_name: z.string().optional(),
last_name: z.string().optional(),
address_1: z.string().optional(),
address_2: z.string().optional(),
city: z.string().optional(),
country_code: z.string().optional(),
province: z.string().optional(),
postal_code: z.string().optional(),
phone: z.string().optional(),
metadata: z.record(z.unknown()).optional(),
})
export const AdminUpdateCustomerAddress = AdminCreateCustomerAddress
export const AdminCustomerAdressesParams = createFindParams({
offset: 0,
limit: 50,
}).merge(
z.object({
address_name: z.union([z.string(), z.array(z.string())]).optional(),
is_default_shipping: z.boolean().optional(),
is_default_billing: z.boolean().optional(),
company: z.union([z.string(), z.array(z.string())]).optional(),
first_name: z.union([z.string(), z.array(z.string())]).optional(),
last_name: z.union([z.string(), z.array(z.string())]).optional(),
address_1: z.union([z.string(), z.array(z.string())]).optional(),
address_2: z.union([z.string(), z.array(z.string())]).optional(),
city: z.union([z.string(), z.array(z.string())]).optional(),
country_code: z.union([z.string(), z.array(z.string())]).optional(),
province: z.union([z.string(), z.array(z.string())]).optional(),
postal_code: z.union([z.string(), z.array(z.string())]).optional(),
phone: z.union([z.string(), z.array(z.string())]).optional(),
metadata: z.record(z.unknown()).optional(),
})
)
export type AdminCustomerParamsType = z.infer<typeof AdminCustomerParams>
export type AdminCustomerGroupParamsType = z.infer<
typeof AdminCustomerGroupParams
>
export type AdminCustomerGroupInCustomerParamsType = z.infer<
typeof AdminCustomerGroupInCustomerParams
>
export type AdminCustomersParamsType = z.infer<typeof AdminCustomersParams>
export type AdminCreateCustomerType = z.infer<typeof AdminCreateCustomer>
export type AdminUpdateCustomerType = z.infer<typeof AdminUpdateCustomer>
export type AdminCreateCustomerAddressType = z.infer<
typeof AdminCreateCustomerAddress
>
@@ -1,8 +1,8 @@
import { createProductsWorkflow } from "@medusajs/core-flows"
import { CreateProductDTO } from "@medusajs/types"
import {
ContainerRegistrationKeys,
ProductStatus,
remoteQueryObjectFromString,
remoteQueryObjectFromString
} from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
@@ -17,7 +17,6 @@ import {
AdminCreateProductType,
AdminGetProductsParamsType,
} from "./validators"
import { CreateProductDTO } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetProductsParamsType>,