feat(medusa,core-flows,types): adds update promotion rule endpoint + workflow (#6702)

what:

- adds endpoint + workflow to update promotion rule
- adds method in promotion to update promotion rules
This commit is contained in:
Riqwan Thamir
2024-03-14 20:04:53 +00:00
committed by GitHub
parent 7be0a2cf6d
commit e5945479e0
13 changed files with 366 additions and 2 deletions
@@ -5,4 +5,5 @@ export * from "./delete-campaigns"
export * from "./delete-promotions"
export * from "./remove-rules-from-promotions"
export * from "./update-campaigns"
export * from "./update-promotion-rules"
export * from "./update-promotions"
@@ -0,0 +1,48 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
IPromotionModuleService,
UpdatePromotionRulesWorkflowDTO,
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const updatePromotionRulesStepId = "update-promotion-rules"
export const updatePromotionRulesStep = createStep(
updatePromotionRulesStepId,
async (input: UpdatePromotionRulesWorkflowDTO, { container }) => {
const { data } = input
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
const promotionRulesBeforeUpdate = await promotionModule.listPromotionRules(
{ id: data.map((d) => d.id) },
{ relations: ["values"] }
)
const updatedPromotionRules = await promotionModule.updatePromotionRules(
data
)
return new StepResponse(updatedPromotionRules, promotionRulesBeforeUpdate)
},
async (updatedPromotionRules, { container }) => {
if (!updatedPromotionRules?.length) {
return
}
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
await promotionModule.updatePromotionRules(
updatedPromotionRules.map((rule) => ({
id: rule.id,
description: rule.description,
attribute: rule.attribute,
operator: rule.operator,
values: rule.values.map((v) => v.value!),
}))
)
}
)
@@ -5,4 +5,5 @@ export * from "./delete-campaigns"
export * from "./delete-promotions"
export * from "./remove-rules-from-promotions"
export * from "./update-campaigns"
export * from "./update-promotion-rules"
export * from "./update-promotions"
@@ -0,0 +1,13 @@
import { UpdatePromotionRulesWorkflowDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updatePromotionRulesStep } from "../steps"
export const updatePromotionRulesWorkflowId = "update-promotion-rules-workflow"
export const updatePromotionRulesWorkflow = createWorkflow(
updatePromotionRulesWorkflowId,
(
input: WorkflowData<UpdatePromotionRulesWorkflowDTO>
): WorkflowData<void> => {
updatePromotionRulesStep(input)
}
)