chore(): start moving some packages to the core directory (#7215)
This commit is contained in:
committed by
GitHub
parent
fdee748eed
commit
bbccd6481d
@@ -0,0 +1,17 @@
|
||||
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"])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,28 @@
|
||||
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"
|
||||
)
|
||||
})
|
||||
})
|
||||
79
packages/core/utils/src/pricing/builders.ts
Normal file
79
packages/core/utils/src/pricing/builders.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
PriceDTO,
|
||||
PriceListRuleDTO,
|
||||
PriceRuleDTO,
|
||||
ProductVariantDTO,
|
||||
UpdatePriceListPriceDTO,
|
||||
} from "@medusajs/types"
|
||||
|
||||
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)
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
export function buildPriceSetRules(
|
||||
priceRules?: PriceRuleDTO[]
|
||||
): Record<string, string> | undefined {
|
||||
if (typeof priceRules === "undefined") {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return priceRules?.reduce((acc, curr) => {
|
||||
const ruleAttribute = curr.rule_type.rule_attribute
|
||||
const ruleValue = curr.value
|
||||
|
||||
acc[ruleAttribute] = ruleValue
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
export function buildPriceSetPricesForCore(
|
||||
prices: (PriceDTO & {
|
||||
price_set?: PriceDTO["price_set"] & {
|
||||
variant?: ProductVariantDTO
|
||||
}
|
||||
})[]
|
||||
): Record<string, any>[] {
|
||||
return prices?.map((price) => {
|
||||
const productVariant = (price.price_set as any)?.variant
|
||||
const rules: Record<string, string> | undefined =
|
||||
typeof price.price_rules === "undefined"
|
||||
? undefined
|
||||
: buildPriceSetRules(price.price_rules || [])
|
||||
|
||||
delete price.price_rules
|
||||
delete price.price_set
|
||||
|
||||
return {
|
||||
...price,
|
||||
variant_id: productVariant?.id ?? undefined,
|
||||
rules,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function buildPriceSetPricesForModule(
|
||||
prices: PriceDTO[]
|
||||
): UpdatePriceListPriceDTO[] {
|
||||
return prices?.map((price) => {
|
||||
const rules: Record<string, string> | undefined =
|
||||
typeof price.price_rules === "undefined"
|
||||
? undefined
|
||||
: buildPriceSetRules(price.price_rules || [])
|
||||
|
||||
return {
|
||||
...price,
|
||||
price_set_id: price.price_set!?.id!,
|
||||
rules,
|
||||
}
|
||||
})
|
||||
}
|
||||
3
packages/core/utils/src/pricing/index.ts
Normal file
3
packages/core/utils/src/pricing/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./builders"
|
||||
export * from "./price-list"
|
||||
export * from "./rule-type"
|
||||
9
packages/core/utils/src/pricing/price-list.ts
Normal file
9
packages/core/utils/src/pricing/price-list.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export enum PriceListStatus {
|
||||
ACTIVE = "active",
|
||||
DRAFT = "draft",
|
||||
}
|
||||
|
||||
export enum PriceListType {
|
||||
SALE = "sale",
|
||||
OVERRIDE = "override",
|
||||
}
|
||||
38
packages/core/utils/src/pricing/rule-type.ts
Normal file
38
packages/core/utils/src/pricing/rule-type.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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