This commit is contained in:
Adria Navarro 2024-10-30 16:59:40 +01:00
parent 7aad0d6e92
commit b510d04129
1 changed files with 27 additions and 32 deletions

View File

@ -25,7 +25,7 @@ import {
AutomationStepStatus, AutomationStepStatus,
BranchSearchFilters, BranchSearchFilters,
BranchStep, BranchStep,
LogicalOperator, isLogicalSearchOperator,
LoopStep, LoopStep,
UserBindings, UserBindings,
} from "@budibase/types" } from "@budibase/types"
@ -547,54 +547,49 @@ class Orchestrator {
) )
} }
private recurseSearchFilters(
filters: BranchSearchFilters,
processFn: (filter: BranchSearchFilters) => BranchSearchFilters
): BranchSearchFilters {
// Process the current level
filters = processFn(filters)
// Recurse through logical operators
for (const logical of Object.values(LogicalOperator)) {
if (filters[logical]) {
filters[logical]!.conditions = filters[logical]!.conditions.map(
condition => this.recurseSearchFilters(condition, processFn)
)
}
}
return filters
}
private async evaluateBranchCondition( private async evaluateBranchCondition(
conditions: BranchSearchFilters conditions: BranchSearchFilters
): Promise<boolean> { ): Promise<boolean> {
const toFilter: Record<string, any> = {} const toFilter: Record<string, any> = {}
const processedConditions = this.recurseSearchFilters( const recurseSearchFilters = (
conditions, filters: BranchSearchFilters
filter => { ): BranchSearchFilters => {
Object.entries(filter).forEach(([_, value]) => { for (const filterKey of Object.keys(
Object.entries(value).forEach(([field, val]) => { filters
) as (keyof typeof filters)[]) {
if (!filters[filterKey]) {
continue
}
if (isLogicalSearchOperator(filterKey)) {
filters[filterKey].conditions = filters[filterKey].conditions.map(
condition => recurseSearchFilters(condition)
)
} else {
for (const [field, value] of Object.entries(filters[filterKey])) {
const fromContext = processStringSync( const fromContext = processStringSync(
field, field,
this.processContext(this.context) this.processContext(this.context)
) )
toFilter[field] = fromContext toFilter[field] = fromContext
if (typeof val === "string" && findHBSBlocks(val).length > 0) { if (typeof value === "string" && findHBSBlocks(value).length > 0) {
const processedVal = processStringSync( const processedVal = processStringSync(
val, value,
this.processContext(this.context) this.processContext(this.context)
) )
value[field] = processedVal filters[filterKey][field] = processedVal
} }
}) }
}) }
return filter
} }
)
return filters
}
const processedConditions = recurseSearchFilters(conditions)
const result = dataFilters.runQuery([toFilter], processedConditions) const result = dataFilters.runQuery([toFilter], processedConditions)
return result.length > 0 return result.length > 0