feat(pricing,types): price list API + price calculations with price lists (#5498)
**what:** **PriceList Service APIs:** - createPriceList - updatePriceList - addPriceListPrices - removePriceListRules - setPriceListRules - deletePriceList - listPriceLists - listAndCountPriceLists **Price Calculations** - Returns prices with price list prices - Returns a new shape with calculated and original prices Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
15
packages/utils/src/common/array-difference.ts
Normal file
15
packages/utils/src/common/array-difference.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
type ArrayDifferenceElement = string | number
|
||||
|
||||
export function arrayDifference(
|
||||
mainArray: ArrayDifferenceElement[],
|
||||
differingArray: ArrayDifferenceElement[]
|
||||
): ArrayDifferenceElement[] {
|
||||
const mainArraySet = new Set(mainArray)
|
||||
const differingArraySet = new Set(differingArray)
|
||||
|
||||
const difference = [...mainArraySet].filter(
|
||||
(element) => !differingArraySet.has(element)
|
||||
)
|
||||
|
||||
return difference
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
export * from "./array-difference"
|
||||
export * from "./build-query"
|
||||
export * from "./camel-to-snake-case"
|
||||
export * from "./container"
|
||||
export * from "./deduplicate"
|
||||
export * from "./deep-equal-obj"
|
||||
export * from "./errors"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./get-config-file"
|
||||
@@ -24,11 +27,9 @@ export * from "./set-metadata"
|
||||
export * from "./simple-hash"
|
||||
export * from "./string-to-select-relation-object"
|
||||
export * from "./stringify-circular"
|
||||
export * from "./camel-to-snake-case"
|
||||
export * from "./to-camel-case"
|
||||
export * from "./to-kebab-case"
|
||||
export * from "./to-pascal-case"
|
||||
export * from "./transaction"
|
||||
export * from "./upper-case-first"
|
||||
export * from "./wrap-handler"
|
||||
export * from "./deep-equal-obj"
|
||||
|
||||
@@ -1,38 +1,2 @@
|
||||
import { MedusaError } from "../common"
|
||||
|
||||
export const ReservedPricingRuleAttributes = [
|
||||
"quantity",
|
||||
"currency_code",
|
||||
"price_list_id",
|
||||
]
|
||||
|
||||
type RuleAttributeInput = string | undefined
|
||||
|
||||
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(", ")}`
|
||||
)
|
||||
}
|
||||
}
|
||||
export * from "./price-list"
|
||||
export * from "./rule-type"
|
||||
|
||||
9
packages/utils/src/pricing/price-list.ts
Normal file
9
packages/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",
|
||||
}
|
||||
37
packages/utils/src/pricing/rule-type.ts
Normal file
37
packages/utils/src/pricing/rule-type.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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