feat(pricing,types,utils): Move calculate pricing query to a repository + rule type validation (#5294)

This commit is contained in:
Riqwan Thamir
2023-10-10 17:05:19 +02:00
committed by GitHub
parent b62af612c7
commit 378ca1b36e
16 changed files with 273 additions and 92 deletions

View File

@@ -6,14 +6,14 @@ import {
} from "@medusajs/types"
import { isString } from "../../common"
import { MedusaContext } from "../../decorators"
import { buildQuery, InjectTransactionManager } from "../../modules-sdk"
import { InjectTransactionManager, buildQuery } from "../../modules-sdk"
import {
getSoftDeletedCascadedEntitiesIdsMappedBy,
transactionWrapper,
} from "../utils"
import { mikroOrmSerializer, mikroOrmUpdateDeletedAtRecursively } from "./utils"
class MikroOrmBase<T = any> {
export class MikroOrmBase<T = any> {
protected readonly manager_: any
protected constructor({ manager }) {

View File

@@ -5,6 +5,7 @@ export * from "./decorators"
export * from "./event-bus"
export * from "./feature-flags"
export * from "./modules-sdk"
export * from "./pricing"
export * from "./product"
export * from "./search"
export * from "./shipping"

View File

@@ -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"])
})
})

View File

@@ -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"
)
})
})

View File

@@ -0,0 +1,38 @@
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(", ")}`
)
}
}