Files
medusa-store/packages/medusa/src/api/admin/promotions/utils/validate-rule-attribute.ts
William Bouchard 6602e893b8 chore(medusa): fetch shipping related attributes (#13244)
* chore(medusa): fet shipping related attributes

* some tests

* changeset

* small refactor

* typing

* adapt attribute value

* test

* test again
2025-08-20 13:03:27 -04:00

41 lines
1.1 KiB
TypeScript

import { MedusaError } from "@medusajs/framework/utils"
import { getRuleAttributesMap } from "./rule-attributes-map"
import {
ApplicationMethodTargetTypeValues,
ApplicationMethodTypeValues,
PromotionTypeValues,
RuleTypeValues,
} from "@medusajs/types"
export function validateRuleAttribute(attributes: {
promotionType: PromotionTypeValues | undefined
ruleType: RuleTypeValues
ruleAttributeId: string
applicationMethodType?: ApplicationMethodTypeValues
applicationMethodTargetType?: ApplicationMethodTargetTypeValues
}) {
const {
promotionType,
ruleType,
ruleAttributeId,
applicationMethodType,
applicationMethodTargetType,
} = attributes
const ruleAttributes =
getRuleAttributesMap({
promotionType,
applicationMethodType,
applicationMethodTargetType,
})[ruleType] || []
const ruleAttribute = ruleAttributes.find((obj) => obj.id === ruleAttributeId)
if (!ruleAttribute) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Invalid rule attribute - ${ruleAttributeId}`
)
}
}