generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,103 @@ | ||
import * as xp from "@dhis2/expression-parser"; | ||
import _c from "../../domain/entities/generic/Collection"; | ||
|
||
export type D2ExpressionParserProgramRuleVariableTypes = | ||
| "text" | ||
| "number" | ||
| "date" | ||
| "datetime" | ||
| "boolean" | ||
| "select"; | ||
export type ProgramRuleVariableType = "text" | "number" | "date" | "boolean"; | ||
|
||
export type D2ExpressionParserProgramRuleVariableName = string; | ||
export type D2ExpressionParserProgramRuleVariableValue = { | ||
type: D2ExpressionParserProgramRuleVariableTypes; | ||
value: string | null; | ||
export type ProgramRuleVariableName = string; | ||
export type ProgramRuleVariableValue = { | ||
type: ProgramRuleVariableType; | ||
value: string; | ||
}; | ||
|
||
export class D2ExpressionParser { | ||
public evaluateRuleEngineCondtion( | ||
public evaluateRuleEngineCondition( | ||
ruleCondtion: string, | ||
ruleVariables: Map< | ||
D2ExpressionParserProgramRuleVariableName, | ||
D2ExpressionParserProgramRuleVariableValue | ||
> | ||
ruleVariables: Map<ProgramRuleVariableName, ProgramRuleVariableValue> | ||
): boolean { | ||
const programRuleVariableExpressionParser = new xp.ExpressionJs( | ||
const expressionParser = new xp.ExpressionJs( | ||
ruleCondtion, | ||
xp.ExpressionMode.RULE_ENGINE_CONDITION | ||
); | ||
|
||
//Mapper function for program rule variables | ||
const programRuleVariables = | ||
programRuleVariableExpressionParser.collectProgramRuleVariableNames(); | ||
const variables = expressionParser.collectProgramRuleVariableNames(); | ||
|
||
const programRuleVariablesValueMap = programRuleVariables.map(programRuleVariable => { | ||
const currentProgramRuleVariableValue = ruleVariables.get(programRuleVariable); | ||
|
||
if (!currentProgramRuleVariableValue) | ||
return { | ||
programRuleVariable: programRuleVariable, | ||
value: new xp.VariableValueJs(xp.ValueType.STRING, null, [], null), | ||
}; | ||
const variablesValueMap = this.mapProgramVariables(variables, ruleVariables); | ||
|
||
const variableValue = this.getVariableValueByType( | ||
currentProgramRuleVariableValue.type, | ||
currentProgramRuleVariableValue.value === "" | ||
? null | ||
: currentProgramRuleVariableValue.value | ||
); | ||
|
||
return { | ||
programRuleVariable: programRuleVariable, | ||
value: variableValue, | ||
}; | ||
}); | ||
|
||
const programRuleVariablesMap = new Map( | ||
programRuleVariablesValueMap.map(a => [a.programRuleVariable, a.value]) | ||
const variablesMap = new Map( | ||
variablesValueMap.map(variable => [variable.programRuleVariable, variable.value]) | ||
); | ||
|
||
const programVariables = programRuleVariableExpressionParser.collectProgramVariablesNames(); | ||
const programVariablesValues = _c( | ||
programVariables.map(programVariable => { | ||
if (programVariable === "current_date") | ||
return { | ||
programVariable: xp.ProgramVariable.current_date.name, | ||
value: new Date(), | ||
}; | ||
}) | ||
) | ||
.compact() | ||
.value(); | ||
const programVariablesMap = new Map( | ||
programVariablesValues.map(a => [a.programVariable, a.value]) | ||
); | ||
// const programVariables = expressionParser.collectProgramVariablesNames(); | ||
// const programVariablesValues = _c( | ||
// programVariables.map(programVariable => { | ||
// if (programVariable === "current_date") | ||
// return { | ||
// programVariable: xp.ProgramVariable.current_date.name, | ||
// value: new Date(), | ||
// }; | ||
// }) | ||
// ) | ||
// .compact() | ||
// .value(); | ||
// const programVariablesMap = new Map( | ||
// programVariablesValues.map(a => [a.programVariable, a.value]) | ||
// ); | ||
const expressionData = new xp.ExpressionDataJs( | ||
programRuleVariablesMap, | ||
programVariablesMap, | ||
variablesMap, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined | ||
); | ||
|
||
const parsedResult: boolean = programRuleVariableExpressionParser.evaluate( | ||
const parsedResult: boolean = expressionParser.evaluate( | ||
a => console.debug("ABC" + a), //SNEHA DEBUG : what is this? | ||
expressionData | ||
); | ||
return parsedResult; | ||
} | ||
|
||
private getVariableValueByType = ( | ||
type: D2ExpressionParserProgramRuleVariableTypes, | ||
type: ProgramRuleVariableType, | ||
stringValue: xp.Nullable<string> | ||
): xp.VariableValueJs => { | ||
switch (type) { | ||
case "select": | ||
case "text": | ||
return new xp.VariableValueJs(xp.ValueType.STRING, stringValue, [], null); | ||
return new xp.VariableValueJs(xp.ValueType.STRING, stringValue, [], null); //Use record mapping | ||
case "boolean": | ||
return new xp.VariableValueJs(xp.ValueType.BOOLEAN, stringValue, [], null); | ||
case "date": | ||
case "datetime": | ||
return new xp.VariableValueJs(xp.ValueType.DATE, stringValue, [], null); | ||
|
||
case "number": | ||
return new xp.VariableValueJs(xp.ValueType.NUMBER, stringValue, [], null); | ||
} | ||
}; | ||
|
||
private mapProgramVariables( | ||
programRuleVariables: string[], | ||
ruleVariables: Map<string, ProgramRuleVariableValue> | ||
) { | ||
return programRuleVariables.map(programRuleVariable => { | ||
const currentProgramRuleVariableValue = ruleVariables.get(programRuleVariable); | ||
|
||
if (!currentProgramRuleVariableValue) | ||
return { | ||
programRuleVariable: programRuleVariable, | ||
value: new xp.VariableValueJs(xp.ValueType.STRING, null, [], null), | ||
}; | ||
|
||
const variableValue = this.getVariableValueByType( | ||
currentProgramRuleVariableValue.type, | ||
currentProgramRuleVariableValue.value === "" | ||
? null | ||
: currentProgramRuleVariableValue.value | ||
); | ||
|
||
return { | ||
programRuleVariable: programRuleVariable, | ||
value: variableValue, | ||
}; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters