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

This commit is contained in:
Harminder Virk authored and GitHub committed 2024-08-05 15:16:32 +05:30
1 parent e7965db071
commit 0dab340013
11 files changed
+159 -45

No files matched your search

@@ -1,7 +1,8 @@
import { CreateCustomerAddressDTO, CustomerAddressDTO } from "@medusajs/types" import { AdditionalData, CreateCustomerAddressDTO } from "@medusajs/types"
import { import {
WorkflowData, WorkflowData,
WorkflowResponse, WorkflowResponse,
createHook,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
@@ -12,14 +13,12 @@ import {
maybeUnsetDefaultShippingAddressesStep, maybeUnsetDefaultShippingAddressesStep,
} from "../steps" } from "../steps"
type WorkflowInput = { addresses: CreateCustomerAddressDTO[] } type WorkflowInput = { addresses: CreateCustomerAddressDTO[] } & AdditionalData
export const createCustomerAddressesWorkflowId = "create-customer-addresses" export const createCustomerAddressesWorkflowId = "create-customer-addresses"
export const createCustomerAddressesWorkflow = createWorkflow( export const createCustomerAddressesWorkflow = createWorkflow(
createCustomerAddressesWorkflowId, createCustomerAddressesWorkflowId,
( (input: WorkflowData<WorkflowInput>) => {
input: WorkflowData<WorkflowInput>
): WorkflowResponse<CustomerAddressDTO[]> => {
const unsetInput = transform(input, (data) => ({ const unsetInput = transform(input, (data) => ({
create: data.addresses, create: data.addresses,
})) }))
@@ -29,6 +28,14 @@ export const createCustomerAddressesWorkflow = createWorkflow(
maybeUnsetDefaultBillingAddressesStep(unsetInput) maybeUnsetDefaultBillingAddressesStep(unsetInput)
) )
return new WorkflowResponse(createCustomerAddressesStep(input.addresses)) const createdAddresses = createCustomerAddressesStep(input.addresses)
const addressesCreated = createHook("addressesCreated", {
addresses: createdAddresses,
additional_data: input.additional_data,
})
return new WorkflowResponse(createdAddresses, {
hooks: [addressesCreated],
})
} }
) )
@@ -1,17 +1,27 @@
import { CustomerDTO, CreateCustomerDTO } from "@medusajs/types" import { CreateCustomerDTO, AdditionalData } from "@medusajs/types"
import { import {
WorkflowData, WorkflowData,
WorkflowResponse, WorkflowResponse,
createHook,
createWorkflow, createWorkflow,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { createCustomersStep } from "../steps" import { createCustomersStep } from "../steps"
type WorkflowInput = { customersData: CreateCustomerDTO[] } type WorkflowInput = { customersData: CreateCustomerDTO[] } & AdditionalData
export const createCustomersWorkflowId = "create-customers" export const createCustomersWorkflowId = "create-customers"
export const createCustomersWorkflow = createWorkflow( export const createCustomersWorkflow = createWorkflow(
createCustomersWorkflowId, createCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowResponse<CustomerDTO[]> => { (input: WorkflowData<WorkflowInput>) => {
return new WorkflowResponse(createCustomersStep(input.customersData)) const createdCustomers = createCustomersStep(input.customersData)
const customersCreated = createHook("customersCreated", {
customers: createdCustomers,
additional_data: input.additional_data,
})
return new WorkflowResponse(createdCustomers, {
hooks: [customersCreated],
})
} }
) )
@@ -1,4 +1,9 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { deleteCustomerAddressesStep } from "../steps" import { deleteCustomerAddressesStep } from "../steps"
type WorkflowInput = { ids: string[] } type WorkflowInput = { ids: string[] }
@@ -6,7 +11,14 @@ type WorkflowInput = { ids: string[] }
export const deleteCustomerAddressesWorkflowId = "delete-customer-addresses" export const deleteCustomerAddressesWorkflowId = "delete-customer-addresses"
export const deleteCustomerAddressesWorkflow = createWorkflow( export const deleteCustomerAddressesWorkflow = createWorkflow(
deleteCustomerAddressesWorkflowId, deleteCustomerAddressesWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => { (input: WorkflowData<WorkflowInput>) => {
return deleteCustomerAddressesStep(input.ids) const deletedAddresses = deleteCustomerAddressesStep(input.ids)
const addressesDeleted = createHook("addressesDeleted", {
ids: input.ids,
})
return new WorkflowResponse(deletedAddresses, {
hooks: [addressesDeleted],
})
} }
) )
@@ -1,4 +1,9 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { deleteCustomersStep } from "../steps" import { deleteCustomersStep } from "../steps"
type WorkflowInput = { ids: string[] } type WorkflowInput = { ids: string[] }
@@ -6,7 +11,14 @@ type WorkflowInput = { ids: string[] }
export const deleteCustomersWorkflowId = "delete-customers" export const deleteCustomersWorkflowId = "delete-customers"
export const deleteCustomersWorkflow = createWorkflow( export const deleteCustomersWorkflow = createWorkflow(
deleteCustomersWorkflowId, deleteCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => { (input: WorkflowData<WorkflowInput>) => {
return deleteCustomersStep(input.ids) const deletedCustomers = deleteCustomersStep(input.ids)
const customersDeleted = createHook("customersDeleted", {
ids: input.ids,
})
return new WorkflowResponse(deletedCustomers, {
hooks: [customersDeleted],
})
} }
) )
@@ -1,11 +1,12 @@
import { import {
FilterableCustomerAddressProps, FilterableCustomerAddressProps,
CustomerAddressDTO,
UpdateCustomerAddressDTO, UpdateCustomerAddressDTO,
AdditionalData,
} from "@medusajs/types" } from "@medusajs/types"
import { import {
WorkflowData, WorkflowData,
WorkflowResponse, WorkflowResponse,
createHook,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
@@ -19,14 +20,12 @@ import {
type WorkflowInput = { type WorkflowInput = {
selector: FilterableCustomerAddressProps selector: FilterableCustomerAddressProps
update: UpdateCustomerAddressDTO update: UpdateCustomerAddressDTO
} } & AdditionalData
export const updateCustomerAddressesWorkflowId = "update-customer-addresses" export const updateCustomerAddressesWorkflowId = "update-customer-addresses"
export const updateCustomerAddressesWorkflow = createWorkflow( export const updateCustomerAddressesWorkflow = createWorkflow(
updateCustomerAddressesWorkflowId, updateCustomerAddressesWorkflowId,
( (input: WorkflowData<WorkflowInput>) => {
input: WorkflowData<WorkflowInput>
): WorkflowResponse<CustomerAddressDTO[]> => {
const unsetInput = transform(input, (data) => ({ const unsetInput = transform(input, (data) => ({
update: data, update: data,
})) }))
@@ -36,6 +35,14 @@ export const updateCustomerAddressesWorkflow = createWorkflow(
maybeUnsetDefaultBillingAddressesStep(unsetInput) maybeUnsetDefaultBillingAddressesStep(unsetInput)
) )
return new WorkflowResponse(updateCustomerAddressesStep(input)) const updatedAddresses = updateCustomerAddressesStep(input)
const addressesUpdated = createHook("addressesUpdated", {
addresses: updatedAddresses,
additional_data: input.additional_data,
})
return new WorkflowResponse(updatedAddresses, {
hooks: [addressesUpdated],
})
} }
) )
@@ -1,11 +1,12 @@
import { import {
CustomerDTO, AdditionalData,
CustomerUpdatableFields, CustomerUpdatableFields,
FilterableCustomerProps, FilterableCustomerProps,
} from "@medusajs/types" } from "@medusajs/types"
import { import {
WorkflowData, WorkflowData,
WorkflowResponse, WorkflowResponse,
createHook,
createWorkflow, createWorkflow,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { updateCustomersStep } from "../steps" import { updateCustomersStep } from "../steps"
@@ -13,14 +14,22 @@ import { updateCustomersStep } from "../steps"
type UpdateCustomersStepInput = { type UpdateCustomersStepInput = {
selector: FilterableCustomerProps selector: FilterableCustomerProps
update: CustomerUpdatableFields update: CustomerUpdatableFields
} } & AdditionalData
type WorkflowInput = UpdateCustomersStepInput type WorkflowInput = UpdateCustomersStepInput
export const updateCustomersWorkflowId = "update-customers" export const updateCustomersWorkflowId = "update-customers"
export const updateCustomersWorkflow = createWorkflow( export const updateCustomersWorkflow = createWorkflow(
updateCustomersWorkflowId, updateCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowResponse<CustomerDTO[]> => { (input: WorkflowData<WorkflowInput>) => {
return new WorkflowResponse(updateCustomersStep(input)) const updatedCustomers = updateCustomersStep(input)
const customersUpdated = createHook("customersUpdated", {
customers: updatedCustomers,
additional_data: input.additional_data,
})
return new WorkflowResponse(updatedCustomers, {
hooks: [customersUpdated],
})
} }
) )
@@ -13,6 +13,7 @@ import {
} from "@medusajs/utils" } from "@medusajs/utils"
import { AdminCreateCustomerAddressType } from "../../../validators" import { AdminCreateCustomerAddressType } from "../../../validators"
import { refetchCustomer } from "../../../helpers" import { refetchCustomer } from "../../../helpers"
import { AdditionalData } from "@medusajs/types"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -33,14 +34,19 @@ export const GET = async (
} }
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerAddressType>, req: AuthenticatedMedusaRequest<
AdminCreateCustomerAddressType & AdditionalData
>,
res: MedusaResponse res: MedusaResponse
) => { ) => {
const { additional_data, ...rest } = req.validatedBody
const updateAddresses = updateCustomerAddressesWorkflow(req.scope) const updateAddresses = updateCustomerAddressesWorkflow(req.scope)
await updateAddresses.run({ await updateAddresses.run({
input: { input: {
selector: { id: req.params.address_id, customer_id: req.params.id }, 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" } from "@medusajs/utils"
import { AdminCreateCustomerAddressType } from "../../validators" import { AdminCreateCustomerAddressType } from "../../validators"
import { refetchCustomer } from "../../helpers" import { refetchCustomer } from "../../helpers"
import { AdditionalData } from "@medusajs/types"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -37,20 +38,23 @@ export const GET = async (
} }
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerAddressType>, req: AuthenticatedMedusaRequest<
AdminCreateCustomerAddressType & AdditionalData
>,
res: MedusaResponse res: MedusaResponse
) => { ) => {
const { additional_data, ...rest } = req.validatedBody
const customerId = req.params.id const customerId = req.params.id
const createAddresses = createCustomerAddressesWorkflow(req.scope) const createAddresses = createCustomerAddressesWorkflow(req.scope)
const addresses = [ const addresses = [
{ {
...req.validatedBody, ...rest,
customer_id: customerId, customer_id: customerId,
}, },
] ]
await createAddresses.run({ await createAddresses.run({
input: { addresses }, input: { addresses, additional_data },
}) })
const customer = await refetchCustomer( const customer = await refetchCustomer(
@@ -2,7 +2,7 @@ import {
deleteCustomersWorkflow, deleteCustomersWorkflow,
updateCustomersWorkflow, updateCustomersWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { AdminCustomer } from "@medusajs/types" import { AdditionalData, AdminCustomer } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils" import { MedusaError } from "@medusajs/utils"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
@@ -32,13 +32,16 @@ export const GET = async (
} }
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerType>, req: AuthenticatedMedusaRequest<AdminUpdateCustomerType & AdditionalData>,
res: MedusaResponse<{ customer: AdminCustomer }> res: MedusaResponse<{ customer: AdminCustomer }>
) => { ) => {
const { additional_data, ...rest } = req.validatedBody
await updateCustomersWorkflow(req.scope).run({ await updateCustomersWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody, update: rest,
additional_data,
}, },
}) })
@@ -1,6 +1,10 @@
import { createCustomersWorkflow } from "@medusajs/core-flows" import { createCustomersWorkflow } from "@medusajs/core-flows"
import { AdminCustomer, PaginatedResponse } from "@medusajs/types" import {
AdditionalData,
AdminCustomer,
PaginatedResponse,
} from "@medusajs/types"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
@@ -38,20 +42,21 @@ export const GET = async (
} }
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCustomerType>, req: AuthenticatedMedusaRequest<AdminCreateCustomerType & AdditionalData>,
res: MedusaResponse<{ customer: AdminCustomer }> res: MedusaResponse<{ customer: AdminCustomer }>
) => { ) => {
const { additional_data, ...rest } = req.validatedBody
const createCustomers = createCustomersWorkflow(req.scope) const createCustomers = createCustomersWorkflow(req.scope)
const customersData = [ const customersData = [
{ {
...req.validatedBody, ...rest,
created_by: req.auth_context.actor_id, created_by: req.auth_context.actor_id,
}, },
] ]
const { result } = await createCustomers.run({ const { result } = await createCustomers.run({
input: { customersData }, input: { customersData, additional_data },
}) })
const customer = await refetchCustomer( const customer = await refetchCustomer(
@@ -1,4 +1,4 @@
import { z } from "zod" import { z, ZodObject } from "zod"
import { import {
createFindParams, createFindParams,
createOperatorMap, createOperatorMap,
@@ -42,7 +42,7 @@ export const AdminCustomersParams = createFindParams({
}) })
) )
export const AdminCreateCustomer = z.object({ export const CreateCustomer = z.object({
email: z.string().email().nullish(), email: z.string().email().nullish(),
company_name: z.string().nullish(), company_name: z.string().nullish(),
first_name: z.string().nullish(), first_name: z.string().nullish(),
@@ -50,8 +50,21 @@ export const AdminCreateCustomer = z.object({
phone: z.string().nullish(), phone: z.string().nullish(),
metadata: z.record(z.unknown()).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(), email: z.string().email().nullish(),
company_name: z.string().nullish(), company_name: z.string().nullish(),
first_name: z.string().nullish(), first_name: z.string().nullish(),
@@ -59,8 +72,21 @@ export const AdminUpdateCustomer = z.object({
phone: z.string().nullish(), phone: z.string().nullish(),
metadata: z.record(z.unknown()).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(), address_name: z.string().nullish(),
is_default_shipping: z.boolean().optional(), is_default_shipping: z.boolean().optional(),
is_default_billing: z.boolean().optional(), is_default_billing: z.boolean().optional(),
@@ -76,6 +102,19 @@ export const AdminCreateCustomerAddress = z.object({
phone: z.string().nullish(), phone: z.string().nullish(),
metadata: z.record(z.unknown()).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 export const AdminUpdateCustomerAddress = AdminCreateCustomerAddress
@@ -95,8 +134,8 @@ export const AdminCustomerAddressesParams = createFindParams({
export type AdminCustomerParamsType = z.infer<typeof AdminCustomerParams> export type AdminCustomerParamsType = z.infer<typeof AdminCustomerParams>
export type AdminCustomersParamsType = z.infer<typeof AdminCustomersParams> export type AdminCustomersParamsType = z.infer<typeof AdminCustomersParams>
export type AdminCreateCustomerType = z.infer<typeof AdminCreateCustomer> export type AdminCreateCustomerType = z.infer<typeof CreateCustomer>
export type AdminUpdateCustomerType = z.infer<typeof AdminUpdateCustomer> export type AdminUpdateCustomerType = z.infer<typeof UpdateCustomer>
export type AdminCreateCustomerAddressType = z.infer< export type AdminCreateCustomerAddressType = z.infer<
typeof AdminCreateCustomerAddress typeof CreateCustomerAddress
> >