-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMOH513-Enroll-Household-in-SF.js
627 lines (616 loc) · 23.3 KB
/
MOH513-Enroll-Household-in-SF.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//MOH513 Enroll Household Form
//Alters CommCare Person arrays so that they are formatted as arrays instead of just single values.
//First we get the Village Location record from Salesforcee
query(
`SELECT Id, Parent_Geographic_Area__c, Parent_Geographic_Area__r.Name, Parent_Geographic_Area__r.Parent_Geographic_Area__c FROM Location__c
WHERE CommCare_User_ID__c = '${dataValue('form.location_id')(state)}'`
);
alterState(state => ({
...state,
data: {
...state.data,
villageNewId:
state.references[0].records && state.references[0].records.length !== 0
? state.references[0].records[0].Id
: undefined,
areaNewId:
state.references[0].records && state.references[0].records.length !== 0
? state.references[0].records[0].Parent_Geographic_Area__c
: undefined,
catchmentNewId:
state.references[0].records && state.references[0].records.length !== 0
? state.references[0].records[0].Parent_Geographic_Area__r
.Parent_Geographic_Area__c
: undefined,
},
}));
alterState(state => {
//console.log('Area: ', state.data.areaNewId);
const person = state.data.form.Person;
if (!Array.isArray(person)) {
state.data.form.Person = [person];
}
titleCase = str => {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
splitStr[i] =
splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
return splitStr.join(' ');
};
const reasonMapping = {
pregnant: 'The client is pregnant',
intentions_of_getting_pregnant: 'Intentions of getting pregnant',
lack_of_access_to_fp_information: 'Lack of access to FP information',
not_sexually_active: 'The client is not sexually active',
other_barriers_culture_male_partners_parents_etc:
'Other barriers (culture, male partners, parents, etc)',
no_access_to_fp_services_hospitals:
'Lack of hospitals or places where FP services can be accessed',
not_willing_to_use_fp_due_to_negative_effects_myths_and_misconceptions:
'Myths and misconceptions',
barriers_at_service_delivery_points: 'Barriers at service delivery points',
};
state.area = state.data.form.area;
state.catchment = state.data.form.catchment;
return { ...state, reasonMapping };
});
//Upserting Household, checks if Household exists via MOH Household Code
upsertIf((state.data.metadata.username!=='openfn.test'&& state.data.metadata.username!=='test.2021' && state.data.form.test_user!=='Yes'),
'Household__c',
'CommCare_Code__c',
fields(
field('CommCare_Username__c', dataValue('form.meta.username')),
field('MOH_household_code__c', dataValue('form.moh_code')),
field('CommCare_Code__c', dataValue('form.case.@case_id')),
field('Source__c', true),
field('Household_CHW__c', state => {
var chw = dataValue('form.CHW_ID')(state);
return chw === 'a030800001zQrk'
? 'a030800001zQrk5'
: chw
? chw
: undefined;
}),
//field('Household_CHW__c', 'a031x000002S9lm'), //HARDCODED FOR SANDBOX TESTING --> To replace with line above
field('Catchment__c', state => {
var catchment = dataValue('form.catchment')(state);
var catchmentNew = dataValue('catchmentNewId')(state);
return catchmentNew !== '' || catchmentNew !== undefined
? catchmentNew
: catchment;
}),
field('Area__c', state => {
var area = dataValue('form.area')(state);
var areaNew = dataValue('areaNewId')(state);
return areaNew !== '' || areaNew !== undefined ? areaNew : area;
}),
relationship(
//new village location mapping
'Village__r',
'CommCare_User_ID__c',
dataValue('form.location_id')
),
field('Household_village__c', dataValue('form.village')),
field('Deaths_in_the_last_6_months__c', state => {
var death = dataValue(
'form.Household_Information.deaths_in_past_6_months'
)(state);
return death > 0 ? 'Yes' : 'No';
}),
field(
'Access_to_safe_water__c',
dataValue('form.Household_Information.Safe_Water')
),
field(
'Treats_Drinking_Water__c',
dataValue('form.Household_Information.Treats_Drinking_Water')
),
field(
'Tippy_Tap__c',
dataValue('form.Household_Information.Active_Handwashing_Station')
),
field(
'Pit_Latrine__c',
dataValue('form.Household_Information.Functional_Latrine')
),
field(
'Rubbish_Pit__c',
dataValue('form.Household_Information.Rubbish_Pit')
),
field(
'Drying_Rack__c',
dataValue('form.Household_Information.Drying_Rack')
),
field(
'Kitchen_Garden__c',
dataValue('form.Household_Information.Kitchen_Garden')
),
field(
'Cookstove__c',
dataValue('form.Household_Information.Improved_Cooking_Method')
),
field('Clothe__c', dataValue('form.Household_Information.Clothesline')),
field(
'WASH_Trained__c',
dataValue('form.Household_Information.WASH_Trained')
),
field(
'Total_household_people__c',
dataValue('form.Total_Number_of_Members')
),
field('Health_insurance__c', dataValue('form.health_insurace_cover')),
field(
'Health_insurance_active_status__c',
dataValue('form.healthinsurance_active')
),
field('Health_insurance_type__c', state => {
var status = dataValue('form.health_insurance')(state);
return status && status === 'other_please_specify_if_active'
? 'Other'
: status === 'nhif'
? 'NHIF'
: status === 'Linda_mama' || 'linda_mama'
? 'Linda mama'
: status;
}),
field(
'Other_Health_Insurance__c',
dataValue('form.if_other_please_specify')
),
field('Work_with_TBA__c', dataValue('form.tba')),
field('TBA_name__c', dataValue('form.which_tba')),
field('Last_Modified_Date_CommCare__c', dataValue('server_modified_on')),
field('Case_Closed_Date__c', state => {
var closed = dataValue('form.case.update.closed')(state);
var date = dataValue('server_modified_on')(state);
return closed && closed == true ? date : undefined;
})
)
);
//Upsert Person via CommCare case ID for each person enrolled
alterState(state => {
if (dataValue('form.Person[0].Source')(state) == 1) {
return beta.each(
//dataPath('form.Person[*]'),
merge(
dataPath('form.Person[*]'),
fields(
field('form.catchment', dataValue('form.catchment')),
field(
'form.location_info.catchment_name',
dataValue('form.location_info.catchment_name')
),
field(
'form.location_info.area_name',
dataValue('form.location_info.area_name')
),
field('areaNewId', dataValue('areaNewId')),
field('form.area', dataValue('form.area')),
field('catchmentNewId', dataValue('catchmentNewId')),
field('catchmentNewName', dataValue('catchmentNewName')),
field('username', dataValue('metadata.username'))
)
),
upsertIf((state.data.metadata.username!=='openfn.test'&& state.data.metadata.username!=='test.2021'),
'Person__c',
'CommCare_ID__c',
fields(
field('Catchment__c', dataValue('catchmentNewId')),
field('Area__c', state => {
var area = dataValue('form.area')(state);
var areaNew = dataValue('areaNewId')(state);
return areaNew !== '' || areaNew !== undefined ? areaNew : area;
}),
field('CommCare_ID__c', dataValue('case.@case_id')),
field('CommCare_HH_Code__c', dataValue('case.index.parent.#text')),
relationship(
'Household__r',
'CommCare_Code__c',
dataValue('case.index.parent.#text')
),
relationship('RecordType', 'Name', state => {
var rt = dataValue('Basic_Information.Record_Type')(state);
var status = dataValue('Basic_Information.Child_Status')(state);
//Here we set set RT to 'Child' if unborn child
return status == 'Unborn'
? 'Child'
: rt.toString().replace(/_/g, ' ');
}),
field('Name', state => {
var status = dataValue('Basic_Information.Child_Status')(state);
var name1 = dataValue('Basic_Information.Name')(state);
var name2 =
name1 === undefined || name1 === ''
? 'Unborn Child'
: name1.replace(/\w\S*/g, function (txt) {
return (
txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);
});
return status !== 'Unborn' ? name2 : 'Unborn Child';
}),
field('Source__c', true),
field('Client_Status__c', 'Active'),
field('Relation_to_the_head_of_the_household__c', state => {
var relation = dataValue('Basic_Information.relation_to_hh')(state);
var toTitleCase =
relation !== undefined
? relation.toString().replace(/_/g, ' ')
: null;
return toTitleCase !== null
? toTitleCase.charAt(0).toUpperCase() + toTitleCase.slice(1)
: null;
}),
field('Active_TT5_Mother__c', state => {
var preg = dataValue('TT5.Mother_Information.Pregnant')(state);
return preg == 'Yes' ? 'Yes' : null;
}),
field('TT5_Mother_Registrant__c', state => {
var preg = dataValue('TT5.Mother_Information.Pregnant')(state);
return preg == 'Yes' ? 'Yes' : null;
}),
field('Active_in_Thrive_Thru_5__c', state => {
var age = dataValue('Basic_Information.age')(state);
var preg = dataValue('TT5.Mother_Information.Pregnant')(state);
return age < 5 || preg == 'Yes' ? 'Yes' : 'No';
}),
field('Active_in_HAWI__c', state => {
var status = dataValue('Basic_Information.person_info.hiv_status')(
state
);
return status == 'positive' ? 'Yes' : 'No';
}),
field('Enrollment_Date__c', state => {
var age = dataValue('Basic_Information.age')(state);
var date = dataValue('Date')(state);
var preg = dataValue('TT5.Mother_Information.Pregnant')(state);
return age < 5 || preg == 'Yes' ? date : null;
}),
field('HAWI_Enrollment_Date__c', state => {
var status = dataValue('Basic_Information.person_info.hiv_status')(
state
);
var date = dataValue('Date')(state);
return status == 'positive' ? date : null;
}),
field('Thrive_Thru_5_Registrant__c', state => {
var age = dataValue('Basic_Information.age')(state);
var preg = dataValue('TT5.Mother_Information.Pregnant')(state);
return age < 5 || preg == 'Yes' ? 'Yes' : 'No';
}),
field('HAWI_Registrant__c', state => {
var status = dataValue('Basic_Information.person_info.hiv_status')(
state
);
return status == 'positive' ? 'Yes' : 'No';
}),
field('Date_of_Birth__c', dataValue('Basic_Information.DOB')),
field('Child_Status__c', state => {
var dob = dataValue('Basic_Information.DOB')(state);
var status = dataValue('Basic_Information.Child_Status')(state);
return dob !== undefined || status == 'Born' ? 'Born' : 'Unborn'; //what about deceased?
}),
field('Gender__c', dataValue('Basic_Information.Gender')),
field(
'Birth_Certificate__c',
dataValue('Basic_Information.birth_certificate')
),
field('Education_Level__c', state => {
var edu = dataValue('Basic_Information.Education_Level')(state);
return edu !== undefined ? edu.toString().replace(/_/g, ' ') : null;
}),
field(
'Currently_enrolled_in_school__c',
dataValue('Basic_Information.enrolled_in_school')
),
field(
'Telephone__c',
dataValue(
'Basic_Information.Contact_Info.contact_phone_number_short'
)
),
field('Family_Planning__c', state => {
var plan = dataValue(
'Basic_Information.family_planning.Currently_on_family_planning'
)(state);
return plan ? 'Yes' : plan;
}),
field('Family_Planning_Method__c', state => {
var method = dataValue(
'Basic_Information.family_planning.Family_Planning_Method'
)(state);
return method ? method.toString().replace(/_/g, ' ') : method;
}),
field('Reasons_for_not_taking_FP_method__c', state => {
var reason = dataValue(
'Basic_Information.family_planning.No_FPmethod_reason'
)(state);
return reason ? state.reasonMapping[reason] : '';
}),
field(
'Use_mosquito_net__c',
dataValue('Basic_Information.person_info.sleep_under_net')
),
field(
'Two_weeks_or_more_cough__c',
dataValue('Basic_Information.person_info.cough_for_2wks')
),
field('Chronic_illness__c', state => {
var value = dataValue(
'Basic_Information.person_info.chronic_illness'
)(state);
var illness =
value !== undefined
? value
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(';')
: null;
return illness !== null
? illness.toString().replace(/_/g, ' ')
: null;
}),
field(
'Knowledge_of_HIV_status__c',
dataValue('Basic_Information.person_info.known_hiv_status')
),
field(
'HIV_Status__c',
dataValue('Basic_Information.person_info.hiv_status')
),
field('Disability__c', state => {
var disability = dataValue(
'Basic_Information.person_info.disability'
)(state);
var toTitleCase =
disability !== undefined
? disability
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(';')
: null;
return toTitleCase;
}),
field('Other_disability__c', state => {
var disability = dataValue(
'Basic_Information.person_info.other_disability'
)(state);
var toTitleCase =
disability !== undefined
? disability
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(';')
: null;
return toTitleCase;
}),
field(
'Unique_Patient_Code__c',
dataValue('HAWI.Unique_Patient_Code')
),
field(
'Active_in_Support_Group__c',
dataValue('HAWI.Active_in_Support_Group')
),
field('Currently_on_ART_s__c', dataValue('HAWI.ART')),
field('ART_Regimen__c', dataValue('form.Person.HAWI.ARVs')),
field('Preferred_Care_Facility__c', state => {
var facility = dataValue('HAWI.Preferred_Care_Facility')(state);
return facility !== undefined
? facility.toString().replace(/_/g, ' ')
: null;
}),
field(
'MCH_booklet__c',
dataValue('TT5.Mother_Information.mch_booklet')
),
field('LMP__c', dataValue('TT5.Child_Information.ANCs.LMP')),
field('ANC_1__c', dataValue('TT5.Child_Information.ANCs.ANC_1')),
field('ANC_2__c', dataValue('TT5.Child_Information.ANCs.ANC_2')),
field('ANC_3__c', dataValue('TT5.Child_Information.ANCs.ANC_3')),
field('ANC_4__c', dataValue('TT5.Child_Information.ANCs.ANC_4')),
field('ANC_5__c', dataValue('TT5.Child_Information.ANCs.ANC_5')),
field('Delivery_Facility__c', state => {
var facility = dataValue(
'TT5.Child_Information.Delivery_Information.Birth_Facility'
)(state);
return facility !== undefined
? facility.toString().replace(/_/g, ' ')
: null;
}),
field(
'Delivery_Facility_Other__c',
dataValue('TT5.Child_Information.Delivery_Information.Other')
),
field('Place_of_Delivery__c', state => {
var facility = dataValue(
'TT5.Child_Information.Delivery_Information.Skilled_Unskilled'
)(state);
if (facility !== undefined) {
return facility == 'Skilled' ? 'Facility' : 'Home';
}
return facility;
}),
field('Place_of_Delivery__c', state => {
var facility = dataValue(
'TT5.Child_Information.Delivery_Information.Delivery_Type'
)(state);
if (facility !== undefined) {
return facility == 'Skilled' ? 'Facility' : 'Home';
}
return facility;
}),
field('BCG__c', dataValue('TT5.Child_Information.Immunizations.BCG')),
field(
'OPV_0__c',
dataValue('TT5.Child_Information.Immunizations.OPV_0')
),
field(
'OPV_1__c',
dataValue('TT5.Child_Information.Immunizations.OPV_PCV_Penta_1')
),
field(
'OPV_2__c',
dataValue('TT5.Child_Information.Immunizations.OPV_PCV_Penta_2')
),
field(
'OPV_3__c',
dataValue('TT5.Child_Information.Immunizations.OPV_PCV_Penta_3')
),
field(
'Measles_6__c',
dataValue('TT5.Child_Information.Immunizations.Measles_6')
),
field(
'Measles_9__c',
dataValue('TT5.Child_Information.Immunizations.Measles_9')
),
field(
'Measles_18__c',
dataValue('TT5.Child_Information.Immunizations.Measles_18')
),
field(
'Vitamin_A__c',
dataValue('TT5.Child_Information.nutrition.vitamin_a')
),
field(
'Food_groups_3_times_a_day__c',
dataValue('TT5.Child_Information.nutrition.food_groups')
),
field(
'Initial_MUAC__c',
dataValue('TT5.Child_Information.nutrition.MUAC')
),
field(
'Exclusive_Breastfeeding__c',
dataValue(
'TT5.Child_Information.Exclusive_Breastfeeding.Exclusive_Breastfeeding'
)
),
field('Pregnant__c', state => {
var preg = dataValue('TT5.Mother_Information.Pregnant')(state);
return preg == 'Yes' ? true : false;
}),
field(
'Gravida__c',
dataValue('TT5.Mother_Information.Pregnancy_Information.Gravida')
),
field(
'Parity__c',
dataValue('TT5.Mother_Information.Pregnancy_Information.Parity')
),
field(
'Nutrition_referral__c',
dataValue('TT5.Child_Information.nutrition.Referral')
),
field(
'Nutrition_referral_date__c',
dataValue('TT5.Child_Information.nutrition.date_malnutrition')
),
field(
'HIV_counseling_and_testing_referral_date__c',
dataValue('Basic_Information.person_info.when_hiv')
),
field(
'Received_pregnancy_test__c',
dataValue(
'Basic_Information.family_planning.did_you_adminsiter_a_pregnancy_test'
)
),
field(
'Pregnancy_test_result__c',
dataValue('Basic_Information.family_planning.pregnancy_test_result')
),
field(
'Pregnancy_referral__c',
dataValue('Basic_Information.family_planning.refer_preg')
),
field(
'Pregnancy_referral_date__c',
dataValue('Basic_Information.family_planning.referal_pregnancy')
),
field('Family_Planning__c', state => {
var plan = dataValue(
'Basic_Information.family_planning.Currently_on_family_planning'
)(state);
return plan ? 'Yes' : plan;
}),
field('Family_Planning_Method__c', state => {
var method = dataValue(
'Basic_Information.family_planning.Family_Planning_Method'
)(state);
return method ? method.toString().replace(/_/g, ' ') : method;
}),
field('Reason_for_not_taking_a_pregnancy_test__c', state => {
var reason = dataValue(
'Basic_Information.family_planning.No_Preg_Test'
)(state);
return reason ? reason.toString().replace(/_/g, ' ') : reason;
}),
field('Last_Modified_Date_CommCare__c', state.data.server_modified_on)
)
)
)(state);
}
console.log('No first person found, not upserting.');
return state;
});
each(
merge(
dataPath('$.form.household_deaths.deaths[*]'),
fields(
field('caseId', dataValue('form.case.@case_id')),
field('catchment', dataValue('form.catchment')),
field('Date', dataValue('form.Date'))
)
),
upsertIf(
state.data.form.household_deaths &&
state.data.form.household_deaths.deaths_in_past_6_months > 0, //only insert deceased Person if deaths
'Person__c',
'CommCare_ID__c',
fields(
field('CommCare_ID__c', state => {
var age = dataValue('age_dead')(state);
return `${state.data.caseId}${age}`;
}),
field('CommCare_HH_Code__c', dataValue('caseId')),
relationship('RecordType', 'Name', state => {
var age = dataValue('age_dead')(state);
var gender = dataValue('gender_dead')(state);
var rt = '';
if (age < 5) {
rt = 'Child';
} else if (age < 18) {
rt = 'Youth';
} else if (gender === 'female') {
rt = 'Female Adult';
} else {
rt = 'Male Adult';
}
return rt;
}),
field('Name', 'Deceased Person'),
field('Source__c', true),
field('Client_Status__c', 'Deceased'),
field('Dead_age__c', dataValue('age_dead')),
field('Cause_of_Death__c', state => {
var cause = dataValue('cause_of_death_dead')(state);
return cause !== undefined ? cause.toString().replace(/_/g, ' ') : null;
}),
field('Verbal_autopsy__c', dataValue('verbal_autopsy')),
field('Client_Status__c', 'Deceased'),
field('Active_in_Thrive_Thru_5__c', 'No'),
field('Active_in_HAWI__c', 'No'),
field('Active_TT5_Mother__c', 'No'),
field('TT5_Mother_Registrant__c', 'No'),
field('Date_of_Death__c', dataValue('Date')),
field('Inactive_Date__c', dataValue('Date')),
field('Last_Modified_Date_CommCare__c', state.data.server_modified_on)
)
)
);