feat(core-flows,types,medusa): API to add promotions to campaign (#7277)

what:

- adds an API to add promotions to campaign
- reworks module to perform atomic actions
This commit is contained in:
Riqwan Thamir
2024-05-10 09:53:56 +02:00
committed by GitHub
parent 489a54e1fb
commit 9a14aeebcf
31 changed files with 1033 additions and 939 deletions

View File

@@ -0,0 +1,41 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService, LinkWorkflowInput } from "@medusajs/types"
import { StepResponse, WorkflowData, createStep } from "@medusajs/workflows-sdk"
export const addCampaignPromotionsStepId = "add-campaign-promotions"
export const addCampaignPromotionsStep = createStep(
addCampaignPromotionsStepId,
async (input: WorkflowData<LinkWorkflowInput>, { container }) => {
const { id: campaignId, add: promotionIdsToAdd = [] } = input
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
if (promotionIdsToAdd.length) {
await promotionModule.addPromotionsToCampaign({
id: campaignId,
promotion_ids: promotionIdsToAdd,
})
}
return new StepResponse(null, input)
},
async (data, { container }) => {
if (!data) {
return
}
const { id: campaignId, add: promotionIdsToRemove = [] } = data
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
if (promotionIdsToRemove.length) {
await promotionModule.removePromotionsFromCampaign({
id: campaignId,
promotion_ids: promotionIdsToRemove,
})
}
}
)

View File

@@ -1,8 +1,10 @@
export * from "./add-campaign-promotions"
export * from "./add-rules-to-promotions"
export * from "./create-campaigns"
export * from "./create-promotions"
export * from "./delete-campaigns"
export * from "./delete-promotions"
export * from "./remove-campaign-promotions"
export * from "./remove-rules-from-promotions"
export * from "./update-campaigns"
export * from "./update-promotion-rules"

View File

@@ -0,0 +1,40 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService, LinkWorkflowInput } from "@medusajs/types"
import { StepResponse, WorkflowData, createStep } from "@medusajs/workflows-sdk"
export const removeCampaignPromotionsStepId = "remove-campaign-promotions"
export const removeCampaignPromotionsStep = createStep(
removeCampaignPromotionsStepId,
async (input: WorkflowData<LinkWorkflowInput>, { container }) => {
const { id: campaignId, remove: promotionIdsToRemove = [] } = input
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
if (promotionIdsToRemove.length) {
await promotionModule.removePromotionsFromCampaign({
id: campaignId,
promotion_ids: promotionIdsToRemove,
})
}
return new StepResponse(null, input)
},
async (data, { container }) => {
if (!data) {
return
}
const { id: campaignId, remove: promotionIdsToAdd = [] } = data
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
if (promotionIdsToAdd.length) {
await promotionModule.addPromotionsToCampaign({
id: campaignId,
promotion_ids: promotionIdsToAdd,
})
}
}
)

View File

@@ -0,0 +1,16 @@
import { LinkWorkflowInput } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import {
addCampaignPromotionsStep,
removeCampaignPromotionsStep,
} from "../steps"
export const addOrRemoveCampaignPromotionsWorkflowId =
"add-or-remove-campaign-promotions"
export const addOrRemoveCampaignPromotionsWorkflow = createWorkflow(
addOrRemoveCampaignPromotionsWorkflowId,
(input: WorkflowData<LinkWorkflowInput>): WorkflowData<void> => {
addCampaignPromotionsStep(input)
removeCampaignPromotionsStep(input)
}
)

View File

@@ -1,10 +1,11 @@
export * from "./add-or-remove-campaign-promotions"
export * from "./batch-promotion-rules"
export * from "./create-campaigns"
export * from "./create-promotion-rules"
export * from "./create-promotions"
export * from "./delete-campaigns"
export * from "./delete-promotion-rules"
export * from "./delete-promotions"
export * from "./update-campaigns"
export * from "./update-promotion-rules"
export * from "./delete-promotion-rules"
export * from "./create-promotion-rules"
export * from "./update-promotions"