Skip to content

Commit

Permalink
#1164 | Add catch block to avoid crashing app for single dashboard ca…
Browse files Browse the repository at this point in the history
…rd rule failure
  • Loading branch information
himeshr committed Nov 1, 2023
1 parent 60df2fc commit 89048dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
30 changes: 19 additions & 11 deletions packages/openchs-android/src/service/RuleEvaluationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import GroupSubjectService from "./GroupSubjectService";
import ProgramService from "./program/ProgramService";
import individualServiceFacade from "./facade/IndividualServiceFacade";
import addressLevelServiceFacade from "./facade/AddressLevelServiceFacade";
import MessageService from './MessageService';

function getImports() {
return {rulesConfig, common, lodash, moment, motherCalculations, log: console.log};
Expand Down Expand Up @@ -76,6 +77,7 @@ class RuleEvaluationService extends BaseService {
this.entityRulesMap.forEach((entityRule, key) => {
entityRule.setFunctions(entityRule.ruleFile);
});
this.I18n = this.getService(MessageService).getI18n();
this.formMappingService = this.getService(FormMappingService);
this.conceptService = this.getService(ConceptService);
this.groupSubjectService = this.getService(GroupSubjectService);
Expand Down Expand Up @@ -656,22 +658,28 @@ class RuleEvaluationService extends BaseService {
return true;
}

executeDashboardCardRule(rule, ruleInput) {
const ruleFunc = eval(rule);
const result = ruleFunc({
params: {db: this.db, ruleInput: ruleInput},
imports: {lodash, moment}
});
return result;
executeDashboardCardRule(reportCard, ruleInput) {
try {
const ruleFunc = eval(reportCard.query);
const result = ruleFunc({
params: {db: this.db, ruleInput: ruleInput},
imports: {lodash, moment}
});
return result;
} catch (error) {
General.logError("Rule-Failure", `DashboardCard report card rule failed for uuid: ${reportCard.uuid}, name: ${reportCard.name}`);
General.logError("Rule-Failure", error);
return {primaryValue: this.I18n.t("Error"), lineListFunction: _.noop()};
}
}

isOldStyleQueryResult(queryResult) {
//The result can either be an array or a RealmResultsProxy. We are verifying this by looking for existence of the length key.
return queryResult.length !== undefined;
}

getDashboardCardCount(rule, ruleInput) {
const queryResult = this.executeDashboardCardRule(rule, ruleInput);
getDashboardCardCount(reportCard, ruleInput) {
const queryResult = this.executeDashboardCardRule(reportCard, ruleInput);
if (this.isOldStyleQueryResult(queryResult)) {
return {primaryValue: queryResult.length, secondaryValue: null, clickable: true};
} else {
Expand All @@ -683,8 +691,8 @@ class RuleEvaluationService extends BaseService {
}
}

getDashboardCardQueryResult(rule, ruleInput) {
const queryResult = this.executeDashboardCardRule(rule, ruleInput);
getDashboardCardQueryResult(reportCard, ruleInput) {
const queryResult = this.executeDashboardCardRule(reportCard, ruleInput);
if (this.isOldStyleQueryResult(queryResult)) {//The result can either be an array or a RealmResultsProxy. We are looking for existence of the length key.
return queryResult;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ReportCardService extends BaseService {
const standardReportCardType = reportCard.standardReportCardType;
switch (true) {
case _.isNil(standardReportCardType) :
return this.getService(RuleEvaluationService).getDashboardCardCount(reportCard.query, ruleInputArray);
return this.getService(RuleEvaluationService).getDashboardCardCount(reportCard, ruleInputArray);
case standardReportCardType.isApprovalType() :
return this.getCountForApprovalCardsType(standardReportCardType.name);
case standardReportCardType.isDefaultType() :
Expand All @@ -129,7 +129,7 @@ class ReportCardService extends BaseService {
const standardReportCardType = reportCard.standardReportCardType;
switch (true) {
case _.isNil(standardReportCardType) : {
const result = this.getService(RuleEvaluationService).getDashboardCardQueryResult(reportCard.query, ruleInputArray);
const result = this.getService(RuleEvaluationService).getDashboardCardQueryResult(reportCard, ruleInputArray);
return {status: null, result}
}
case standardReportCardType.isApprovalType() :
Expand Down

0 comments on commit 89048dd

Please sign in to comment.