chore: rename route from api-v2 to api (#7379)
* chore: rename route from api-v2 to api * chore: change oas references * chore: remove v2 ref
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import {
|
||||
deleteCustomerAddressesWorkflow,
|
||||
updateCustomerAddressesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { AdminCreateCustomerAddressType } from "../../../validators"
|
||||
import { refetchCustomer } from "../../../helpers"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "customer_address",
|
||||
variables: {
|
||||
filters: { id: req.params.address_id, customer_id: req.params.id },
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [address] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ address })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateCustomerAddressType>,
|
||||
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,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const customer = await refetchCustomer(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ customer })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
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
|
||||
}
|
||||
|
||||
const customer = await refetchCustomer(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "customer_address",
|
||||
deleted: true,
|
||||
parent: customer,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { AdminCreateCustomerAddressType } from "../../validators"
|
||||
import { refetchCustomer } from "../../helpers"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const customerId = req.params.id
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "customer_address",
|
||||
variables: {
|
||||
filters: { ...req.filterableFields, customer_id: customerId },
|
||||
...req.remoteQueryConfig.pagination,
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const { rows: addresses, metadata } = await remoteQuery(query)
|
||||
|
||||
res.json({
|
||||
addresses,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateCustomerAddressType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const customerId = req.params.id
|
||||
const createAddresses = createCustomerAddressesWorkflow(req.scope)
|
||||
const addresses = [
|
||||
{
|
||||
...req.validatedBody,
|
||||
customer_id: customerId,
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createAddresses.run({
|
||||
input: { addresses },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const customer = await refetchCustomer(
|
||||
customerId,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ customer })
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
deleteCustomersWorkflow,
|
||||
updateCustomersWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { AdminCustomerResponse } from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { refetchCustomer } from "../helpers"
|
||||
import { AdminUpdateCustomerType } from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse<AdminCustomerResponse>
|
||||
) => {
|
||||
const customer = await refetchCustomer(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
if (!customer) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Customer with id: ${req.params.id} not found`
|
||||
)
|
||||
}
|
||||
|
||||
res.status(200).json({ customer })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType>,
|
||||
res: MedusaResponse<AdminCustomerResponse>
|
||||
) => {
|
||||
const { errors } = await updateCustomersWorkflow(req.scope).run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const customer = await refetchCustomer(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
res.status(200).json({ customer })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
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,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const refetchCustomer = async (
|
||||
customerId: string,
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "customer",
|
||||
variables: {
|
||||
filters: { id: customerId },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
const customers = await remoteQuery(queryObject)
|
||||
return customers[0]
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import {
|
||||
AdminCreateCustomer,
|
||||
AdminCreateCustomerAddress,
|
||||
AdminCustomerAddressesParams,
|
||||
AdminCustomerParams,
|
||||
AdminCustomersParams,
|
||||
AdminUpdateCustomer,
|
||||
AdminUpdateCustomerAddress,
|
||||
} from "./validators"
|
||||
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/middlewares/authenticate-middleware"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
|
||||
export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
matcher: "/admin/customers*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/customers",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminCustomersParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/customers",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateCustomer),
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/customers/:id",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/customers/:id",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminUpdateCustomer),
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/customers/:id/addresses",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateCustomerAddress),
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/customers/:id/addresses/:address_id",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminUpdateCustomerAddress),
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/customers/:id/addresses/:address_id",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/customers/:id/addresses",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminCustomerAddressesParams,
|
||||
QueryConfig.listAddressesTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,52 @@
|
||||
export const defaultAdminCustomerFields = [
|
||||
"id",
|
||||
"company_name",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"email",
|
||||
"phone",
|
||||
"metadata",
|
||||
"has_account",
|
||||
"created_by",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaults: defaultAdminCustomerFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
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 = {
|
||||
defaults: defaultAdminCustomerAddressFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listAddressesTransformQueryConfig = {
|
||||
...retrieveAddressTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { createCustomersWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AdminCustomerListResponse,
|
||||
AdminCustomerResponse,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { AdminCreateCustomerType } from "./validators"
|
||||
import { refetchCustomer } from "./helpers"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse<AdminCustomerListResponse>
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "customers",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
...req.remoteQueryConfig.pagination,
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const { rows: customers, metadata } = await remoteQuery(query)
|
||||
|
||||
res.json({
|
||||
customers,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateCustomerType>,
|
||||
res: MedusaResponse<AdminCustomerResponse>
|
||||
) => {
|
||||
const createCustomers = createCustomersWorkflow(req.scope)
|
||||
|
||||
const customersData = [
|
||||
{
|
||||
...req.validatedBody,
|
||||
created_by: req.auth?.actor_id,
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createCustomers.run({
|
||||
input: { customersData },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const customer = await refetchCustomer(
|
||||
result[0].id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ customer })
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import { z } from "zod"
|
||||
import {
|
||||
createFindParams,
|
||||
createOperatorMap,
|
||||
createSelectParams,
|
||||
} from "../../utils/validators"
|
||||
|
||||
export const AdminCustomerParams = createSelectParams()
|
||||
|
||||
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,
|
||||
}).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(),
|
||||
updated_at: createOperatorMap().optional(),
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
$and: z.lazy(() => AdminCustomersParams.array()).optional(),
|
||||
$or: z.lazy(() => AdminCustomersParams.array()).optional(),
|
||||
})
|
||||
)
|
||||
|
||||
export const AdminCreateCustomer = z.object({
|
||||
email: z.string().email().optional(),
|
||||
company_name: z.string().optional(),
|
||||
first_name: z.string().optional(),
|
||||
last_name: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
|
||||
export const AdminUpdateCustomer = z.object({
|
||||
email: z.string().email().nullable().optional(),
|
||||
company_name: z.string().nullable().optional(),
|
||||
first_name: z.string().nullable().optional(),
|
||||
last_name: z.string().nullable().optional(),
|
||||
phone: z.string().nullable().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
|
||||
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 AdminCustomerAddressesParams = createFindParams({
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
}).merge(
|
||||
z.object({
|
||||
q: z.string().optional(),
|
||||
company: 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(),
|
||||
})
|
||||
)
|
||||
|
||||
export type AdminCustomerParamsType = z.infer<typeof AdminCustomerParams>
|
||||
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
|
||||
>
|
||||
Reference in New Issue
Block a user