feat(customer): admin CRUD endpoints (#6232)
**What** - GET /customers/:id - POST /customers/:id - DELETE /customers/:id - POST /customers Including workflows for each.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export * from "./steps"
|
||||
export * from "./workflows"
|
||||
@@ -0,0 +1,31 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
import { CreateCustomerDTO, ICustomerModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
export const createCustomersStepId = "create-customers"
|
||||
export const createCustomersStep = createStep(
|
||||
createCustomersStepId,
|
||||
async (data: CreateCustomerDTO[], { container }) => {
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const createdCustomers = await service.create(data)
|
||||
|
||||
return new StepResponse(
|
||||
createdCustomers,
|
||||
createdCustomers.map((createdCustomers) => createdCustomers.id)
|
||||
)
|
||||
},
|
||||
async (createdCustomerIds, { container }) => {
|
||||
if (!createdCustomerIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await service.delete(createdCustomerIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
type DeleteCustomerStepInput = string[]
|
||||
|
||||
export const deleteCustomerStepId = "delete-customer"
|
||||
export const deleteCustomerStep = createStep(
|
||||
deleteCustomerStepId,
|
||||
async (ids: DeleteCustomerStepInput, { container }) => {
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await service.softDelete(ids)
|
||||
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevCustomerIds, { container }) => {
|
||||
if (!prevCustomerIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await service.restore(prevCustomerIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./create-customers"
|
||||
export * from "./update-customers"
|
||||
export * from "./delete-customers"
|
||||
@@ -0,0 +1,59 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
FilterableCustomerProps,
|
||||
ICustomerModuleService,
|
||||
CustomerUpdatableFields,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
getSelectsAndRelationsFromObjectArray,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateCustomersStepInput = {
|
||||
selector: FilterableCustomerProps
|
||||
update: CustomerUpdatableFields
|
||||
}
|
||||
|
||||
export const updateCustomersStepId = "update-customer"
|
||||
export const updateCustomersStep = createStep(
|
||||
updateCustomersStepId,
|
||||
async (data: UpdateCustomersStepInput, { container }) => {
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
const prevCustomers = await service.list(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const customers = await service.update(data.selector, data.update)
|
||||
|
||||
return new StepResponse(customers, prevCustomers)
|
||||
},
|
||||
async (prevCustomers, { container }) => {
|
||||
if (!prevCustomers?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ICustomerModuleService>(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
|
||||
await promiseAll(
|
||||
prevCustomers.map((c) =>
|
||||
service.update(c.id, {
|
||||
first_name: c.first_name,
|
||||
last_name: c.last_name,
|
||||
email: c.email,
|
||||
phone: c.phone,
|
||||
metadata: c.metadata,
|
||||
})
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CustomerDTO, CreateCustomerDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createCustomersStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { customersData: CreateCustomerDTO[] }
|
||||
|
||||
export const createCustomersWorkflowId = "create-customers"
|
||||
export const createCustomersWorkflow = createWorkflow(
|
||||
createCustomersWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerDTO[]> => {
|
||||
return createCustomersStep(input.customersData)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteCustomerStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteCustomersWorkflowId = "delete-customers"
|
||||
export const deleteCustomersWorkflow = createWorkflow(
|
||||
deleteCustomersWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteCustomerStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./create-customers"
|
||||
export * from "./update-customers"
|
||||
export * from "./delete-customers"
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
CustomerDTO,
|
||||
CustomerUpdatableFields,
|
||||
FilterableCustomerProps,
|
||||
} from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateCustomersStep } from "../steps"
|
||||
|
||||
type UpdateCustomersStepInput = {
|
||||
selector: FilterableCustomerProps
|
||||
update: CustomerUpdatableFields
|
||||
}
|
||||
|
||||
type WorkflowInput = UpdateCustomersStepInput
|
||||
|
||||
export const updateCustomersWorkflowId = "update-customers"
|
||||
export const updateCustomersWorkflow = createWorkflow(
|
||||
updateCustomersWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerDTO[]> => {
|
||||
return updateCustomersStep(input)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user