fix: Customer registration (#8896)
* fix: Customer registration * update test * one mroe test * chore: add transformation
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { MedusaError, ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
import { CreateCustomerAccountWorkflowInput } from "../workflows"
|
||||
|
||||
export const validateCustomerAccountCreationStepId =
|
||||
"validate-customer-account-creation"
|
||||
|
||||
export const validateCustomerAccountCreation = createStep(
|
||||
validateCustomerAccountCreationStepId,
|
||||
async (input: CreateCustomerAccountWorkflowInput, { container }) => {
|
||||
const customerService = container.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
|
||||
const { email } = input.customerData
|
||||
|
||||
if (!email) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Email is required to create a customer"
|
||||
)
|
||||
}
|
||||
|
||||
// Check if customer with email already exists
|
||||
const existingCustomers = await customerService.listCustomers({ email })
|
||||
|
||||
if (existingCustomers?.length) {
|
||||
const hasExistingAccount = existingCustomers.some(
|
||||
(customer) => customer.has_account
|
||||
)
|
||||
|
||||
if (hasExistingAccount && input.authIdentityId) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.DUPLICATE_ERROR,
|
||||
"Customer with this email already has an account"
|
||||
)
|
||||
}
|
||||
|
||||
if (!hasExistingAccount && !input.authIdentityId) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.DUPLICATE_ERROR,
|
||||
"Guest customer with this email already exists"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -1,16 +1,17 @@
|
||||
import { CreateCustomerDTO, CustomerDTO } from "@medusajs/types"
|
||||
import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createCustomersStep } from "../steps"
|
||||
import { transform } from "@medusajs/workflows-sdk"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
import { createCustomersStep } from "../steps"
|
||||
import { validateCustomerAccountCreation } from "../steps/validate-customer-account-creation"
|
||||
|
||||
export type CreateCustomerAccountWorkflowInput = {
|
||||
authIdentityId: string
|
||||
customersData: CreateCustomerDTO
|
||||
customerData: CreateCustomerDTO
|
||||
}
|
||||
|
||||
export const createCustomerAccountWorkflowId = "create-customer-account"
|
||||
@@ -19,8 +20,19 @@ export const createCustomerAccountWorkflowId = "create-customer-account"
|
||||
*/
|
||||
export const createCustomerAccountWorkflow = createWorkflow(
|
||||
createCustomerAccountWorkflowId,
|
||||
(input: WorkflowData<CreateCustomerAccountWorkflowInput>): WorkflowResponse<CustomerDTO> => {
|
||||
const customers = createCustomersStep([input.customersData])
|
||||
(
|
||||
input: WorkflowData<CreateCustomerAccountWorkflowInput>
|
||||
): WorkflowResponse<CustomerDTO> => {
|
||||
validateCustomerAccountCreation(input)
|
||||
|
||||
const customerData = transform({ input }, (data) => {
|
||||
return {
|
||||
...data.input.customerData,
|
||||
has_account: !!data.input.authIdentityId,
|
||||
}
|
||||
})
|
||||
|
||||
const customers = createCustomersStep([customerData])
|
||||
|
||||
const customer = transform(
|
||||
customers,
|
||||
|
||||
@@ -199,6 +199,11 @@ export interface CreateCustomerDTO {
|
||||
*/
|
||||
created_by?: string | null
|
||||
|
||||
/**
|
||||
* Whether the customer has an account.
|
||||
*/
|
||||
has_account?: boolean
|
||||
|
||||
/**
|
||||
* The addresses of the customer.
|
||||
*/
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
@@ -6,7 +7,6 @@ import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.AdminGetFulfillmentProvidersParams>,
|
||||
|
||||
@@ -6,6 +6,7 @@ const defaultStoreCustomersFields = [
|
||||
"last_name",
|
||||
"phone",
|
||||
"metadata",
|
||||
"has_account",
|
||||
"created_by",
|
||||
"deleted_at",
|
||||
"created_at",
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
|
||||
import { createCustomerAccountWorkflow } from "@medusajs/core-flows"
|
||||
import { refetchCustomer } from "./helpers"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { refetchCustomer } from "./helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.StoreCreateCustomer>,
|
||||
@@ -21,10 +21,10 @@ export const POST = async (
|
||||
}
|
||||
|
||||
const createCustomers = createCustomerAccountWorkflow(req.scope)
|
||||
const customersData = req.validatedBody
|
||||
const customerData = req.validatedBody
|
||||
|
||||
const { result } = await createCustomers.run({
|
||||
input: { customersData, authIdentityId: req.auth_context.auth_identity_id },
|
||||
input: { customerData, authIdentityId: req.auth_context.auth_identity_id },
|
||||
})
|
||||
|
||||
const customer = await refetchCustomer(
|
||||
|
||||
Reference in New Issue
Block a user