feat(core-flows,medusa,types): remove rules from promotion endpoints + workflows (#6696)

This commit is contained in:
Riqwan Thamir
2024-03-14 09:19:05 +01:00
committed by GitHub
parent 640eccd5dd
commit 04a532e5ef
16 changed files with 485 additions and 53 deletions

View File

@@ -39,7 +39,7 @@ export const addRulesToPromotionsStep = createStep(
return new StepResponse(promotionRules, {
id: data.id,
ruleIds: createdPromotionRules.map((pr) => pr.id),
promotionRuleIds: createdPromotionRules.map((pr) => pr.id),
buyRuleIds: createdPromotionBuyRules.map((pr) => pr.id),
targetRuleIds: createdPromotionBuyRules.map((pr) => pr.id),
})
@@ -49,28 +49,24 @@ export const addRulesToPromotionsStep = createStep(
return
}
const { id, ruleIds = [], buyRuleIds = [], targetRuleIds = [] } = data
const {
id,
promotionRuleIds = [],
buyRuleIds = [],
targetRuleIds = [],
} = data
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
ruleIds.length &&
(await promotionModule.removePromotionRules(
id,
ruleIds.map((id) => ({ id }))
))
promotionRuleIds.length &&
(await promotionModule.removePromotionRules(id, promotionRuleIds))
buyRuleIds.length &&
(await promotionModule.removePromotionBuyRules(
id,
buyRuleIds.map((id) => ({ id }))
))
(await promotionModule.removePromotionBuyRules(id, buyRuleIds))
targetRuleIds.length &&
(await promotionModule.removePromotionBuyRules(
id,
targetRuleIds.map((id) => ({ id }))
))
(await promotionModule.removePromotionBuyRules(id, targetRuleIds))
}
)

View File

@@ -3,5 +3,6 @@ export * from "./create-campaigns"
export * from "./create-promotions"
export * from "./delete-campaigns"
export * from "./delete-promotions"
export * from "./remove-rules-from-promotions"
export * from "./update-campaigns"
export * from "./update-promotions"

View File

@@ -0,0 +1,95 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CreatePromotionRuleDTO,
IPromotionModuleService,
PromotionRuleDTO,
RemovePromotionRulesWorkflowDTO,
} from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const removeRulesFromPromotionsStepId = "remove-rules-from-promotions"
export const removeRulesFromPromotionsStep = createStep(
removeRulesFromPromotionsStepId,
async (input: RemovePromotionRulesWorkflowDTO, { container }) => {
const { data, rule_type: ruleType } = input
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
const promotion = await promotionModule.retrieve(data.id, {
relations: [
"rules.values",
"application_method.target_rules.values",
"application_method.buy_rules.values",
],
})
const promotionRulesToCreate: CreatePromotionRuleDTO[] = []
const buyRulesToCreate: CreatePromotionRuleDTO[] = []
const targetRulesToCreate: CreatePromotionRuleDTO[] = []
if (ruleType === RuleType.RULES) {
const rules = promotion.rules!
promotionRulesToCreate.push(...promotionRuleAttribute(rules))
await promotionModule.removePromotionRules(data.id, data.rule_ids)
}
if (ruleType === RuleType.BUY_RULES) {
const rules = promotion.application_method?.buy_rules!
buyRulesToCreate.push(...promotionRuleAttribute(rules))
await promotionModule.removePromotionBuyRules(data.id, data.rule_ids)
}
if (ruleType === RuleType.TARGET_RULES) {
const rules = promotion.application_method?.target_rules!
targetRulesToCreate.push(...promotionRuleAttribute(rules))
await promotionModule.removePromotionTargetRules(data.id, data.rule_ids)
}
return new StepResponse(null, {
id: data.id,
promotionRulesToCreate,
buyRulesToCreate,
targetRulesToCreate,
})
},
async (data, { container }) => {
if (!data) {
return
}
const {
id,
promotionRulesToCreate = [],
buyRulesToCreate = [],
targetRulesToCreate = [],
} = data
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
promotionRulesToCreate.length &&
(await promotionModule.addPromotionRules(id, promotionRulesToCreate))
buyRulesToCreate.length &&
(await promotionModule.addPromotionBuyRules(id, buyRulesToCreate))
targetRulesToCreate.length &&
(await promotionModule.addPromotionBuyRules(id, targetRulesToCreate))
}
)
function promotionRuleAttribute(rules: PromotionRuleDTO[]) {
return rules.map((rule) => ({
description: rule.description!,
attribute: rule.attribute!,
operator: rule.operator!,
values: rule.values!.map((val) => val.value!),
}))
}

View File

@@ -3,5 +3,6 @@ export * from "./create-campaigns"
export * from "./create-promotions"
export * from "./delete-campaigns"
export * from "./delete-promotions"
export * from "./remove-rules-from-promotions"
export * from "./update-campaigns"
export * from "./update-promotions"

View File

@@ -0,0 +1,14 @@
import { RemovePromotionRulesWorkflowDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { removeRulesFromPromotionsStep } from "../steps"
export const removeRulesFromPromotionsWorkflowId =
"remove-rules-from-promotions-workflow"
export const removeRulesFromPromotionsWorkflow = createWorkflow(
removeRulesFromPromotionsWorkflowId,
(
input: WorkflowData<RemovePromotionRulesWorkflowDTO>
): WorkflowData<void> => {
removeRulesFromPromotionsStep(input)
}
)