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:
Frane Polić
2024-10-25 11:55:57 +02:00
committed by GitHub
co-authored by Oli Juhl
parent bb6d7c6641
commit 259d050e53
12 changed files with 271 additions and 32 deletions
@@ -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)
}
)
@@ -210,4 +210,38 @@ export class Customer {
}
)
}
/**
* This method manages customer groups for a customer.
* It sends a request to the [Manage Customers](https://docs.medusajs.com/api/admin#customers_postcustomersidcustomergroups)
* API route.
*
* @param id - The customer's ID.
* @param body - The groups to add customer to or remove customer from.
* @param headers - Headers to pass in the request
* @returns The customers details.
*
* @example
* sdk.admin.customer.batchCustomerGroups("cus_123", {
* add: ["cusgroup_123"],
* remove: ["cusgroup_321"]
* })
* .then(({ customer }) => {
* console.log(customer)
* })
*/
async batchCustomerGroups(
id: string,
body: HttpTypes.AdminBatchLink,
headers?: ClientHeaders
) {
return await this.client.fetch<HttpTypes.AdminCustomerResponse>(
`/admin/customers/${id}/customer-groups`,
{
method: "POST",
headers,
body,
}
)
}
}