Skip to content

Commit

Permalink
#1273 - rule based edit check on enrolment edit and program exit edit
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Feb 14, 2024
1 parent 305dd0b commit 5c8c395
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class IndividualRegistrationDetailsActions {
subjectSummary,
isRelationshipTypePresent: individualRelationGenderMappings.length > 0,
subjectProgramEligibilityStatuses,
groupAffiliation: groupAffiliationState,
editFormRuleResponse: EditFormRuleResponse.createEditAllowedResponse()
groupAffiliation: groupAffiliationState
};
}

Expand All @@ -54,7 +53,7 @@ class IndividualRegistrationDetailsActions {
const editFormRuleResponse = context.get(RuleEvaluationService).runEditFormRule(registrationForm, state.individual, 'Individual');
const newState = {...state};
if (editFormRuleResponse.isEditAllowed())
action.formEditAllowed();
action.continueRegistrationEdit();
else
newState.editFormRuleResponse = editFormRuleResponse;
return newState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import EntityService from "../../service/EntityService";
import {
Encounter,
NullProgramEnrolment,
ProgramEncounter,
ProgramEnrolment,
WorkItem,
WorkList,
WorkLists
} from 'avni-models';
import {Encounter, NullProgramEnrolment, ProgramEncounter, ProgramEnrolment, WorkItem, WorkList, WorkLists} from 'avni-models';
import _ from 'lodash';
import EntityTypeChoiceState from "../common/EntityTypeChoiceState";
import FormMappingService from "../../service/FormMappingService";
Expand All @@ -22,6 +14,7 @@ import UserInfoService from "../../service/UserInfoService";
import ProgramEnrolmentService from "../../service/ProgramEnrolmentService";
import IndividualService from "../../service/IndividualService";
import {firebaseEvents, logEvent} from "../../utility/Analytics";
import {EditFormRuleResponse} from "rules-config";

class ProgramEnrolmentDashboardActions {
static setEncounterType(encounterType) {
Expand All @@ -41,7 +34,8 @@ class ProgramEnrolmentDashboardActions {
encounterTypes: [],
displayActionSelector: false,
expandEnrolmentInfo: false,
completedEncounters: []
completedEncounters: [],
editFormRuleResponse: EditFormRuleResponse.createEditAllowedResponse()
};
}

Expand Down Expand Up @@ -78,19 +72,10 @@ class ProgramEnrolmentDashboardActions {

static clone(state) {
return {
...state,
programEncounterTypeState: state.programEncounterTypeState.clone(),
enrolment: state.enrolment,
encounter: state.encounter.cloneForEdit(),
encounterTypes: state.encounterTypes.slice(),
displayActionSelector: state.displayActionSelector,
programsAvailable: state.programsAvailable,
showCount: state.showCount,
dashboardButtons: state.dashboardButtons,
enrolmentSummary: state.enrolmentSummary,
hideExit: state.hideExit,
hideEnrol: state.hideEnrol,
expandEnrolmentInfo: state.expandEnrolmentInfo,
completedEncounters: state.completedEncounters,
encounterTypes: state.encounterTypes.slice()
};
}

Expand All @@ -106,9 +91,7 @@ class ProgramEnrolmentDashboardActions {
const enrolment = ProgramEnrolmentDashboardActions._getEnrolment(newState, context, individualUUID, enrolmentUUID);
newState.completedEncounters = _.filter(enrolment.nonVoidedEncounters(), (encounter) => encounter.encounterDateTime || encounter.cancelDateTime)
.map(encounter => ({encounter, expand: false}));
let onEnrolmentChange = ProgramEnrolmentDashboardActions._onEnrolmentChange(newState, context, enrolment);

return onEnrolmentChange;
return ProgramEnrolmentDashboardActions._onEnrolmentChange(newState, context, enrolment);
}

static onLanding(state, action, context) {
Expand Down Expand Up @@ -166,9 +149,6 @@ class ProgramEnrolmentDashboardActions {
return newState;
}

//Program Encounter Type

//Encounter Type
static launchChooseEncounterType(state, action, context) {
const newState = ProgramEnrolmentDashboardActions.clone(state);
newState.encounterTypeState.launchChooseEntityType();
Expand All @@ -193,12 +173,20 @@ class ProgramEnrolmentDashboardActions {
return newState;
}

//Encounter Type

static onEditEnrolment(state, action, context) {
logEvent(firebaseEvents.EDIT_ENROLMENT);
const enrolment = context.get(EntityService).findByUUID(state.enrolment.uuid, ProgramEnrolment.schema.name);
let workLists = new WorkLists(

const form = context.get(FormMappingService).findFormForProgramEnrolment(enrolment.program, enrolment.individual.subjectType);
const editFormRuleResponse = context.get(RuleEvaluationService).runEditFormRule(form, enrolment, 'ProgramEnrolment');

if (!editFormRuleResponse.isEditAllowed()) {
const newState = {...state};
newState.editFormRuleResponse = editFormRuleResponse;
return newState;
}

const workLists = new WorkLists(
new WorkList('Enrolment',
[new WorkItem(General.randomUUID(),
WorkItem.type.PROGRAM_ENROLMENT,
Expand All @@ -207,13 +195,22 @@ class ProgramEnrolmentDashboardActions {
programName: enrolment.program.name,
})
]));
action.cb(enrolment, workLists);
action.continueEnrolmentEdit(enrolment, workLists);
return state;
}

static onEditEnrolmentExit(state, action, context) {
logEvent(firebaseEvents.EDIT_PROGRAM_EXIT);
const enrolment = context.get(EntityService).findByUUID(state.enrolment.uuid, ProgramEnrolment.schema.name);

const form = context.get(FormMappingService).findFormForProgramExit(enrolment.program, enrolment.individual.subjectType);
const editFormRuleResponse = context.get(RuleEvaluationService).runEditFormRule(form, enrolment, 'ProgramEnrolment');
if (!editFormRuleResponse.isEditAllowed()) {
const newState = {...state};
newState.editFormRuleResponse = editFormRuleResponse;
return newState;
}

const workLists = new WorkLists(
new WorkList('Exit',
[new WorkItem(General.randomUUID(),
Expand All @@ -223,7 +220,7 @@ class ProgramEnrolmentDashboardActions {
programName: enrolment.program.name,
})
]));
action.cb(enrolment, workLists);
action.continueEditExit(enrolment, workLists);
return state;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class SubjectDashboardProfileTab extends AbstractComponent {
editProfile() {
logEvent(firebaseEvents.EDIT_SUBJECT);
this.dispatchAction(Actions.ON_EDIT_START, {
formEditAllowed: () => {
continueRegistrationEdit: () => {
CHSNavigator.navigateToRegisterView(this, {
workLists: new WorkLists(
new WorkList(`${this.state.individual.subjectType.name} `,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Alert, ScrollView, TouchableOpacity, View} from "react-native";
import {Alert, ScrollView, ToastAndroid, TouchableOpacity, View} from "react-native";
import PropTypes from 'prop-types';
import React from "react";
import AbstractComponent from "../../framework/view/AbstractComponent";
Expand Down Expand Up @@ -67,13 +67,13 @@ class SubjectDashboardProgramsTab extends AbstractComponent {

editEnrolment(pageNumber) {
this.dispatchAction(Actions.ON_EDIT_ENROLMENT, {
cb: (enrolment, workLists) => CHSNavigator.navigateToProgramEnrolmentView(this, enrolment, workLists, true, pageNumber)
continueEnrolmentEdit: (enrolment, workLists) => CHSNavigator.navigateToProgramEnrolmentView(this, enrolment, workLists, true, pageNumber)
});
}

editExit(pageNumber) {
this.dispatchAction(Actions.ON_EDIT_ENROLMENT_EXIT, {
cb: (enrolment, workLists) => CHSNavigator.navigateToExitProgram(this, enrolment, workLists, true, pageNumber)
continueEditExit: (enrolment, workLists) => CHSNavigator.navigateToExitProgram(this, enrolment, workLists, true, pageNumber)
});
}

Expand Down Expand Up @@ -256,6 +256,7 @@ class SubjectDashboardProgramsTab extends AbstractComponent {
primaryAction={this.getPrimaryEnrolmentContextAction(hasExitPrivilege)}/>
</View>
</TouchableOpacity>
{this.state.editFormRuleResponse.isEditDisallowed() && ToastAndroid.show(this.state.editFormRuleResponse.getMessageKey(), ToastAndroid.SHORT)}
</View>);
}

Expand Down

0 comments on commit 5c8c395

Please sign in to comment.