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:
@@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -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"
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -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)
|
||||
}
|
||||
)
|
||||
@@ -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"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { BaseFilterable } from "../../dal"
|
||||
import { CampaignBudgetDTO } from "./campaign-budget"
|
||||
import { PromotionDTO } from "./promotion"
|
||||
|
||||
/**
|
||||
* The campaign details.
|
||||
@@ -44,6 +45,11 @@ export interface CampaignDTO {
|
||||
* The associated campaign budget.
|
||||
*/
|
||||
budget?: CampaignBudgetDTO
|
||||
|
||||
/**
|
||||
* The associated promotions.
|
||||
*/
|
||||
promotions?: PromotionDTO[]
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -133,7 +133,7 @@ export interface UpdatePromotionDTO {
|
||||
/**
|
||||
* The associated campaign's ID.
|
||||
*/
|
||||
campaign_id?: string
|
||||
campaign_id?: string | null
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { CampaignBudgetTypeValues, PromotionDTO } from "./common"
|
||||
import { CampaignBudgetTypeValues } from "./common"
|
||||
|
||||
/**
|
||||
* The campaign budget to be created.
|
||||
@@ -83,11 +83,6 @@ export interface CreateCampaignDTO {
|
||||
* The associated campaign budget.
|
||||
*/
|
||||
budget?: CreateCampaignBudgetDTO
|
||||
|
||||
/**
|
||||
* The promotions of the campaign.
|
||||
*/
|
||||
promotions?: Pick<PromotionDTO, "id">[]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,9 +128,28 @@ export interface UpdateCampaignDTO {
|
||||
* The budget of the campaign.
|
||||
*/
|
||||
budget?: Omit<UpdateCampaignBudgetDTO, "id">
|
||||
}
|
||||
|
||||
export interface AddPromotionsToCampaignDTO {
|
||||
/**
|
||||
* The ID of the campaign.
|
||||
*/
|
||||
id: string
|
||||
|
||||
/**
|
||||
* The promotions of the campaign.
|
||||
* Ids of promotions to add
|
||||
*/
|
||||
promotions?: Pick<PromotionDTO, "id">[]
|
||||
promotion_ids: string[]
|
||||
}
|
||||
|
||||
export interface RemovePromotionsFromCampaignDTO {
|
||||
/**
|
||||
* The ID of the campaign.
|
||||
*/
|
||||
id: string
|
||||
|
||||
/**
|
||||
* Ids of promotions to add
|
||||
*/
|
||||
promotion_ids: string[]
|
||||
}
|
||||
|
||||
@@ -16,7 +16,12 @@ import {
|
||||
UpdatePromotionDTO,
|
||||
UpdatePromotionRuleDTO,
|
||||
} from "./common"
|
||||
import { CreateCampaignDTO, UpdateCampaignDTO } from "./mutations"
|
||||
import {
|
||||
AddPromotionsToCampaignDTO,
|
||||
CreateCampaignDTO,
|
||||
RemovePromotionsFromCampaignDTO,
|
||||
UpdateCampaignDTO,
|
||||
} from "./mutations"
|
||||
|
||||
/**
|
||||
* The main service interface for the Promotion Module.
|
||||
@@ -967,4 +972,14 @@ export interface IPromotionModuleService extends IModuleService {
|
||||
config?: RestoreReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | void>
|
||||
|
||||
addPromotionsToCampaign(
|
||||
data: AddPromotionsToCampaignDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<{ ids: string[] }>
|
||||
|
||||
removePromotionsFromCampaign(
|
||||
data: RemovePromotionsFromCampaignDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<{ ids: string[] }>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user