-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-flows,medusa,types): remove rules from promotion endpoints …
…+ workflows (#6696)
- Loading branch information
Showing
16 changed files
with
485 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@medusajs/core-flows": patch | ||
"@medusajs/medusa": patch | ||
"@medusajs/types": patch | ||
--- | ||
|
||
feat(core-flows,medusa,types): remove rules from promotion endpoints + workflows |
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
95 changes: 95 additions & 0 deletions
95
packages/core-flows/src/promotion/steps/remove-rules-from-promotions.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,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!), | ||
})) | ||
} |
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
14 changes: 14 additions & 0 deletions
14
packages/core-flows/src/promotion/workflows/remove-rules-from-promotions.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,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) | ||
} | ||
) |
Oops, something went wrong.