Skip to content

Commit

Permalink
fix: return an either
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Nov 4, 2024
1 parent 7e0dd8f commit 39474f9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
88 changes: 50 additions & 38 deletions src/data/entities/D2ExpressionParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as xp from "@dhis2/expression-parser";
import _c from "../../domain/entities/generic/Collection";
import { Either } from "../../domain/entities/generic/Either";

export type ProgramRuleVariableType = "text" | "number" | "date" | "boolean";

Expand All @@ -20,49 +21,60 @@ export class D2ExpressionParser {
public evaluateRuleEngineCondition(
ruleCondtion: string,
ruleVariables: Map<ProgramRuleVariableName, ProgramRuleVariableValue>
): boolean {
const expressionParser = new xp.ExpressionJs(
ruleCondtion,
xp.ExpressionMode.RULE_ENGINE_CONDITION
);
): Either<Error, boolean> {
try {
const expressionParser = new xp.ExpressionJs(
ruleCondtion,
xp.ExpressionMode.RULE_ENGINE_CONDITION
);

//Mapper function for program rule variables
const variables = expressionParser.collectProgramRuleVariableNames();

//Mapper function for program rule variables
const variables = expressionParser.collectProgramRuleVariableNames();
const variablesValueMap = this.mapProgramVariables(variables, ruleVariables);

const variablesValueMap = this.mapProgramVariables(variables, ruleVariables);
const variablesMap = new Map(
variablesValueMap.map(variable => [variable.programRuleVariable, variable.value])
);

const variablesMap = new Map(
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 expressionData = new xp.ExpressionDataJs(
variablesMap,
undefined,
undefined,
undefined,
undefined
);
// 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(
variablesMap,
undefined,
undefined,
undefined,
undefined
);

const parsedResult: boolean = expressionParser.evaluate(
() => console.debug(""),
expressionData
);
return parsedResult;
const parsedResult: boolean = expressionParser.evaluate(
() => console.debug(""),
expressionData
);
return Either.success(parsedResult);
} catch (error) {
return Either.error(
new Error(
`An error occurred while evaluating the rule in D2ExpressionParser::evaluateRuleEngineCondition: ${error}`
)
);
}
}

private getVariableValueByType = (
Expand Down
8 changes: 0 additions & 8 deletions src/domain/entities/Questionnaire/QuestionnaireQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,19 @@ export class QuestionnaireQuestion {

const finalUpdatesWithSideEffects = allQuestionsRequiringUpdate.reduce(
(acc, questionRequiringUpdate) => {
console.debug("updatedQuestion", updatedQuestion);
console.debug("questionRequiringUpdate", questionRequiringUpdate);
console.debug("acc", acc);

const currentApplicableRules = getApplicableRules(
questionRequiringUpdate,
questionnaire.rules,
acc
);
console.debug("rules", currentApplicableRules);
processedQuestions.push(questionRequiringUpdate);
console.debug("processedQuestions", processedQuestions);
const updates = this.updateQuestions(
processedQuestions,
acc,
questionRequiringUpdate,
currentApplicableRules,
questionnaire
);

console.debug("updates", updates);
return updates;
},
finalUpdatedQuestions
Expand Down
25 changes: 11 additions & 14 deletions src/domain/entities/Questionnaire/QuestionnaireRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,16 @@ function getProgramRuleVariableValues(
return programRuleVariableValues;
}

const parseConditionWithExpressionParser = (rule: QuestionnaireRule, questions: Question[]) => {
try {
const programRuleVariableValues = getProgramRuleVariableValues(
rule.programRuleVariables,
questions
);
const parseConditionWithExpressionParser = (
rule: QuestionnaireRule,
questions: Question[]
): boolean => {
const programRuleVariableValues = getProgramRuleVariableValues(
rule.programRuleVariables,
questions
);

return new D2ExpressionParser().evaluateRuleEngineCondition(
rule.condition,
programRuleVariableValues
);
} catch (error) {
console.error(`Error parsing rule condition: ${rule.condition} with error : ${error}`);
return false;
}
return new D2ExpressionParser()
.evaluateRuleEngineCondition(rule.condition, programRuleVariableValues)
.match({ success: x => x, error: () => false });
};

0 comments on commit 39474f9

Please sign in to comment.