Skip to content

Commit

Permalink
fix: some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Oct 10, 2024
1 parent 8a065b8 commit 0c0d032
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 91 deletions.
124 changes: 59 additions & 65 deletions src/data/entities/D2ExpressionParser.ts
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,
};
});
}
}
54 changes: 28 additions & 26 deletions src/domain/entities/Questionnaire/QuestionnaireRules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
D2ExpressionParser,
D2ExpressionParserProgramRuleVariableName,
D2ExpressionParserProgramRuleVariableValue,
ProgramRuleVariableName,
ProgramRuleVariableValue,
} from "../../../data/entities/D2ExpressionParser";
import { D2ProgramRuleVariable } from "../../../data/entities/D2Program";
import { Maybe } from "../../../utils/ts-utils";
Expand Down Expand Up @@ -98,30 +98,33 @@ export const getQuestionValueByType = (question: Question): string => {
function getProgramRuleVariableValues(
programRuleVariables: Maybe<D2ProgramRuleVariable[]>,
questions: Question[]
): Map<string, D2ExpressionParserProgramRuleVariableValue> {
const programRuleVariableValues: Map<
D2ExpressionParserProgramRuleVariableName,
D2ExpressionParserProgramRuleVariableValue
> = new Map(
programRuleVariables?.map(prv => {
const currentQuestion = questions.find(
question =>
question.id === prv?.dataElement?.id ||
question.id === prv?.trackedEntityAttribute?.id
);
): Map<string, ProgramRuleVariableValue> {
const programRuleVariableValues: Map<ProgramRuleVariableName, ProgramRuleVariableValue> =
new Map(
programRuleVariables?.map(prv => {
const currentQuestion = questions.find(
question =>
question.id === prv?.dataElement?.id ||
question.id === prv?.trackedEntityAttribute?.id
);

if (!currentQuestion) return [prv.name, { type: "text", value: "" }];
const value = getQuestionValueByType(currentQuestion);
if (!currentQuestion) return [prv.name, { type: "text", value: "" }];
const value = getQuestionValueByType(currentQuestion);

return [
prv.name,
{
type: currentQuestion.type,
value: value,
},
];
})
);
return [
prv.name,
{
type:
currentQuestion.type === "datetime"
? "date"
: currentQuestion.type === "select"
? "text"
: currentQuestion.type,
value: value,
},
];
})
);

return programRuleVariableValues;
}
Expand All @@ -133,9 +136,8 @@ const parseConditionWithExpressionParser = (rule: QuestionnaireRule, questions:
questions
);

return new D2ExpressionParser().evaluateRuleEngineCondtion(
return new D2ExpressionParser().evaluateRuleEngineCondition(
rule.originalCondition,

programRuleVariableValues
);
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions src/domain/entities/generic/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export class HashMap<K, V> {
toObject(): ToObject<K, V> {
return imap.toObject(this._map) as ToObject<K, V>;
}
//TO DO
// toMap()
}

type ToObject<K, V> = K extends keyof any ? Record<K, V> : Record<string, V>;

0 comments on commit 0c0d032

Please sign in to comment.