feat: Admin V2 customer group (#7000)

This commit is contained in:
Oli Juhl authored and GitHub committed 2024-04-09 12:32:27 +02:00
1 parent dbddfc12ed
commit 3882cac622
43 files changed
+583 -309

No files matched your search

@@ -1,15 +1,19 @@
import { createCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"
import { createCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
} from "../../../../../../../types/routing"
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../../validators"
export const POST = async (
// eslint-disable-next-line max-len
req: AuthenticatedMedusaRequest<AdminPostCustomerGroupsGroupCustomersBatchReq>,
res: MedusaResponse
res: MedusaResponse<AdminCustomerGroupResponse>
) => {
const { id } = req.params
const { customer_ids } = req.validatedBody
@@ -30,5 +34,15 @@ export const POST = async (
throw errors[0].error
}
res.status(200).json({ customer_group_customers: result })
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const queryObject = remoteQueryObjectFromString({
entryPoint: "customer_group",
variables: { id },
fields: req.remoteQueryConfig.fields,
})
const [customer_group] = await remoteQuery(queryObject)
res.status(200).json({ customer_group })
}
@@ -1,15 +1,19 @@
import { deleteCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
import { AdminCustomerGroupResponse } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"
import { deleteCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
} from "../../../../../../../types/routing"
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../../validators"
export const POST = async (
// eslint-disable-next-line max-len
req: AuthenticatedMedusaRequest<AdminPostCustomerGroupsGroupCustomersBatchReq>,
res: MedusaResponse
res: MedusaResponse<AdminCustomerGroupResponse>
) => {
const { id } = req.params
const { customer_ids } = req.validatedBody
@@ -30,8 +34,15 @@ export const POST = async (
throw errors[0].error
}
res.status(200).json({
object: "customer_group_customers",
deleted: true,
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const queryObject = remoteQueryObjectFromString({
entryPoint: "customer_group",
variables: { id },
fields: req.remoteQueryConfig.fields,
})
const [customer_group] = await remoteQuery(queryObject)
res.status(200).json({ customer_group })
}
@@ -1,5 +1,6 @@
import * as QueryConfig from "./query-config"
import { transformBody, transformQuery } from "../../../api/middlewares"
import {
AdminDeleteCustomerGroupsGroupCustomersBatchReq,
AdminGetCustomerGroupsGroupCustomersParams,
@@ -9,7 +10,6 @@ import {
AdminPostCustomerGroupsGroupReq,
AdminPostCustomerGroupsReq,
} from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
@@ -63,14 +63,24 @@ export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/customer-groups/:id/customers/batch",
middlewares: [transformBody(AdminPostCustomerGroupsGroupCustomersBatchReq)],
matcher: "/admin/customer-groups/:id/customers/batch/add",
middlewares: [
transformBody(AdminPostCustomerGroupsGroupCustomersBatchReq),
transformQuery(
AdminGetCustomerGroupsGroupParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/customer-groups/:id/customers/remove",
matcher: "/admin/customer-groups/:id/customers/batch/remove",
middlewares: [
transformBody(AdminDeleteCustomerGroupsGroupCustomersBatchReq),
transformQuery(
AdminGetCustomerGroupsGroupParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
]
@@ -1,14 +1,15 @@
import { OperatorMap } from "@medusajs/types"
import { Transform, Type } from "class-transformer"
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
import {
IsNotEmpty,
IsOptional,
IsString,
ValidateNested,
} from "class-validator"
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
import { OperatorMapValidator } from "../../../types/validators/operator-map"
import { Transform, Type } from "class-transformer"
import { IsType } from "../../../utils"
import { OperatorMap } from "@medusajs/types"
import { OperatorMapValidator } from "../../../types/validators/operator-map"
export class AdminGetCustomerGroupsGroupParams extends FindParams {}
@@ -61,6 +62,10 @@ export class AdminGetCustomerGroupsParams extends extendedFindParamsMixin({
limit: 100,
offset: 0,
}) {
@IsOptional()
@IsString()
q?: string
@IsOptional()
@IsString({ each: true })
id?: string | string[]
@@ -120,6 +125,10 @@ export class AdminGetCustomerGroupsGroupCustomersParams extends extendedFindPara
offset: 0,
}
) {
@IsOptional()
@IsString()
q?: string
@IsOptional()
@IsString({ each: true })
id?: string | string[]
@@ -2,12 +2,11 @@ import {
deleteCustomersWorkflow,
updateCustomersWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { AdminCustomerResponse, CustomerUpdatableFields } from "@medusajs/types"
import {
AdminCustomerResponse,
CustomerUpdatableFields,
ICustomerModuleService,
} from "@medusajs/types"
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
@@ -17,24 +16,26 @@ export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse<AdminCustomerResponse>
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const customer = await customerModuleService.retrieve(req.params.id, {
select: req.retrieveConfig.select,
relations: req.retrieveConfig.relations,
const variables = { id: req.params.id }
const queryObject = remoteQueryObjectFromString({
entryPoint: "customer",
variables,
fields: req.remoteQueryConfig.fields,
})
res.status(200).json({ customer: customer as AdminCustomerResponse["customer"] })
const [customer] = await remoteQuery(queryObject)
res.status(200).json({ customer })
}
export const POST = async (
req: AuthenticatedMedusaRequest<CustomerUpdatableFields>,
res: MedusaResponse<AdminCustomerResponse>
) => {
const updateCustomers = updateCustomersWorkflow(req.scope)
const { result, errors } = await updateCustomers.run({
const { errors } = await updateCustomersWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
@@ -46,7 +47,19 @@ export const POST = async (
throw errors[0].error
}
res.status(200).json({ customer: result[0] as AdminCustomerResponse["customer"] })
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const queryObject = remoteQueryObjectFromString({
entryPoint: "customer",
variables: {
filters: { id: req.params.id },
},
fields: req.remoteQueryConfig.fields,
})
const [customer] = await remoteQuery(queryObject)
res.status(200).json({ customer })
}
export const DELETE = async (
@@ -6,6 +6,7 @@ export const defaultAdminCustomerFields = [
"first_name",
"last_name",
"email",
"phone",
"created_at",
"updated_at",
"deleted_at",
@@ -24,11 +24,13 @@ export const AdminCustomersParams = createFindParams({
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(),
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(),