Skip to content

Commit

Permalink
fix: code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Oct 28, 2024
1 parent 0c0d032 commit 222fed1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
21 changes: 10 additions & 11 deletions src/data/entities/D2ExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export type ProgramRuleVariableValue = {
value: string;
};

const VariableValueTypeMap: Record<ProgramRuleVariableType, xp.ValueType> = {
text: xp.ValueType.STRING,
boolean: xp.ValueType.BOOLEAN,
date: xp.ValueType.DATE,
number: xp.ValueType.NUMBER,
};

export class D2ExpressionParser {
public evaluateRuleEngineCondition(
ruleCondtion: string,
Expand Down Expand Up @@ -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;
Expand All @@ -62,16 +69,8 @@ export class D2ExpressionParser {
type: ProgramRuleVariableType,
stringValue: xp.Nullable<string>
): 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(
Expand Down
2 changes: 1 addition & 1 deletion src/data/utils/ruleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [],
Expand Down
10 changes: 3 additions & 7 deletions src/domain/entities/Questionnaire/QuestionnaireRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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;
}
};

0 comments on commit 222fed1

Please sign in to comment.