feat: customer bulk endpoint form managing customer groups (#9761)
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -2,3 +2,4 @@ export * from "./update-customer-groups"
|
||||
export * from "./delete-customer-groups"
|
||||
export * from "./create-customer-groups"
|
||||
export * from "./link-customers-customer-group"
|
||||
export * from "./link-customer-groups-customer"
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import {
|
||||
ICustomerModuleService,
|
||||
LinkWorkflowInput,
|
||||
} from "@medusajs/framework/types"
|
||||
import { Modules, promiseAll } from "@medusajs/framework/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
export const linkCustomerGroupsToCustomerStepId =
|
||||
"link-customers-to-customer-group"
|
||||
/**
|
||||
* This step creates one or more links between a customer and customer groups records.
|
||||
*/
|
||||
export const linkCustomerGroupsToCustomerStep = createStep(
|
||||
linkCustomerGroupsToCustomerStepId,
|
||||
async (data: LinkWorkflowInput, { container }) => {
|
||||
const service = container.resolve<ICustomerModuleService>(Modules.CUSTOMER)
|
||||
|
||||
const toAdd = (data.add ?? []).map((customerGroupId) => {
|
||||
return {
|
||||
customer_group_id: customerGroupId,
|
||||
customer_id: data.id,
|
||||
}
|
||||
})
|
||||
|
||||
const toRemove = (data.remove ?? []).map((customerGroupId) => {
|
||||
return {
|
||||
customer_group_id: customerGroupId,
|
||||
customer_id: data.id,
|
||||
}
|
||||
})
|
||||
|
||||
const promises: Promise<any>[] = []
|
||||
if (toAdd.length) {
|
||||
promises.push(service.addCustomerToGroup(toAdd))
|
||||
}
|
||||
if (toRemove.length) {
|
||||
promises.push(service.removeCustomerFromGroup(toRemove))
|
||||
}
|
||||
await promiseAll(promises)
|
||||
|
||||
return new StepResponse(void 0, { toAdd, toRemove })
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData) {
|
||||
return
|
||||
}
|
||||
const service = container.resolve<ICustomerModuleService>(Modules.CUSTOMER)
|
||||
|
||||
if (prevData.toAdd.length) {
|
||||
await service.removeCustomerFromGroup(prevData.toAdd)
|
||||
}
|
||||
if (prevData.toRemove.length) {
|
||||
await service.addCustomerToGroup(prevData.toRemove)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -2,3 +2,4 @@ export * from "./update-customer-groups"
|
||||
export * from "./delete-customer-groups"
|
||||
export * from "./create-customer-groups"
|
||||
export * from "./link-customers-customer-group"
|
||||
export * from "./link-customer-groups-customer"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { LinkWorkflowInput } from "@medusajs/framework/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/framework/workflows-sdk"
|
||||
import { linkCustomerGroupsToCustomerStep } from "../steps"
|
||||
|
||||
export const linkCustomerGroupsToCustomerWorkflowId =
|
||||
"link-customer-groups-to-customer"
|
||||
/**
|
||||
* This workflow creates one or more links between a customer and customer groups.
|
||||
*/
|
||||
export const linkCustomerGroupsToCustomerWorkflow = createWorkflow(
|
||||
linkCustomerGroupsToCustomerWorkflowId,
|
||||
(input: WorkflowData<LinkWorkflowInput>): WorkflowData<void> => {
|
||||
return linkCustomerGroupsToCustomerStep(input)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user