Skip to content

Instantly share code, notes, and snippets.

@hithacker
Created December 12, 2019 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hithacker/d0fe89107b974797fbb11ced1feda146 to your computer and use it in GitHub Desktop.
Save hithacker/d0fe89107b974797fbb11ced1feda146 to your computer and use it in GitHub Desktop.
({params, imports}) => {
const workLists = params.workLists;
const context = params.context;
const RuleCondition = imports.rulesConfig.RuleCondition;
const WorkItem = imports.models.WorkItem;
const needsAssessment = (workLists) => {
const enrolToMotherProgram = () => workLists.addItemsToCurrentWorkList(
new WorkItem("31af5921-368f-4cbd-a830-40f38d1c73c3",
WorkItem.type.PROGRAM_ENROLMENT,
{
programName: "Mother",
subjectUUID: _.get(context, "entity.programEnrolment.individual.uuid")
}));
const programEncounter = context.entity;
const ruleCondition = new RuleCondition({programEncounter})
.when
.valueInEncounter("Whether currently pregnant").is.yes
.and.whenItem(!_.some(programEncounter.programEnrolment.individual.enrolments,
enrolment => !enrolment.voided && enrolment.program.name === "Mother")).is.truthy;
if (ruleCondition.matches()) {
enrolToMotherProgram();
}
};
//Move mother PNC encounter to the end, and insert registration and enrolment of child.
const delivery = (workLists) => {
const currentWorkList = workLists.currentWorkList;
//Remove ANC scheduled visits if any
const positionOfANC = _.findIndex(currentWorkList.workItems,
(workItem) => workItem.type === WorkItem.type.PROGRAM_ENCOUNTER && workItem.parameters.encounterType === "ANC ASHA");
if (positionOfANC !== -1) {
currentWorkList.workItems.splice(positionOfANC, 1);
}
let splicePosition = currentWorkList.findWorkItemIndex(currentWorkList.currentWorkItem.id) + 1;
currentWorkList.workItems.splice(
splicePosition,
0,
new WorkItem("27b873ad-063e-4f18-a534-f8367de917b5", WorkItem.type.REGISTRATION, {
subjectTypeName: "Individual",
saveAndProceedLabel: "registerAChild"
}),
new WorkItem("cbe9dd3b-8f10-487a-bc30-39a18f6fc5bd", WorkItem.type.PROGRAM_ENROLMENT, {
programName: "Child"
})
);
return workLists;
};
const pnc = (workLists) => {
const workItems = workLists.currentWorkList.workItems;
const pncWorkItemIndex = workItems.findIndex(workItem => workItem.parameters.encounterType === "PNC");
let neonatalWorkItemIndex = workItems.findIndex(workItem => workItem.parameters.encounterType === "Neonatal");
if (pncWorkItemIndex !== -1 && neonatalWorkItemIndex !== -1) {
const [pncWorkItem] = workItems.splice(pncWorkItemIndex, 1);
//Recalculate since this would have changed since last splice
neonatalWorkItemIndex = workItems.findIndex(workItem => workItem.parameters.encounterType === "Neonatal");
workItems.splice(neonatalWorkItemIndex, 0, pncWorkItem);
}
return workLists;
};
const programEnrolment = (workLists) => {
const currentWorkItem = workLists.getCurrentWorkItem();
if (currentWorkItem.parameters.programName === "Child") {
return pnc(workLists);
}
return workLists;
};
const programEncounter = (workLists) => {
const currentWorkItem = workLists.getCurrentWorkItem();
switch (currentWorkItem.parameters.encounterType) {
case "Monthly needs assessment": {
return needsAssessment(workLists);
}
case "Delivery": {
return delivery(workLists);
}
default: {
return workLists;
}
}
};
const main = () => {
const currentWorkItem = workLists.getCurrentWorkItem();
if (currentWorkItem.type === WorkItem.type.PROGRAM_ENROLMENT) {
programEnrolment(workLists);
}
if (currentWorkItem.type === WorkItem.type.PROGRAM_ENCOUNTER) {
programEncounter(workLists);
}
return workLists;
};
return main();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment