forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: customer bulk endpoint form managing customer groups (medusajs#…
…9761) Co-authored-by: Oli Juhl <[email protected]>
- Loading branch information
1 parent
bb6d7c6
commit 259d050
Showing
12 changed files
with
271 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/core/core-flows/src/customer-group/steps/link-customer-groups-customer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/core/core-flows/src/customer-group/workflows/link-customer-groups-customer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/medusa/src/api/admin/customers/[id]/customer-groups/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { linkCustomerGroupsToCustomerWorkflow } from "@medusajs/core-flows" | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "@medusajs/framework/http" | ||
|
||
import { HttpTypes, LinkMethodRequest } from "@medusajs/framework/types" | ||
|
||
import { refetchCustomer } from "../../helpers" | ||
|
||
export const POST = async ( | ||
req: AuthenticatedMedusaRequest<LinkMethodRequest>, | ||
res: MedusaResponse<HttpTypes.AdminCustomerResponse> | ||
) => { | ||
const { id } = req.params | ||
const { add, remove } = req.validatedBody | ||
|
||
const workflow = linkCustomerGroupsToCustomerWorkflow(req.scope) | ||
await workflow.run({ | ||
input: { | ||
id, | ||
add, | ||
remove, | ||
}, | ||
}) | ||
|
||
const customer = await refetchCustomer( | ||
id, | ||
req.scope, | ||
req.remoteQueryConfig.fields | ||
) | ||
|
||
res.status(200).json({ customer: customer }) | ||
} |
Oops, something went wrong.