feat(customer): Add create and retrieve customer from store side (#6267)

**What**
- GET /store/customers/me
- POST /store/customers
- Workflow for customer account creation
- Authentication middleware on customer routes
This commit is contained in:
Sebastian Rindom
2024-01-31 11:58:29 +00:00
committed by GitHub
parent f41877ef61
commit 7903a15e0f
33 changed files with 421 additions and 16 deletions
+1
View File
@@ -0,0 +1 @@
export * from "./steps"
@@ -0,0 +1 @@
export * from "./set-auth-app-metadata"
@@ -0,0 +1,59 @@
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { IAuthModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { isDefined } from "@medusajs/utils"
type StepInput = {
authUserId: string
key: string
value: string
}
export const setAuthAppMetadataStepId = "set-auth-app-metadata"
export const setAuthAppMetadataStep = createStep(
setAuthAppMetadataStepId,
async (data: StepInput, { container }) => {
const service = container.resolve<IAuthModuleService>(
ModuleRegistrationName.AUTH
)
const authUser = await service.retrieveAuthUser(data.authUserId)
const appMetadata = authUser.app_metadata || {}
if (isDefined(appMetadata[data.key])) {
throw new Error(`Key ${data.key} already exists in app metadata`)
}
appMetadata[data.key] = data.value
await service.updateAuthUser({
id: authUser.id,
app_metadata: appMetadata,
})
return new StepResponse(authUser, { id: authUser.id, key: data.key })
},
async (idAndKey, { container }) => {
if (!idAndKey) {
return
}
const { id, key } = idAndKey
const service = container.resolve<IAuthModuleService>(
ModuleRegistrationName.AUTH
)
const authUser = await service.retrieveAuthUser(id)
const appMetadata = authUser.app_metadata || {}
if (isDefined(appMetadata[key])) {
delete appMetadata[key]
}
await service.updateAuthUser({
id: authUser.id,
app_metadata: appMetadata,
})
}
)
@@ -4,9 +4,9 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk"
type DeleteCustomerStepInput = string[]
export const deleteCustomerStepId = "delete-customer"
export const deleteCustomerStep = createStep(
deleteCustomerStepId,
export const deleteCustomersStepId = "delete-customers"
export const deleteCustomersStep = createStep(
deleteCustomersStepId,
async (ids: DeleteCustomerStepInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
@@ -0,0 +1,31 @@
import { CreateCustomerDTO, CustomerDTO } from "@medusajs/types"
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
import { createCustomersStep } from "../steps"
import { setAuthAppMetadataStep } from "../../auth/steps"
import { transform } from "@medusajs/workflows-sdk"
type WorkflowInput = {
authUserId: string
customersData: CreateCustomerDTO
}
export const createCustomerAccountWorkflowId = "create-customer-account"
export const createCustomerAccountWorkflow = createWorkflow(
createCustomerAccountWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerDTO> => {
const customers = createCustomersStep([input.customersData])
const customer = transform(
customers,
(customers: CustomerDTO[]) => customers[0]
)
setAuthAppMetadataStep({
authUserId: input.authUserId,
key: "customer_id",
value: customer.id,
})
return customer
}
)
@@ -1,5 +1,5 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteCustomerStep } from "../steps"
import { deleteCustomersStep } from "../steps"
type WorkflowInput = { ids: string[] }
@@ -7,6 +7,6 @@ export const deleteCustomersWorkflowId = "delete-customers"
export const deleteCustomersWorkflow = createWorkflow(
deleteCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteCustomerStep(input.ids)
return deleteCustomersStep(input.ids)
}
)
@@ -1,6 +1,7 @@
export * from "./create-customers"
export * from "./update-customers"
export * from "./delete-customers"
export * from "./create-customer-account"
export * from "./create-addresses"
export * from "./update-addresses"
export * from "./delete-addresses"