* ../../core/types/src/dml/index.ts * ../../core/types/src/dml/index.ts * fix: relationships mapping * handle nullable foreign keys types * handle nullable foreign keys types * handle nullable foreign keys types * continue to update product category repository * fix all product category repositories issues * fix product category service types * fix product module service types * fix product module service types * fix repository template type * refactor: use a singleton DMLToMikroORM factory instance Since the MikroORM MetadataStorage is global, we will also have to turn DML to MikroORM entities conversion use a global bucket as well * refactor: update product module to use DML in tests * wip: tests * WIP product linkable fixes * continue type fixing and start test fixing * test: fix more tests * fix repository * fix pivot table computaion + fix mikro orm repository * fix many to many management and configuration * fix many to many management and configuration * fix many to many management and configuration * update product tag relation configuration * Introduce experimental dml hooks to fix some issues with categories * more fixes * fix product tests * add missing id prefixes * fix product category handle management * test: fix more failing tests * test: make it all green * test: fix breaking tests * fix: build issues * fix: build issues * fix: more breaking tests * refactor: fix issues after merge * refactor: fix issues after merge * refactor: surpress types error * test: fix DML failing tests * improve many to many inference + tests * Wip fix columns from product entity * remove product model before create hook and manage handle validation and transformation at the service level * test: fix breaking unit tests * fix: product module service to not update handle on product update * fix define link and joiner config * test: fix joiner config test * test: fix joiner config test * fix joiner config primary keys * Fix joiner config builder * Fix joiner config builder * test: remove only modifier from test * refactor: remove hooks usage from product collection * refactor: remove hooks usage from product-option * refactor: remove hooks usage for computing category handle * refactor: remove hooks usage from productCategory model * refactor: remove hooks from DML * refactor: remove cruft * order dml * cleanup * re add foerign key indexes * wip * chore: remove unused types * wip * changes * rm raw * autoincrement * wip * rel * refactor: cleanup * migration and models configuration adjustments * cleanup * number searchable * fix random ordering * fix * test: fix product-category tests * test: update breaking DML tests * test: array assertion to not care about ordering * fix: temporarily apply id ordering for products * types * wip * WIP type improvements * update order models * partially fix types temporarely * rel * fix: recursive type issue * improve type inference breaks * improve type inference breaks * update models * rm nullable * default value * repository * update default value handling * fix unit tests * WIP * toMikroORM * fix relations * cascades * fix * experimental dml hooks * rm migration * serial * nullable autoincrement * fix model * model changes * fix one to one DML * order test * fix addresses * fix unit tests * Re align dml entity name inference * update model table name config * update model table name config * revert * update return relation * WIP * hasOne * models * fix * model * initial commit * cart service * order module * utils unit test * index engine * changeset * merge * fix hasOne with fk * update * free text filter per entity * tests * prod category * property string many to many * fix big number * link modules migration set names * merge * shipping option rules * serializer * unit test * fix test mikro orm init * fix test mikro orm init * Maintain merge object properties * fix test mikro orm init * prevent unit test from connecting to db * wip * fix test * fix test * link test * schema * models * auto increment * hook * model hooks * order * wip * orm version * request return field * fix return configuration on order model * workflows * core flows * unit test * test * base repo * test * base repo * test fix * inventory move * locking inventory * test * free text fix * rm timeout mock * migrate fulfillment values * v6.4.3 * cleanup * link-modules update sql * revert test * remove fake timers --------- Co-authored-by: adrien2p <adrien.deperetti@gmail.com> Co-authored-by: Harminder Virk <virk.officials@gmail.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
174 lines
4.8 KiB
TypeScript
174 lines
4.8 KiB
TypeScript
import {
|
|
MedusaError,
|
|
RuleOperator,
|
|
isObject,
|
|
isString,
|
|
pickValueFromObject,
|
|
} from "@medusajs/framework/utils"
|
|
|
|
/**
|
|
* The rule engine here is kept inside the module as of now, but it could be moved
|
|
* to the utils package and be used across the different modules that provides context
|
|
* based rule filtering.
|
|
*
|
|
* TODO: discussion around that should happen at some point
|
|
*/
|
|
|
|
export type Rule = {
|
|
attribute: string
|
|
operator: Lowercase<keyof typeof RuleOperator> | (string & {})
|
|
value: string | string[] | null
|
|
}
|
|
|
|
export const availableOperators = Object.values(RuleOperator)
|
|
|
|
const isDate = (str: string) => {
|
|
return !isNaN(Date.parse(str))
|
|
}
|
|
|
|
const operatorsPredicate = {
|
|
in: (contextValue: string, ruleValue: string[]) =>
|
|
ruleValue.includes(contextValue),
|
|
nin: (contextValue: string, ruleValue: string[]) =>
|
|
!ruleValue.includes(contextValue),
|
|
eq: (contextValue: string, ruleValue: string) => contextValue === ruleValue,
|
|
ne: (contextValue: string, ruleValue: string) => contextValue !== ruleValue,
|
|
gt: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) > new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) > Number(ruleValue)
|
|
},
|
|
gte: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) >= new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) >= Number(ruleValue)
|
|
},
|
|
lt: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) < new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) < Number(ruleValue)
|
|
},
|
|
lte: (contextValue: string, ruleValue: string) => {
|
|
if (isDate(contextValue) && isDate(ruleValue)) {
|
|
return new Date(contextValue) <= new Date(ruleValue)
|
|
}
|
|
return Number(contextValue) <= Number(ruleValue)
|
|
},
|
|
}
|
|
|
|
/**
|
|
* Validate contextValue context object from contextValue set of rules.
|
|
* By default, all rules must be valid to return true unless the option atLeastOneValidRule is set to true.
|
|
* @param context
|
|
* @param rules
|
|
* @param options
|
|
*/
|
|
export function isContextValid(
|
|
context: Record<string, any>,
|
|
rules: Rule[],
|
|
options: {
|
|
someAreValid: boolean
|
|
} = {
|
|
someAreValid: false,
|
|
}
|
|
): boolean {
|
|
const { someAreValid } = options
|
|
|
|
const loopComparator = someAreValid ? rules.some : rules.every
|
|
const predicate = (rule) => {
|
|
const { attribute, operator, value } = rule
|
|
const contextValue = pickValueFromObject(attribute, context)
|
|
|
|
return operatorsPredicate[operator](
|
|
`${contextValue}`,
|
|
value as string & string[]
|
|
)
|
|
}
|
|
|
|
return loopComparator.apply(rules, [predicate])
|
|
}
|
|
|
|
/**
|
|
* Validate contextValue rule object
|
|
* @param rule
|
|
*/
|
|
export function validateRule(rule: Record<string, unknown>): boolean {
|
|
if (!rule.attribute || !rule.operator || !rule.value) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule must have an attribute, an operator and a value"
|
|
)
|
|
}
|
|
|
|
if (!isString(rule.attribute)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule attribute must be a string"
|
|
)
|
|
}
|
|
|
|
if (!isString(rule.operator)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule operator must be a string"
|
|
)
|
|
}
|
|
|
|
if (!availableOperators.includes(rule.operator as RuleOperator)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
`Rule operator ${
|
|
rule.operator
|
|
} is not supported. Must be one of ${availableOperators.join(", ")}`
|
|
)
|
|
}
|
|
|
|
if (rule.operator === RuleOperator.IN || rule.operator === RuleOperator.NIN) {
|
|
if (!Array.isArray(rule.value)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Rule value must be an array for in/nin operators"
|
|
)
|
|
}
|
|
} else {
|
|
if (Array.isArray(rule.value) || isObject(rule.value)) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
`Rule value must be a string, bool, number value for the selected operator ${rule.operator}`
|
|
)
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
export function normalizeRulesValue<T extends Partial<Rule>>(rules: T[]): void {
|
|
rules.forEach((rule: any) => {
|
|
/**
|
|
* If a boolean is provided, then we convert to string
|
|
*/
|
|
if (rule.value === true || rule.value === false) {
|
|
rule.value = rule.value === true ? "true" : "false"
|
|
}
|
|
|
|
return rule
|
|
})
|
|
}
|
|
|
|
export function validateAndNormalizeRules<T extends Partial<Rule>>(rules: T[]) {
|
|
rules.forEach(validateRule)
|
|
normalizeRulesValue(rules)
|
|
}
|
|
|
|
/**
|
|
* Validate contextValue set of rules
|
|
* @param rules
|
|
*/
|
|
export function validateRules(rules: Record<string, unknown>[]): boolean {
|
|
rules.forEach(validateRule)
|
|
return true
|
|
}
|