feature: add hooks to customers modules workflows (#8442)

This commit is contained in:
Harminder Virk
2024-08-05 15:16:32 +05:30
committed by GitHub
parent e7965db071
commit 0dab340013
11 changed files with 159 additions and 45 deletions
@@ -13,6 +13,7 @@ import {
} from "@medusajs/utils"
import { AdminCreateCustomerAddressType } from "../../../validators"
import { refetchCustomer } from "../../../helpers"
import { AdditionalData } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -33,14 +34,19 @@ export const GET = async (
}
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerAddressType>,
req: AuthenticatedMedusaRequest<
AdminCreateCustomerAddressType & AdditionalData
>,
res: MedusaResponse
) => {
const { additional_data, ...rest } = req.validatedBody
const updateAddresses = updateCustomerAddressesWorkflow(req.scope)
await updateAddresses.run({
input: {
selector: { id: req.params.address_id, customer_id: req.params.id },
update: req.validatedBody,
update: rest,
additional_data,
},
})
@@ -9,6 +9,7 @@ import {
} from "@medusajs/utils"
import { AdminCreateCustomerAddressType } from "../../validators"
import { refetchCustomer } from "../../helpers"
import { AdditionalData } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -37,20 +38,23 @@ export const GET = async (
}
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerAddressType>,
req: AuthenticatedMedusaRequest<
AdminCreateCustomerAddressType & AdditionalData
>,
res: MedusaResponse
) => {
const { additional_data, ...rest } = req.validatedBody
const customerId = req.params.id
const createAddresses = createCustomerAddressesWorkflow(req.scope)
const addresses = [
{
...req.validatedBody,
...rest,
customer_id: customerId,
},
]
await createAddresses.run({
input: { addresses },
input: { addresses, additional_data },
})
const customer = await refetchCustomer(
@@ -2,7 +2,7 @@ import {
deleteCustomersWorkflow,
updateCustomersWorkflow,
} from "@medusajs/core-flows"
import { AdminCustomer } from "@medusajs/types"
import { AdditionalData, AdminCustomer } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
@@ -32,13 +32,16 @@ export const GET = async (
}
export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType>,
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType & AdditionalData>,
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const { additional_data, ...rest } = req.validatedBody
await updateCustomersWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
update: rest,
additional_data,
},
})
@@ -1,6 +1,10 @@
import { createCustomersWorkflow } from "@medusajs/core-flows"
import { AdminCustomer, PaginatedResponse } from "@medusajs/types"
import {
AdditionalData,
AdminCustomer,
PaginatedResponse,
} from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
@@ -38,20 +42,21 @@ export const GET = async (
}
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerType>,
req: AuthenticatedMedusaRequest<AdminCreateCustomerType & AdditionalData>,
res: MedusaResponse<{ customer: AdminCustomer }>
) => {
const { additional_data, ...rest } = req.validatedBody
const createCustomers = createCustomersWorkflow(req.scope)
const customersData = [
{
...req.validatedBody,
...rest,
created_by: req.auth_context.actor_id,
},
]
const { result } = await createCustomers.run({
input: { customersData },
input: { customersData, additional_data },
})
const customer = await refetchCustomer(
@@ -1,4 +1,4 @@
import { z } from "zod"
import { z, ZodObject } from "zod"
import {
createFindParams,
createOperatorMap,
@@ -42,7 +42,7 @@ export const AdminCustomersParams = createFindParams({
})
)
export const AdminCreateCustomer = z.object({
export const CreateCustomer = z.object({
email: z.string().email().nullish(),
company_name: z.string().nullish(),
first_name: z.string().nullish(),
@@ -50,8 +50,21 @@ export const AdminCreateCustomer = z.object({
phone: z.string().nullish(),
metadata: z.record(z.unknown()).nullish(),
})
export const AdminCreateCustomer = (
additionalDataValidator?: ZodObject<any, any>
) => {
if (!additionalDataValidator) {
return CreateCustomer.extend({
additional_data: z.record(z.unknown()).nullish(),
})
}
export const AdminUpdateCustomer = z.object({
return CreateCustomer.extend({
additional_data: additionalDataValidator,
})
}
export const UpdateCustomer = z.object({
email: z.string().email().nullish(),
company_name: z.string().nullish(),
first_name: z.string().nullish(),
@@ -59,8 +72,21 @@ export const AdminUpdateCustomer = z.object({
phone: z.string().nullish(),
metadata: z.record(z.unknown()).nullish(),
})
export const AdminUpdateCustomer = (
additionalDataValidator?: ZodObject<any, any>
) => {
if (!additionalDataValidator) {
return UpdateCustomer.extend({
additional_data: z.record(z.unknown()).nullish(),
})
}
export const AdminCreateCustomerAddress = z.object({
return UpdateCustomer.extend({
additional_data: additionalDataValidator,
})
}
export const CreateCustomerAddress = z.object({
address_name: z.string().nullish(),
is_default_shipping: z.boolean().optional(),
is_default_billing: z.boolean().optional(),
@@ -76,6 +102,19 @@ export const AdminCreateCustomerAddress = z.object({
phone: z.string().nullish(),
metadata: z.record(z.unknown()).nullish(),
})
export const AdminCreateCustomerAddress = (
additionalDataValidator?: ZodObject<any, any>
) => {
if (!additionalDataValidator) {
return CreateCustomerAddress.extend({
additional_data: z.record(z.unknown()).nullish(),
})
}
return CreateCustomerAddress.extend({
additional_data: additionalDataValidator,
})
}
export const AdminUpdateCustomerAddress = AdminCreateCustomerAddress
@@ -95,8 +134,8 @@ export const AdminCustomerAddressesParams = createFindParams({
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 AdminCreateCustomerType = z.infer<typeof CreateCustomer>
export type AdminUpdateCustomerType = z.infer<typeof UpdateCustomer>
export type AdminCreateCustomerAddressType = z.infer<
typeof AdminCreateCustomerAddress
typeof CreateCustomerAddress
>