Skip to content

Commit

Permalink
fix: handle generic variable current_date
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Nov 10, 2024
1 parent 39474f9 commit 8bbb990
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/data/entities/D2ExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,27 @@ export class D2ExpressionParser {
variablesValueMap.map(variable => [variable.programRuleVariable, variable.value])
);

//TO DO : use switch instead of If

// 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 programVariables = expressionParser.collectProgramVariablesNames();

programVariables.forEach(programVariable => {
switch (programVariable) {
case "current_date": {
variablesMap.set(
programVariable,
this.getVariableValueByType(
"date",
new Date().toISOString().split("T")[0]
)
);
break;
}
default:
throw new Error(
`Unhandled Program variable of type : ${programVariable}. Please contact developer`
);
}
});

const expressionData = new xp.ExpressionDataJs(
variablesMap,
undefined,
Expand Down
9 changes: 8 additions & 1 deletion src/domain/entities/Questionnaire/QuestionnaireRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const getQuestionValueByType = (question: Question): string => {
case "boolean":
return question.value === undefined ? "false" : question.value.toString();
case "date":
return question.value?.toISOString().split("T")[0] ?? "";
case "datetime":
return question.value?.toString() ?? "";

Expand Down Expand Up @@ -138,5 +139,11 @@ const parseConditionWithExpressionParser = (

return new D2ExpressionParser()
.evaluateRuleEngineCondition(rule.condition, programRuleVariableValues)
.match({ success: x => x, error: () => false });
.match({
success: parsedResult => parsedResult,
error: errMsg => {
console.error(errMsg);
return false;
},
});
};

0 comments on commit 8bbb990

Please sign in to comment.