feat: Cart Customer link + create cart with customer (#6426)
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CustomerDTO, ICustomerModuleService } from "@medusajs/types"
|
||||
import { validateEmail } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
interface StepInput {
|
||||
customerId?: string
|
||||
email?: string
|
||||
}
|
||||
|
||||
interface StepOutput {
|
||||
customer?: CustomerDTO
|
||||
email?: string
|
||||
}
|
||||
|
||||
interface StepCompensateInput {
|
||||
customer?: CustomerDTO
|
||||
customerWasCreated: boolean
|
||||
}
|
||||
|
||||
export const findOrCreateCustomerStepId = "find-or-create-customer"
|
||||
export const findOrCreateCustomerStep = createStep(
|
||||
findOrCreateCustomerStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const customerData: StepOutput = {}
|
||||
let customerWasCreated = false
|
||||
|
||||
if (data.customerId) {
|
||||
const customer = await service.retrieve(data.customerId)
|
||||
customerData.customer = customer
|
||||
customerData.email = customer.email
|
||||
|
||||
return new StepResponse(customerData, {
|
||||
customerWasCreated,
|
||||
})
|
||||
}
|
||||
|
||||
if (data.email) {
|
||||
const validatedEmail = validateEmail(data.email)
|
||||
|
||||
let [customer] = await service.list({
|
||||
email: validatedEmail,
|
||||
has_account: false,
|
||||
})
|
||||
|
||||
if (!customer) {
|
||||
customer = await service.create({ email: validatedEmail })
|
||||
customerWasCreated = true
|
||||
}
|
||||
|
||||
customerData.customer = customer
|
||||
customerData.email = customer.email
|
||||
}
|
||||
|
||||
return new StepResponse(customerData, {
|
||||
customer: customerData.customer,
|
||||
customerWasCreated,
|
||||
})
|
||||
},
|
||||
async (compData, { container }) => {
|
||||
const { customer, customerWasCreated } = compData as StepCompensateInput
|
||||
|
||||
if (!customerWasCreated || !customer?.id) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await service.delete(customer.id)
|
||||
}
|
||||
)
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./create-carts"
|
||||
export * from "./find-one-or-any-region"
|
||||
export * from "./find-or-create-customer"
|
||||
export * from "./update-carts"
|
||||
|
||||
|
||||
@@ -4,28 +4,44 @@ import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createCartsStep, findOneOrAnyRegionStep } from "../steps"
|
||||
import {
|
||||
createCartsStep,
|
||||
findOneOrAnyRegionStep,
|
||||
findOrCreateCustomerStep,
|
||||
} from "../steps"
|
||||
|
||||
export const createCartWorkflowId = "create-cart"
|
||||
export const createCartWorkflow = createWorkflow(
|
||||
createCartWorkflowId,
|
||||
(
|
||||
input: WorkflowData<CreateCartWorkflowInputDTO>
|
||||
): WorkflowData<CartDTO[]> => {
|
||||
(input: WorkflowData<CreateCartWorkflowInputDTO>): WorkflowData<CartDTO> => {
|
||||
const region = findOneOrAnyRegionStep({
|
||||
regionId: input.region_id,
|
||||
})
|
||||
|
||||
const cartInput = transform({ input, region }, (data) => {
|
||||
return {
|
||||
const customerData = findOrCreateCustomerStep({
|
||||
customerId: input.customer_id,
|
||||
email: input.email,
|
||||
})
|
||||
|
||||
const cartInput = transform({ input, region, customerData }, (data) => {
|
||||
const data_ = {
|
||||
...data.input,
|
||||
currency_code: data?.input.currency_code || data.region.currency_code,
|
||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||
region_id: data.region.id,
|
||||
}
|
||||
|
||||
if (data.customerData.customer?.id) {
|
||||
data_.customer_id = data.customerData.customer.id
|
||||
data_.email = data.input?.email ?? data.customerData.customer.email
|
||||
}
|
||||
|
||||
return data_
|
||||
})
|
||||
|
||||
// TODO: Add line items
|
||||
|
||||
const cart = createCartsStep([cartInput])
|
||||
|
||||
return cart
|
||||
return cart[0]
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user