diff --git a/src/data/entities/D2ExpressionParser.ts b/src/data/entities/D2ExpressionParser.ts index 0aa2f06..fc1c727 100644 --- a/src/data/entities/D2ExpressionParser.ts +++ b/src/data/entities/D2ExpressionParser.ts @@ -9,6 +9,13 @@ export type ProgramRuleVariableValue = { value: string; }; +const VariableValueTypeMap: Record = { + text: xp.ValueType.STRING, + boolean: xp.ValueType.BOOLEAN, + date: xp.ValueType.DATE, + number: xp.ValueType.NUMBER, +}; + export class D2ExpressionParser { public evaluateRuleEngineCondition( ruleCondtion: string, @@ -52,7 +59,7 @@ export class D2ExpressionParser { ); const parsedResult: boolean = expressionParser.evaluate( - a => console.debug("ABC" + a), //SNEHA DEBUG : what is this? + () => console.debug(""), expressionData ); return parsedResult; @@ -62,16 +69,8 @@ export class D2ExpressionParser { type: ProgramRuleVariableType, stringValue: xp.Nullable ): xp.VariableValueJs => { - switch (type) { - case "text": - 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": - return new xp.VariableValueJs(xp.ValueType.DATE, stringValue, [], null); - case "number": - return new xp.VariableValueJs(xp.ValueType.NUMBER, stringValue, [], null); - } + const valueType = VariableValueTypeMap[type]; + return new xp.VariableValueJs(valueType, stringValue, [], null); }; private mapProgramVariables( diff --git a/src/data/utils/ruleHelper.ts b/src/data/utils/ruleHelper.ts index 09ea173..9540d2a 100644 --- a/src/data/utils/ruleHelper.ts +++ b/src/data/utils/ruleHelper.ts @@ -62,7 +62,7 @@ export const getProgramRules = ( return { id: id, - originalCondition: condition, + condition: condition, dataElementIds: _(dataElementIds).uniq().compact().value(), teAttributeIds: _(teaIds).uniq().compact().value(), actions: programRuleActions || [], diff --git a/src/domain/entities/Questionnaire/QuestionnaireRules.ts b/src/domain/entities/Questionnaire/QuestionnaireRules.ts index 585f52b..1660c19 100644 --- a/src/domain/entities/Questionnaire/QuestionnaireRules.ts +++ b/src/domain/entities/Questionnaire/QuestionnaireRules.ts @@ -42,9 +42,7 @@ export interface QuestionnaireRuleAction { } export interface QuestionnaireRule { id: Id; - // condition: string; //condition is parsed with dataelementId e.g: #{dataElementId} == 'Yes' - // d2Condition: string; //SNEHA TO DO : remove above condition and use this condition after testing - originalCondition: string; + condition: string; dataElementIds: Id[]; // all dataElements in condition (there could be mutiple conditions) teAttributeIds: Id[]; // all trackedEntityAttributes in condition (there could be mutiple conditions) actions: QuestionnaireRuleAction[]; @@ -137,13 +135,11 @@ const parseConditionWithExpressionParser = (rule: QuestionnaireRule, questions: ); return new D2ExpressionParser().evaluateRuleEngineCondition( - rule.originalCondition, + rule.condition, programRuleVariableValues ); } catch (error) { - console.error( - `Error parsing rule condition: ${rule.originalCondition} with error : ${error}` - ); + console.error(`Error parsing rule condition: ${rule.condition} with error : ${error}`); return false; } };