-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleHelper.js
125 lines (104 loc) · 5.37 KB
/
RuleHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {FormElementStatusBuilder, FormElementStatus, VisitScheduleBuilder} from 'rules-config/rules';
import lib from './lib';
import moment from 'moment';
const addSchedule = (builder, visitSchedule) => {
if (moment().isSameOrBefore(visitSchedule.maxDate, 'day')) {
builder.add(visitSchedule);
}
};
const addSchedules = (builder, visitSchedules) => visitSchedules.forEach((vs) => addSchedule(builder, vs));
class RuleHelper {
static getAgeOfYoungestChildInMonths(individual, referenceDate) {
const youngestChild = lib.C.getYoungestChild(individual);
return youngestChild && youngestChild.getAgeInMonths(referenceDate);
};
static encounterCodedObsHas(programEncounter, formElement, conceptName, ...answerConceptNames) {
let statusBuilder = new FormElementStatusBuilder({programEncounter, formElement});
statusBuilder.show().when.valueInEncounter(conceptName).containsAnyAnswerConceptName(...answerConceptNames);
return statusBuilder.build();
}
static encounterCodedObsNotHave(programEncounter, formElement, conceptName, answerConceptName) {
let statusBuilder = new FormElementStatusBuilder({programEncounter, formElement});
statusBuilder.show().when.valueInEncounter(conceptName).not.containsAnswerConceptName(answerConceptName);
return statusBuilder.build();
}
static generalObservationMatcher(context, scope, conceptName, matchingFn, [...answers] /*always array*/) {
let statusBuilder = new FormElementStatusBuilder(context);
statusBuilder.show().when['valueIn' + scope](conceptName)[matchingFn](...answers);
return statusBuilder.build();
}
static Scope = {
Enrolment: 'Enrolment',
Encounter: 'Encounter',
EntireEnrolment: 'EntireEnrolment'
};
static _calculateBMI = (weight, height) => {
return Math.ceil((weight / Math.pow(height, 2)) * 10000, 1);
};
static createBMIFormElementStatus(height, weight, formElement) {
let value;
height = height && height.getValue();
weight = weight && weight.getValue();
if (Number.isFinite(weight) && Number.isFinite(height)) {
value = lib.C.calculateBMI(weight, height);
}
return new FormElementStatus(formElement.uuid, true, value);
}
static removeRecommendation(decisions, groupName, recommendationName, reason) {
const defaultVal = {name: recommendationName, value: []};
const group = decisions[groupName] = decisions[groupName] || [];
const recommendation = group.find((d) => d.name === recommendationName) || (group.push(defaultVal), defaultVal);
recommendation.value = recommendation.value || [];
const withRemoved = recommendation.value.filter((r) => r.toUpperCase() !== reason.toUpperCase());
const removedOrNot = (withRemoved.length !== recommendation.value.length);
recommendation.value = withRemoved;
return removedOrNot;
}
static addRecommendation(decisions, groupName, recommendationName, reason) {
const defaultVal = {name: recommendationName, value: []};
const group = decisions[groupName] = decisions[groupName] || [];
const recommendation = group.find((d) => d.name === recommendationName) || (group.push(defaultVal), defaultVal);
recommendation.value = recommendation.value || [];
recommendation.value.push(reason);
return decisions;
}
static createProgramEncounterVisitScheduleBuilder(programEncounter, visitSchedules) {
const scheduleBuilder = new VisitScheduleBuilder({
programEncounter,
programEnrolment: programEncounter.programEnrolment
});
addSchedules(scheduleBuilder, visitSchedules);
return scheduleBuilder;
}
static createEnrolmentScheduleBuilder(programEnrolment, visitSchedules) {
const scheduleBuilder = new VisitScheduleBuilder({
programEnrolment,
});
addSchedules(scheduleBuilder, visitSchedules);
return scheduleBuilder;
}
static addSchedule(scheduleBuilder, name, encounterType, earliestDate, numberOfDaysForMaxOffset) {
const maxDate = moment(earliestDate).add(numberOfDaysForMaxOffset, 'days').toDate();
addSchedule(scheduleBuilder, {name, encounterType, earliestDate, maxDate});
}
static justSchedule(scheduleBuilder, visit) {
addSchedule(scheduleBuilder, visit);
}
static blindAddSchedule(scheduleBuilder, name, encounterType, earliestDate, numberOfDaysForMaxOffset) {
const maxDate = moment(earliestDate).add(numberOfDaysForMaxOffset, 'days').toDate();
scheduleBuilder.add({name, encounterType, earliestDate, maxDate});
}
static hideFormElementGroup(formElementGroup) {
return formElementGroup.getFormElements().map(fe => new FormElementStatus(fe.uuid, false));
}
static scheduleOneVisit(scheduleBuilder, visitName, encounterTypeName, earliestDate, numberOfDaysForMaxOffset) {
this.addSchedule(scheduleBuilder, visitName, encounterTypeName, earliestDate, numberOfDaysForMaxOffset);
return scheduleBuilder.getAllUnique("encounterType");
}
static firstOfNextMonth(realEventDate) {
const currentDate = moment(realEventDate).date();
const month = moment(realEventDate).month() + 1;
return moment(realEventDate).month(month).date(1).toDate();
};
}
module.exports = RuleHelper;