feat: Completely revamp the pricing module (#7852)
* feat: Completely revamp the pricing module * chore: Update all places to the new pricing interfaces * fix: Remove unnecessary join to itself * chore: Add data migration for existing users * fix: Apply the correct index to price rule
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
import { getInvalidRuleAttributes } from ".."
|
||||
|
||||
describe("getInvalidRuleAttributes", function () {
|
||||
it("should return list of rule attributes that matches reserved keywords", function () {
|
||||
let result = getInvalidRuleAttributes(["shouldnotmatch"])
|
||||
expect(result).toEqual([])
|
||||
|
||||
result = getInvalidRuleAttributes(["currency_code", "shouldnotmatch"])
|
||||
expect(result).toEqual(["currency_code"])
|
||||
|
||||
result = getInvalidRuleAttributes(["currency_code", "price_list_id"])
|
||||
expect(result).toEqual(["currency_code", "price_list_id"])
|
||||
|
||||
result = getInvalidRuleAttributes(["shouldnotmatch", "quantity"])
|
||||
expect(result).toEqual(["quantity"])
|
||||
})
|
||||
})
|
||||
@@ -1,28 +0,0 @@
|
||||
import { validateRuleAttributes } from ".."
|
||||
|
||||
describe("validateRuleAttributes", function () {
|
||||
it("should return void if there are no validation errors", function () {
|
||||
let result = validateRuleAttributes(["shouldpasswithouterrors"])
|
||||
|
||||
expect(result).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should throw an error if one of the array strings matches a reserved keyword", function () {
|
||||
let error
|
||||
|
||||
try {
|
||||
validateRuleAttributes([
|
||||
"currency_code",
|
||||
"shouldnotbepresent",
|
||||
"quantity",
|
||||
"price_list_id",
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Can't create rule_attribute with reserved keywords [quantity, currency_code, price_list_id] - quantity, currency_code, price_list_id"
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -10,11 +10,10 @@ export function buildPriceListRules(
|
||||
priceListRules?: PriceListRuleDTO[]
|
||||
): Record<string, string[]> | undefined {
|
||||
return priceListRules?.reduce((acc, curr) => {
|
||||
const ruleAttribute = curr.rule_type.rule_attribute
|
||||
const ruleValues = curr.price_list_rule_values || []
|
||||
|
||||
acc[ruleAttribute] = ruleValues.map((ruleValue) => ruleValue.value)
|
||||
const ruleAttribute = curr.attribute
|
||||
const ruleValues = curr.value || []
|
||||
|
||||
acc[ruleAttribute] = ruleValues
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
@@ -27,11 +26,10 @@ export function buildPriceSetRules(
|
||||
}
|
||||
|
||||
return priceRules?.reduce((acc, curr) => {
|
||||
const ruleAttribute = curr.rule_type.rule_attribute
|
||||
const ruleAttribute = curr.attribute
|
||||
const ruleValue = curr.value
|
||||
|
||||
acc[ruleAttribute] = ruleValue
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
@@ -2,24 +2,12 @@ import { buildEventNamesFromEntityName } from "../event-bus"
|
||||
import { Modules } from "../modules-sdk"
|
||||
|
||||
const eventBaseNames: [
|
||||
"priceListRuleValue",
|
||||
"priceListRule",
|
||||
"priceList",
|
||||
"priceRule",
|
||||
"priceSetRuleType",
|
||||
"priceSet",
|
||||
"price",
|
||||
"ruleType"
|
||||
] = [
|
||||
"priceListRuleValue",
|
||||
"priceListRule",
|
||||
"priceList",
|
||||
"priceRule",
|
||||
"priceSetRuleType",
|
||||
"priceSet",
|
||||
"price",
|
||||
"ruleType",
|
||||
]
|
||||
"price"
|
||||
] = ["priceListRule", "priceList", "priceRule", "priceSet", "price"]
|
||||
|
||||
export const PricingEvents = buildEventNamesFromEntityName(
|
||||
eventBaseNames,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from "./builders"
|
||||
export * from "./price-list"
|
||||
export * from "./rule-type"
|
||||
export * from "./events"
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
import { MedusaError } from "../common"
|
||||
|
||||
type RuleAttributeInput = string | undefined
|
||||
|
||||
export const ReservedPricingRuleAttributes = [
|
||||
"quantity",
|
||||
"currency_code",
|
||||
"price_list_id",
|
||||
]
|
||||
|
||||
export const getInvalidRuleAttributes = (
|
||||
ruleAttributes: RuleAttributeInput[]
|
||||
): string[] => {
|
||||
const invalidRuleAttributes: string[] = []
|
||||
|
||||
for (const attribute of ReservedPricingRuleAttributes) {
|
||||
if (ruleAttributes.indexOf(attribute) > -1) {
|
||||
invalidRuleAttributes.push(attribute)
|
||||
}
|
||||
}
|
||||
|
||||
return invalidRuleAttributes
|
||||
}
|
||||
|
||||
export const validateRuleAttributes = (
|
||||
ruleAttributes: RuleAttributeInput[]
|
||||
): void => {
|
||||
const invalidRuleAttributes = getInvalidRuleAttributes(ruleAttributes)
|
||||
|
||||
if (invalidRuleAttributes.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Can't create rule_attribute with reserved keywords [${ReservedPricingRuleAttributes.join(
|
||||
", "
|
||||
)}] - ${invalidRuleAttributes.join(", ")}`
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user