Skip to content

Commit

Permalink
#1273 - edit registration checks for editability
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Feb 13, 2024
1 parent 7b71376 commit 856ad85
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 68 deletions.
26 changes: 13 additions & 13 deletions packages/openchs-android/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/openchs-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"lodash": "4.17.21",
"moment": "2.29.4",
"native-base": "3.4.9",
"openchs-models": "1.31.29",
"openchs-models": "1.31.33",
"prop-types": "15.8.1",
"react": "18.2.0",
"react-native": "0.72.3",
Expand Down Expand Up @@ -96,7 +96,7 @@
"realm": "11.8.0",
"redux": "4.2.0",
"rn-fetch-blob": "0.13.0-beta-1",
"rules-config": "github:openchs/rules-config#fe552da405368bfd138e2f38e605c1d307e3ebe4",
"rules-config": "github:openchs/rules-config#982bb007c4f759639063159196edee386b48cac3",
"stacktrace-js": "2.0.2",
"transducers-js": "0.4.174",
"uuid": "^9.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import SubjectProgramEligibilityService from "../../service/program/SubjectProgr
import EntityService from "../../service/EntityService";
import FormMappingService from "../../service/FormMappingService";
import GroupAffiliationState from '../../state/GroupAffiliationState';
import {EditFormRuleResponse} from "rules-config";

class IndividualRegistrationDetailsActions {
static getInitialState() {
return {
expand: false,
subjectProgramEligibilityStatuses: [],
displayIndicator: false,
groupAffiliationState: new GroupAffiliationState()
groupAffiliationState: new GroupAffiliationState(),
editFormRuleResponse: EditFormRuleResponse.createEditAllowedResponse()
};
}

Expand All @@ -42,10 +44,22 @@ class IndividualRegistrationDetailsActions {
subjectSummary,
isRelationshipTypePresent: individualRelationGenderMappings.length > 0,
subjectProgramEligibilityStatuses,
groupAffiliation: groupAffiliationState
groupAffiliation: groupAffiliationState,
editFormRuleResponse: EditFormRuleResponse.createEditAllowedResponse()
};
}

static onEditStart(state, action, context) {
const registrationForm = context.get(FormMappingService).findRegistrationForm(state.individual.subjectType);
const editFormRuleResponse = context.get(RuleEvaluationService).runEditFormRule(registrationForm, state.individual, 'Individual');
const newState = {...state};
if (editFormRuleResponse.isEditAllowed())
action.formEditAllowed();
else
newState.editFormRuleResponse = editFormRuleResponse;
return newState;
}

static onDeleteRelative(state, action, context) {
context.get(IndividualRelationshipService).deleteRelative(action.individualRelative);
const relatives = context.get(IndividualRelationshipService).getRelatives(state.individual);
Expand Down Expand Up @@ -130,7 +144,6 @@ class IndividualRegistrationDetailsActions {
}
});
}

}

const IndividualRegistrationDetailsActionsNames = {
Expand All @@ -140,6 +153,7 @@ const IndividualRegistrationDetailsActionsNames = {
ON_TOGGLE: "IRDA.ON_TOGGLE",
ON_SUBJECT_PROGRAM_ELIGIBILITY_CHECK: "IRDA.ON_SUBJECT_PROGRAM_ELIGIBILITY_CHECK",
ON_DISPLAY_INDICATOR_TOGGLE: "IRDA.ON_DISPLAY_INDICATOR_TOGGLE",
ON_EDIT_START: "IRDA.ON_EDIT_START"
};

const IndividualRegistrationDetailsActionsMap = new Map([
Expand All @@ -149,6 +163,7 @@ const IndividualRegistrationDetailsActionsMap = new Map([
[IndividualRegistrationDetailsActionsNames.ON_TOGGLE, IndividualRegistrationDetailsActions.onToggle],
[IndividualRegistrationDetailsActionsNames.ON_SUBJECT_PROGRAM_ELIGIBILITY_CHECK, IndividualRegistrationDetailsActions.onSubjectProgramEligibilityCheck],
[IndividualRegistrationDetailsActionsNames.ON_DISPLAY_INDICATOR_TOGGLE, IndividualRegistrationDetailsActions.onDisplayIndicatorToggle],
[IndividualRegistrationDetailsActionsNames.ON_EDIT_START, IndividualRegistrationDetailsActions.onEditStart],
]);

export {
Expand Down
9 changes: 0 additions & 9 deletions packages/openchs-android/src/model/Rules.js

This file was deleted.

66 changes: 45 additions & 21 deletions packages/openchs-android/src/service/RuleEvaluationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ import ProgramService from "./program/ProgramService";
import individualServiceFacade from "./facade/IndividualServiceFacade";
import addressLevelServiceFacade from "./facade/AddressLevelServiceFacade";
import MessageService from './MessageService';
import {JSONStringify} from '../utility/JsonStringify';
import {Groups} from "openchs-models";
import {JSONStringify} from "../utility/JsonStringify";
import UserInfoService from "./UserInfoService";
import {EditFormRuleResponse} from "rules-config";

function getImports() {
return {rulesConfig, common, lodash, moment, motherCalculations, log: console.log};
Expand Down Expand Up @@ -88,7 +91,7 @@ class RuleEvaluationService extends BaseService {
}
}

getIndividualUUID = (entity, entityName) => {
getIndividualUUID(entity, entityName) {
switch (entityName) {
case 'Individual':
return entity.uuid;
Expand Down Expand Up @@ -136,9 +139,9 @@ class RuleEvaluationService extends BaseService {
General.logDebug("RuleEvaluationService", trimmedDecisions);
return trimmedDecisions;
} catch (e) {
General.logDebug("RuleEvaluationService",`form.uuid: ${form.uuid} entityName: ${entityName}`);
General.logDebug("RuleEvaluationService", `form.uuid: ${form.uuid} entityName: ${entityName}`);
this.saveFailedRules(e, form.uuid, individualUUID,
'Decision', form.uuid, entityName, entity.uuid);
'Decision', form.uuid, entityName, entity.uuid);
}
} else {
const decisionsMap = rulesFromTheBundle.reduce((decisions, rule) => {
Expand Down Expand Up @@ -185,7 +188,7 @@ class RuleEvaluationService extends BaseService {
return true;
} catch (error) {
this.saveFailedRules(error, ruleUUID, individualUUID,
'Validation', ruleUUID, 'Individual', individualUUID);
'Validation', ruleUUID, 'Individual', individualUUID);
return false;
}
}
Expand All @@ -210,7 +213,7 @@ class RuleEvaluationService extends BaseService {
} catch (e) {
General.logDebug("Rule-Failure", `New worklist updation rule failed ${orgConfig.uuid} `);
this.saveFailedRules(e, orgConfig.uuid, this.getIndividualUUID(workLists, "WorkList"),
'WorkListUpdation', orgConfig.uuid, entityName, context.entity.uuid);
'WorkListUpdation', orgConfig.uuid, entityName, context.entity.uuid);
}
} else {
const additionalRules = this.getService(RuleService).getRulesByType('WorkListUpdation');
Expand All @@ -220,6 +223,27 @@ class RuleEvaluationService extends BaseService {
return workLists;
}

runEditFormRule(form, entity, entityName) {
const user = this.getService(UserInfoService).getUserInfo();
const userGroups = this.getService(EntityService).loadAll(Groups.schema.name);
if (_.isEmpty(form.editFormRule)) {
return EditFormRuleResponse.createEditAllowedResponse();
} else {
try {
const ruleFunc = eval(form.editFormRule);
const ruleResponse = ruleFunc({
params: {entity, form, services: this.services, user, myUserGroups: userGroups},
imports: getImports()
});
return EditFormRuleResponse.createEditRuleResponse(ruleResponse);
} catch (e) {
General.logDebug("Rule-Failure", `EditFormRule failed: ${JSONStringify(e)}`);
this.saveFailedRules(e, form.uuid, this.getIndividualUUID(entity, entityName), 'EditForm', form.uuid, entityName, entity.uuid);
return EditFormRuleResponse.createEditAllowedResponse();
}
}
}

runRuleAndSaveFailure(rule, entityName, entity, ruleTypeValue, config, context, entityContext) {
try {
if (entityName === 'WorkList') {
Expand All @@ -233,7 +257,7 @@ class RuleEvaluationService extends BaseService {
} catch (error) {
General.logDebug("Rule-Failure", `Rule failed: ${rule.name}, uuid: ${rule.uuid}`);
this.saveFailedRules(error, rule.uuid, this.getIndividualUUID(entity, entityName),
'Decision', rule.uuid, entityName, entity.uuid);
'Decision', rule.uuid, entityName, entity.uuid);
return ruleTypeValue;
}
}
Expand Down Expand Up @@ -277,7 +301,7 @@ class RuleEvaluationService extends BaseService {
return this.entityRulesMap.get(entityName).getEnrolmentSummary(enrolment, context);
} catch (error) {
this.saveFailedRules(error, '', this.getIndividualUUID(enrolment, entityName),
'EnrolmentSummary', enrolment.program.uuid, entityName, enrolment.uuid);
'EnrolmentSummary', enrolment.program.uuid, entityName, enrolment.uuid);
return [];
}
}
Expand Down Expand Up @@ -312,7 +336,7 @@ class RuleEvaluationService extends BaseService {
General.logDebug("Rule-Failure",
`Subject Summary Rule failed for: ${subjectType.name} Subject type`);
this.saveFailedRules(e, subjectType.uuid, this.getIndividualUUID(individual, entityName),
'SubjectSummary', subjectType.uuid, entityName, individual.uuid);
'SubjectSummary', subjectType.uuid, entityName, individual.uuid);
return [];
}
}
Expand Down Expand Up @@ -344,7 +368,7 @@ class RuleEvaluationService extends BaseService {
General.logDebug("Rule-Failure",
`Subject Program Eligibility Rule failed for: ${subjectType.name} Subject type ${e.message} ${e.stack}`);
this.saveFailedRules(e, subjectType.uuid, this.getIndividualUUID(individual, 'Individual'),
'EnrolmentEligibilityCheck', subjectType.uuid, 'Individual', individual.uuid);
'EnrolmentEligibilityCheck', subjectType.uuid, 'Individual', individual.uuid);
throw Error(e.message);
}
}
Expand Down Expand Up @@ -383,7 +407,7 @@ class RuleEvaluationService extends BaseService {
General.logDebug("Rule-Failure",
`New Enrolment Summary Rule failed for: ${enrolment.program.name} program`);
this.saveFailedRules(e, program.uuid, this.getIndividualUUID(enrolment, entityName),
'EnrolmentSummary', program.uuid, entityName, enrolment.uuid);
'EnrolmentSummary', program.uuid, entityName, enrolment.uuid);
return [];
}
}
Expand All @@ -404,7 +428,7 @@ class RuleEvaluationService extends BaseService {
console.log(e);
General.logDebug("Rule-Failure", `Validation failed for: ${form.name} form name`);
this.saveFailedRules(e, form.uuid, this.getIndividualUUID(entity, entityName),
'Validation', form.uuid, entityName, entity.uuid);
'Validation', form.uuid, entityName, entity.uuid);
}
} else if (!_.isEmpty(rulesFromTheBundle)) {
const validationErrors = rulesFromTheBundle.reduce(
Expand Down Expand Up @@ -463,7 +487,7 @@ class RuleEvaluationService extends BaseService {
} catch (e) {
General.logDebug("Rule-Failure", `New checklist rule failed for form: ${form.uuid}`);
this.saveFailedRules(e, form.uuid, this.getIndividualUUID(entity, entityName),
'Checklist', form.uuid, entityName, entity.uuid);
'Checklist', form.uuid, entityName, entity.uuid);
}
} else {
const allChecklists = this.getAllRuleItemsFor(form, "Checklists", "Form")
Expand Down Expand Up @@ -500,7 +524,7 @@ class RuleEvaluationService extends BaseService {
} catch (e) {
General.logDebug("Rule-Failure", `New form element group rule failed for: ${formElementGroup.uuid}`);
this.saveFailedRules(e, formElementGroup.uuid, this.getIndividualUUID(entity, entityName),
'FormElementGroup', formElementGroup.uuid, entityName, entity.uuid);
'FormElementGroup', formElementGroup.uuid, entityName, entity.uuid);
}
}

Expand Down Expand Up @@ -588,7 +612,7 @@ class RuleEvaluationService extends BaseService {
} catch (e) {
General.logDebug("Rule-Failure", `New Rule failed for: ${formElement.name}`);
this.saveFailedRules(e, formElement.uuid, this.getIndividualUUID(entity, entityName),
'FormElement', formElement.uuid, entityName, entity.uuid);
'FormElement', formElement.uuid, entityName, entity.uuid);
return null;
}
}
Expand Down Expand Up @@ -630,7 +654,7 @@ class RuleEvaluationService extends BaseService {
General.logDebug("Rule-Faiure", e);
General.logDebug("Rule-Failure", `New encounter eligibility failed for: ${encounterType.name} encounter name`);
this.saveFailedRules(e, encounterType.uuid, individual.uuid,
'EncounterEligibilityCheck', encounterType.uuid, 'Individual', individual.uuid);
'EncounterEligibilityCheck', encounterType.uuid, 'Individual', individual.uuid);
}
} else if (!_.isEmpty(rulesFromTheBundle)) {
return this.runRuleAndSaveFailure(_.last(rulesFromTheBundle), 'Encounter', {individual}, true);
Expand All @@ -653,7 +677,7 @@ class RuleEvaluationService extends BaseService {
General.logDebug("Rule-Failure", e);
General.logDebug("Rule-Failure", `New enrolment eligibility failed for: ${program.name} program name`);
this.saveFailedRules(e, program.uuid, individual.uuid,
'EnrolmentEligibilityCheck', program.uuid, 'Individual', individual.uuid);
'EnrolmentEligibilityCheck', program.uuid, 'Individual', individual.uuid);
}
} else if (!_.isEmpty(rulesFromTheBundle)) {
return this.runRuleAndSaveFailure(_.last(rulesFromTheBundle), 'Encounter', {individual}, true);
Expand All @@ -674,7 +698,7 @@ class RuleEvaluationService extends BaseService {
General.logDebug("Rule-Failure", e);
General.logDebug("Rule-Failure", `Manual enrolment eligibility failed for: ${program.name} program name`);
this.saveFailedRules(e, program.uuid, subject.uuid,
'ManualEnrolmentEligibilityCheckRule', program.uuid, 'Individual', subject.uuid);
'ManualEnrolmentEligibilityCheckRule', program.uuid, 'Individual', subject.uuid);
}
}

Expand All @@ -693,7 +717,7 @@ class RuleEvaluationService extends BaseService {
General.logError("Rule-Failure", `DashboardCard report card rule failed for uuid: ${reportCard.uuid}, name: ${reportCard.name}`);
General.logError("Rule-Failure", error);
this.saveFailedRules(error, reportCard.uuid, '',
'ReportCard', reportCard.uuid, null, null);
'ReportCard', reportCard.uuid, null, null);
return {
hasErrorMsg: true,
primaryValue: this.I18n.t("Error"),
Expand All @@ -710,7 +734,7 @@ class RuleEvaluationService extends BaseService {

getDashboardCardCount(reportCard, ruleInput) {
const queryResult = this.executeDashboardCardRule(reportCard, ruleInput);
if(queryResult.hasErrorMsg) {
if (queryResult.hasErrorMsg) {
return this.createErrorResponse(reportCard, queryResult);
}
if (this.isOldStyleQueryResult(queryResult)) {
Expand Down Expand Up @@ -761,7 +785,7 @@ class RuleEvaluationService extends BaseService {
return queryResult;
} else if (reportCard.nested) {
return _.flatMap(queryResult.reportCards, (reportCardResultsItr, index) => (
reportCard.itemKey === reportCard.getCardId(index) && _.isFunction(reportCardResultsItr.lineListFunction) ? reportCardResultsItr.lineListFunction() : []
reportCard.itemKey === reportCard.getCardId(index) && _.isFunction(reportCardResultsItr.lineListFunction) ? reportCardResultsItr.lineListFunction() : []
));
} else {
return _.isFunction(queryResult.lineListFunction) ? queryResult.lineListFunction() : null;
Expand Down
Loading

0 comments on commit 856ad85

Please sign in to comment.