feat(types,utils): added promotion create with rules and application target rules (#5957)

* feat(types,utils): added promotion create with rules

* chore: add rules to promotion and application method

* chore: use common code for rule and values

* chore: address pr reviews

* chore: fix test
This commit is contained in:
Riqwan Thamir
2024-01-03 09:54:48 +01:00
committed by GitHub
parent d16d10619d
commit 42cc8ae3f8
33 changed files with 1560 additions and 122 deletions
@@ -1 +1,2 @@
export * from "./application-method"
export * from "./promotion-rule"
@@ -0,0 +1,39 @@
import { PromotionRuleOperatorValues } from "@medusajs/types"
import { MedusaError, PromotionRuleOperator, isPresent } from "@medusajs/utils"
import { CreatePromotionRuleDTO } from "../../types"
export function validatePromotionRuleAttributes(
promotionRulesData: CreatePromotionRuleDTO[]
) {
const errors: string[] = []
for (const promotionRuleData of promotionRulesData) {
if (!isPresent(promotionRuleData.attribute)) {
errors.push("rules[].attribute is a required field")
}
if (!isPresent(promotionRuleData.operator)) {
errors.push("rules[].operator is a required field")
}
if (isPresent(promotionRuleData.operator)) {
const allowedOperators: PromotionRuleOperatorValues[] = Object.values(
PromotionRuleOperator
)
if (!allowedOperators.includes(promotionRuleData.operator)) {
errors.push(
`rules[].operator (${
promotionRuleData.operator
}) is invalid. It should be one of ${allowedOperators.join(", ")}`
)
}
} else {
errors.push("rules[].operator is a required field")
}
}
if (!errors.length) return
throw new MedusaError(MedusaError.Types.INVALID_DATA, errors.join(", "))
}