feat(customer): add customer group management apis (#6233)

**What**
```
POST /admin/customer-groups
POST /admin/customer-groups/:id
GET /admin/customer-groups/:id
DELETE /admin/customer-groups/:id
```

- Workflows
This commit is contained in:
Sebastian Rindom
2024-01-30 19:37:53 +00:00
committed by GitHub
parent 8c7a031090
commit ca0e0631af
20 changed files with 540 additions and 20 deletions
@@ -0,0 +1,2 @@
export * from "./workflows"
export * from "./steps"
@@ -0,0 +1,33 @@
import { CreateCustomerGroupDTO, ICustomerModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const createCustomerGroupsStepId = "create-customer-groups"
export const createCustomerGroupsStep = createStep(
createCustomerGroupsStepId,
async (data: CreateCustomerGroupDTO[], { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const createdCustomerGroups = await service.createCustomerGroup(data)
return new StepResponse(
createdCustomerGroups,
createdCustomerGroups.map(
(createdCustomerGroups) => createdCustomerGroups.id
)
)
},
async (createdCustomerGroupIds, { container }) => {
if (!createdCustomerGroupIds?.length) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await service.delete(createdCustomerGroupIds)
}
)
@@ -0,0 +1,30 @@
import { ICustomerModuleService } from "@medusajs/types"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
type DeleteCustomerGroupStepInput = string[]
export const deleteCustomerGroupStepId = "delete-customer-groups"
export const deleteCustomerGroupStep = createStep(
deleteCustomerGroupStepId,
async (ids: DeleteCustomerGroupStepInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await service.softDeleteCustomerGroup(ids)
return new StepResponse(void 0, ids)
},
async (prevCustomerGroups, { container }) => {
if (!prevCustomerGroups) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await service.restoreCustomerGroup(prevCustomerGroups)
}
)
@@ -0,0 +1,3 @@
export * from "./update-customer-groups"
export * from "./delete-customer-groups"
export * from "./create-customer-groups"
@@ -0,0 +1,58 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
FilterableCustomerGroupProps,
ICustomerModuleService,
CustomerGroupUpdatableFields,
} from "@medusajs/types"
import {
getSelectsAndRelationsFromObjectArray,
promiseAll,
} from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
type UpdateCustomerGroupStepInput = {
selector: FilterableCustomerGroupProps
update: CustomerGroupUpdatableFields
}
export const updateCustomerGroupStepId = "update-customer-groups"
export const updateCustomerGroupsStep = createStep(
updateCustomerGroupStepId,
async (data: UpdateCustomerGroupStepInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
data.update,
])
const prevCustomerGroups = await service.listCustomerGroups(data.selector, {
select: selects,
relations,
})
const customers = await service.updateCustomerGroup(
data.selector,
data.update
)
return new StepResponse(customers, prevCustomerGroups)
},
async (prevCustomerGroups, { container }) => {
if (!prevCustomerGroups) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await promiseAll(
prevCustomerGroups.map((c) =>
service.updateCustomerGroup(c.id, {
name: c.name,
})
)
)
}
)
@@ -0,0 +1,13 @@
import { CustomerGroupDTO, CreateCustomerGroupDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createCustomerGroupsStep } from "../steps"
type WorkflowInput = { customersData: CreateCustomerGroupDTO[] }
export const createCustomerGroupsWorkflowId = "create-customer-groups"
export const createCustomerGroupsWorkflow = createWorkflow(
createCustomerGroupsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerGroupDTO[]> => {
return createCustomerGroupsStep(input.customersData)
}
)
@@ -0,0 +1,12 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteCustomerGroupStep } from "../steps"
type WorkflowInput = { ids: string[] }
export const deleteCustomerGroupsWorkflowId = "delete-customer-groups"
export const deleteCustomerGroupsWorkflow = createWorkflow(
deleteCustomerGroupsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteCustomerGroupStep(input.ids)
}
)
@@ -0,0 +1,3 @@
export * from "./update-customer-groups"
export * from "./delete-customer-groups"
export * from "./create-customer-groups"
@@ -0,0 +1,20 @@
import {
CustomerGroupDTO,
FilterableCustomerGroupProps,
CustomerGroupUpdatableFields,
} from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateCustomerGroupsStep } from "../steps"
type WorkflowInput = {
selector: FilterableCustomerGroupProps
update: CustomerGroupUpdatableFields
}
export const updateCustomerGroupsWorkflowId = "update-customer-groups"
export const updateCustomerGroupsWorkflow = createWorkflow(
updateCustomerGroupsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerGroupDTO[]> => {
return updateCustomerGroupsStep(input)
}
)
+1
View File
@@ -3,3 +3,4 @@ export * from "./definitions"
export * as Handlers from "./handlers"
export * from "./promotion"
export * from "./customer"
export * from "./customer-group"