-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUpsert-Person-Case-Update.js
805 lines (765 loc) · 30.2 KB
/
Upsert-Person-Case-Update.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// create constants and functions
fn(state => {
state.cleanChoice = function (state, choice) {
if (choice) {
return choice.charAt(0).toUpperCase() + choice.slice(1).replace('_', ' ');
} else {
return '';
}
};
state.handleMultiSelect = function (state, multiField) {
return multiField
? multiField
.replace(/ /gi, ';')
.toLowerCase()
.split(';')
.map(value => {
return (
value.charAt(0).toUpperCase() + value.slice(1).replace('_', ' ')
);
})
.join(';')
: '';
};
const pregDangerMap = {
Vaginal_Bleeding: 'Vaginal Bleeding',
Water_Breaks: 'Water Breaks before Time of Delivery',
Prolonged_Labour: 'Prolonged Labour over 12 Hours',
Convulsions: 'Convulsions or Fits',
Abdominal_Pain: 'Severe Abdominal Pain before Delivery',
High_Fever: 'High Fever',
Low_Baby_Movement: 'Feeling the Baby move less or not at all',
Swelling: 'Swelling of Face and Hands',
Severe_Headache: 'Severe or Continuous Headache for more than 12 hours',
Severe_Vomiting: 'Severe or Continuous Vomiting',
none: 'None',
};
const counselMap = {
anc_visits: 'ANC Visits',
early_initiation_of_anc_less_than_3_months:
'Early initiation of ANC (less than 3 months)',
completing_recomended_anc_visits: 'Completing recomended ANC visits',
danger_signs: 'Danger signs',
skilled_birth: 'Skilled birth',
immunization: 'Immunization',
individual_birth_plan: 'Individual Birth Plan',
emergency_preparedness: 'Emergency preparedness',
childcare_and_affection: 'Childcare and affection',
nutrition_counseling: 'Nutrition counseling',
growth_monitoring: 'Growth monitoring',
exclusive_breastfeeding: 'Exclusive breastfeeding',
complementary_feeding: 'Complementary feeding',
sleeping_under_llitn: 'Sleeping under LLITN',
knowing_hiv_status: 'Knowing HIV status',
indoor_pollution: 'Indoor pollution',
personal_hygiene: 'Personal Hygiene',
safe_drinking_water: 'Safe drinking water',
safe_disposal_of_human_waste: 'Safe disposal of human waste',
};
const serviceMap = {
Scheduled_PSC_Apt: 'Scheduled PSC Apt',
Adverse_Drug_Reaction_Side_Effect: 'Adverse Drug Reaction/Side Effect',
Malnutrition: 'Malnutrition',
Malaria: 'Malaria',
TB: 'TB',
Treatment_for_Other_OIs: 'Treatment for other Ols',
ARI: 'ARI',
Anemia: 'Anemia',
Diarrhea: 'Diarrhea',
Pregnancy_Care: 'Pregnancy Care (ANC)',
Family_Planning: 'Family Planning (FP)',
Preconception_Counseling: 'Preconception Counseling',
Injury: 'Injury',
Other: 'Other',
};
const reasonMapping = {
lack_of_access_to_fp_information: 'Lack of access to FP information',
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',
pregnant: 'The client is pregnant',
intentions_of_getting_pregnant: 'Intentions of getting pregnant',
not_sexually_active: 'The client is not sexually active',
other_barriers_culture_male_partners_parents_etc:
'Other barriers (culture, male partners, parents, etc)',
};
const milestoneTypeMap = {
cognitive_delays_learning_difficulties:
'Cognitive Delays Learning Difficulties',
motor_delays: 'Motor Delays',
speech_and_language_delay: 'Delay Speech and Language Delay',
social_and_emotional: 'Social and emotional',
};
const milestoneMap = {
movement: 'Movement',
hearing: 'Hearing',
communication: 'Communication',
seeing: 'Seeing',
cognitive_delays: 'Cognitive Delays',
play: 'Play',
};
const nutritionMap = {
severe: 'Severely Malnourished',
moderate: 'Moderately Malnourished',
normal: 'Normal',
};
const fpMethodMap = {
male_condoms: 'Male condoms',
female_condoms: 'Female condoms',
pop: 'POP',
coc: 'COC',
emergency_pills: 'Emergency pills',
none: 'None',
};
return {
...state,
counselMap,
serviceMap,
reasonMapping,
milestoneTypeMap,
milestoneMap,
nutritionMap,
pregDangerMap,
fpMethodMap,
};
});
// get data from SF
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(
'properties.owner_id'
)(state)}'`
);
// build IDs from queried SF data
fn(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
? state.references[0].records[0].Parent_Geographic_Area__r
.Parent_Geographic_Area__c
: undefined
: undefined,
},
}));
upsertIf(
state.data.properties.commcare_username !== 'test.2021' &&
state.data.properties.test_user !== 'Yes',
'Household__c',
'CommCare_Code__c',
fields(
field('CommCare_Code__c', state => {
return (
dataValue('indices.parent.case_id')(state) ||
dataValue('properties.parent_id')(state)
);
})
)
),
// build sfRecord before upserting
fn(state => {
// This mapping was initially constructed with fields(field(), ...) syntax. We
// preserve it here and use "expandReferences" but could also refactor this to
// use standard object syntax, as Salesforce looks for { k: v, ... }.
const originalMapping = fields(
/* field(
'deworming_medication__c',
dataValue('form.TT5.Child_Information.Deworming')
),depracated field*/
field('Source__c', 1),
field('CommCare_ID__c', dataValue('case_id')),
relationship(
'Household__r',
'CommCare_Code__c', state => {
// (dataValue('properties.parent_id') || dataValue('indices.parent.case_id'))
return (
dataValue('properties.parent_id')(state) ||
dataValue('indices.parent.case_id')(state)
);
}),
field('commcare_location_id__c',dataValue('properties.commcare_location_id')),
field('CommCare_Username__c',dataValue('properties.commcare_username')),
field('Telephone__c', dataValue('properties.contact_phone_number')),
field(
'Consent_for_data_use__c',
dataValue('properties.data_sharing_consent')
),
field('CommCare_HH_Code__c', dataValue('indices.parent.case_id')),
field('Client_Status__c', dataValue('properties.Client_Status')),
field('Catchment__c', dataValue('catchmentNewId')),
field('Area__c', dataValue('areaNewId')),
field('Household_Village__c', dataValue('villageNewId')),
field('Name', state => {
var name1 = dataValue('properties.Person_Name')(state); //check
var unborn = dataValue('properties.name')(state); //check
var name2 =
name1 === undefined || name1 === '' || name1 === null
? unborn
: name1.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
return name1 !== null ? name2 : 'Unborn Child';
}),
field('Chronic_illness__c', state => {
var choice = dataValue(
'properties.please_specify_which_chronic_illness_the_person_has'
)(state);
var choice2 = state.handleMultiSelect(state, choice);
return choice2 ? choice2.replace(/_/g, ' ') : '';
}),
field(
'Currently_enrolled_in_school__c',
dataValue('properties.enrolled_in_school')
),
field('Education_Level__c', state => {
var level = dataValue('properties.Education_Level')(state);
return level ? level.toString().replace(/_/g, ' ') : null;
}),
field('Relation_to_the_head_of_the_household__c', state => {
var relation = dataValue('properties.relation_to_hh')(state);
if (relation) {
relation = relation.toString().replace(/_/g, ' ');
var toTitleCase = relation.charAt(0).toUpperCase() + relation.slice(1);
return toTitleCase;
}
return null;
}),
field('Gender__c',dataValue('properties.Gender')),
field('Disability__c', state => {
var disability = dataValue('properties.disability')(state);
var toTitleCase =
disability !== undefined
? disability
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(';')
: null;
return toTitleCase;
}), //need case property
field('Other_disability__c', state => {
var disability = dataValue('properties.other_disability')(state);
var toTitleCase =
disability !== undefined
? disability
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(';')
: null;
return toTitleCase;
}), //need case property
field('Use_mosquito_net__c', dataValue('properties.sleep_under_net')), //need case property
// field('Birth_Certificate__c',dataValue('properties.birth_certificate')),
field('Birth_Certificate__c', dataValue('properties.birth_certificate')),
field('Child_Status__c', state => {
var status = dataValue('properties.Child_Status')(state);
var rt = dataValue('properties.Record_Type')(state); //check that this is the right one
if (status && rt === 'Unborn') {
status = 'Unborn';
} else if (status && rt === 'Born') {
status = 'Born';
}
return status;
}),
//===================================================//
relationship('RecordType', 'Name', state => {
var rt = dataValue('properties.Record_Type')(state);
return rt === 'Unborn' || rt === ''
? 'Child'
: rt.toString().replace(/_/g, ' '); //convert Unborn children to Child RT
}),
//TT5 Mother Information
field('MCH_booklet__c', dataValue('properties.mch_booklet')), //need to create a case property
field('Reason_for_not_taking_a_pregnancy_test__c', state => {
var reason = dataValue('properties.No_Preg_Test')(state);
return reason ? reason.toString().replace(/_/g, ' ') : undefined;
}),
field('Pregnancy_danger_signs__c', state => {
var signs = dataValue('properties.pregnancy_danger_signs')(state);
return signs ? state.pregDangerMap[signs] : undefined;
}),
field(
'Individual_birth_plan_counselling__c',
dataValue('properties.individual_birth_plan')
),
field('Child_Danger_Signs__c', state => {
var signs = dataValue('properties.Other_Danger_Signs')(state);
return signs
? signs
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(';')
.toString()
.replace(/_/g, ' ')
: signs;
}),
//HAWI
field(
'Unique_Patient_Code__c',
dataValue('properties.Unique_Patient_Code')
),
field(
'Active_in_Support_Group__c',
dataValue('properties.Active_in_Support_Group')
),
field(
'Preferred_Care_Facility__c',
dataValue('properties.Preferred_Care_Facility')
),
field('Currently_on_ART_s__c', dataValue('properties.ART')),
field('ART_Regimen__c', dataValue('properties.ARVs')),
field('HAWI_Defaulter__c', state => {
var hawi = dataValue('properties.default')(state);
return hawi === 'Yes' ? true : false;
}),
field('Date_of_Default__c', dataValue('properties.date_of_default')),
field('Know_HIV_status__c', dataValue('properties.known_hiv_status')),
field('HIV_Status__c', dataValue('properties.hiv_status')),
/*field('HIV_Status__c', state => {
var status = dataValue('properties.hiv_status')(state);
return status === 'yes'
? 'Known'
: status === 'no'
? 'Unknown'
: undefined;
}),//CHECK MAPPING ON THIS ONE*/
//Illness
field('Persons_temperature__c', dataValue('properties.temperature')),
field(
'Days_since_illness_start__c',
dataValue('properties.duration_of_sickness')
),
field(
'Current_Malaria_Status__c',
dataValue('properties.malaria_test_results')
),
field('Malaria_test__c', dataValue('properties.malaria_test')),
field(
'Last_Malaria_Home_Test__c',
dataValue('properties.malaria_test_date')
),
field('Last_Malaria_Home_Treatment__c', state => {
var choice = dataValue(
'properties.malaria_test_date'
)(state);
return state.cleanChoice(state, choice);
}),
/*field('Current_Malaria_Status__c', dataValue('form.Malaria_Status')),//check
field('Malaria_Facility__c',dataValue('form.treatment_and_tracking.malaria_referral_facility')),
field('Fever_over_7days__c',dataValue('form.treatment_and_tracking.symptoms_check_fever')),//check*/
field('Cough_over_14days__c', dataValue('properties.symptoms_check_cough')),
/*field('Diarrhoea_over_14days__c',dataValue('form.treatment_and_tracking.symptoms_check_diarrhea')),//check
field('Diarrhoea_less_than_14_days__c',dataValue('form.treatment_and_tracking.mild_symptoms_check_diarrhea')),//check*/
field(
'TB_patients_therapy_observed__c',
dataValue('properties.observed_tb_therapy')
),
field('Injuries_or_wounds__c', dataValue('properties.wounds_or_injuries')),
field(
'Pulse_Oximeter__c',
dataValue('properties.pulse_oximeter_available')
),
field(
'Heart_Rate_Pulse_Oximeter__c',
dataValue('properties.heart_rate_pulse_oximeter')
),
field(
'Oxygen_Concentration_Pulse_Oximeter__c',
dataValue('properties.oxygen_concentration')
),
field('Can_child_drink__c', dataValue('properties.can_child_drink')),
// field('Antibiotic_provided_for_fast_breathing__c',dataValue('properties.antibiotic_fast_breathing')),
field('Antibiotic_provided_for_fast_breathing__c', state => {
var choice = dataValue('properties.antibiotic_fast_breathing')(state);
return state.cleanChoice(state, choice);
}),
// field('Antibiotic_provided_for_chest_indrawing__c',dataValue('properties.antibiotic_chest_indrawing')),
field('Antibiotic_provided_for_chest_indrawing__c', state => {
var choice = dataValue('properties.antibiotic_chest_indrawing')(state);
return state.cleanChoice(state, choice);
}),
/*field('Child_zinc__c',dataValue('form.TT5.Child_Information.Clinical_Services.diarrhea_clinic_treatment_zinc')),//check
field('Child_ORS__c',dataValue('form.TT5.Child_Information.Clinical_Services.diarrhea_clinic_treatment_ORS')),//check
field('Childs_breath_per_minute__c',dataValue('form.psbi.breaths_per_minuite')),//check
field('Child_chest_in_drawing__c',dataValue('form.psbi.Child_chest_in_drawing_c')),//check*/
field('Default_on_TB_treatment__c', state => {
var choice = dataValue('properties.default_tb_treatment')(state); //check
return state.cleanChoice(state, choice);
}),
field('Treatment_Distribution__c', state => {
var choice = dataValue('properties.distributed_treatments')(state);
return state.cleanChoice(state, choice);
}), //check
//Delivery
field(
'Immediate_Breastfeeding__c',
dataValue('properties.Breastfeeding_Delivery')
),
field('Place_of_Delivery__c', state => {
var facility = dataValue('properties.Delivery_Type')(state);
return facility === 'Skilled'
? 'Facility'
: facility === 'Unskilled'
? 'Home'
: undefined;
}),
field('Delivery_Facility__c', state => {
var facility = dataValue('properties.Delivery_Facility')(state);
return facility ? facility.toString().replace(/_/g, ' ') : null;
}),
/* field('Delivery_Facility__c', state => {
//HMN 21//11/2022
var birthFacility =dataValue('properties.Birth_Facility')(state);
var deliveryFacility = dataValue('properties.Delivery_Facility')(state);
var facilityOfBirth = dataValue ('properties.Facility_of_Birth')(state);
var faciliy_val =
facilityOfBirth //!== undefined || facilityOfBirth !== '' || facilityOfBirth !== null
? facilityOfBirth
:birthFacility //!== undefined || birthFacility !== '' || birthFacility !== null
? birthFacility
:deliveryFacility //!== undefined || deliveryFacility !== '' || deliveryFacility !== null
? deliveryFacility
:"";
return faciliy_val.toString().replace(/_/g, ' ');
//HMN
}), */
//field('Delivery_Facility__c', state => {
// var facility = dataValue('properties.Birth_Facility')(state);
// return facility ? facility.toString().replace(/_/g, ' ') : null;
//}),
field(
'Delivery_Facility_Other__c',
dataValue('properties.Delivery_Facility_Other')
),
//Family Planning
field('LMP__c', dataValue('properties.LMP')),
field('Family_Planning__c', dataValue('properties.family_planning')),
field(
'Family_Planning_Method__c',
dataValue('properties.family_planning_method')
),
field('FP_Method_Distributed__c', state => {
var status = dataValue('properties.FP_commodity')(state);
var value =
status && status !== ''
? status
.replace(/ /gi, ';')
.split(';')
.map(value => {
return state.fpMethodMap[value] || value;
})
: undefined;
return value ? value.join(';') : undefined;
}),
field('Reasons_for_not_taking_FP_method__c', state => {
var status = dataValue('properties.No_FPmethod_reason')(state);
var value =
status && status !== ''
? status
.replace(/ /gi, ';')
.split(';')
.map(value => {
return state.reasonMapping[value] || value;
})
: undefined;
return value ? value.join(';') : undefined;
}),
field('Pregnant__c', state => {
var preg = dataValue('properties.Pregnant')(state);
return preg === 'Yes' ? true : false;
}),
field('Date_of_Delivery__c', dataValue('properties.delivery_date')),
field(
'Counselled_on_FP_Methods__c',
dataValue('properties.CounselledFP_methods')
),
field('Client_counselled_on__c', state => {
var choices =
dataValue('properties.counsel_topic')(state) ||
dataValue('properties.counsel_topic')(state); //need to create a case property
var choiceGroups = choices ? choices.split(' ') : null;
var choicesMulti = choiceGroups
? choiceGroups
.map(cg => {
return state.counselMap[cg];
})
.join(';')
: choiceGroups;
return choicesMulti;
}), //OpenFn Question - can this be simplified now that this is a case property
field('Client_provided_with_FP__c', state => {
var choice = dataValue(
'properties.was_the_woman_15-49yrs_provided_with_family_planning_commodities_by_chv'
)(state);
return state.cleanChoice(state, choice);
}),
field(
'Received_pregnancy_test__c',
dataValue('properties.did_you_adminsiter_a_pregnancy_test')
),
field(
'Pregnancy_test_result__c',
dataValue('properties.pregnancy_test_result')
),
field('Gravida__c', dataValue('properties.Gravida')),
field('Parity__c', dataValue('properties.Parity')),
//TT5 Child Information
field(
'Exclusive_Breastfeeding__c',
dataValue('properties.Exclusive_Breastfeeding')
),
field(
'Counselled_on_Exclusive_Breastfeeding__c',
dataValue('properties.counseling')
),
field(
'Newborn_visited_48_hours_of_delivery__c',
dataValue('properties.newborn_visited_48_hours_of_delivery')
),
field('Newborn_visit_counselling__c', state => {
var choice = dataValue('properties.did_you_consel_the_mother_on1')(state);
return state.cleanChoice(state, choice);
}),
field(
'mother_visited_48_hours_of_the_delivery__c',
dataValue('properties.visit_mother_48')
),
/*
//HMN -06012023- The object Visit_after_unskilled__c seems to be missing in sfRecord
//Investigation issue https://lwala.atlassian.net/browse/LWAL-716 raised
//Meanwhile this is being removed to allow re-processing
field(
'Visit_after_unskilled__c',
dataValue('properties.visit_24hours_after_unskilled_delivery')
),
*/
field('Mother_visit_counselling__c', state => {
var choice = dataValue('properties.did_you_consel_the_mother_on2')(state);
return state.cleanChoice(state, choice);
}),
/*field('Newborn_visited_48_hours_of_delivery__c',dataValue('properties.newborn_visited_48_hours_of_delivery')), Duplicate Mapping*/
field(
'Newborn_visited_by_a_CHW_within_6_days__c',
dataValue('properties.visit_6_days_from_delivery')
),
//Nutrition
field(
'Caretaker_action_after_muac_screening__c',
dataValue('properties.mother_screened_muac_action')
),
field(
'Caretaker_muac_findings__c',
dataValue('properties.mother_screened_child_muac_result')
),
field('Food_groups_3_times_a_day__c',dataValue('properties.food_groups')),
// field('Caretaker_screened_for_muac_this__c', dataValue('properties.mother_screened_child_muac')),
field('Caretaker_screened_for_muac_this__c', state => {
var choice = dataValue('properties.mother_screened_child_muac')(state); //check
return state.cleanChoice(state, choice);
}),
// field('Caretaker_trained_in_muac__c', dataValue('properties.mother_trained_muac')),
field('Caretaker_trained_in_muac__c', state => {
var choice = dataValue('properties.mother_trained_muac')(state); //check
return state.cleanChoice(state, choice);
}),
field(
'of_Caretaker_MUAC_screenings__c',
dataValue('properties.mother_nb_screening')
),
field('Current_Weight__c', dataValue('properties.Current_Weight')), //Only on task update
field('Current_Height__c', dataValue('properties.current_height')),
field('Current_MUAC__c', dataValue('properties.MUAC')),
field('Current_Nutrition_Status__c', state => {
var status = dataValue('properties.Nutrition_Status')(state);
return status ? state.nutritionMap[status] : undefined;
}),
//TT5 & HAWI
field('TT5_Mother_Registrant__c', state => {
var preg = dataValue('properties.Pregnant')(state);
return preg == 'Yes' ? 'Yes' : null;
}),
field('Enrollment_Date__c', state => {
var age = dataValue('properties.age')(state);
var date = dataValue('server_date_modified')(state);
var preg = dataValue('properties.Pregnant')(state);
return age < 5 || preg == 'Yes' ? date : null;
}),
field('HAWI_Enrollment_Date__c', state => {
var date = dataValue('server_date_modified')(state);
var status = dataValue('properties.hiv_status')(state);
return status == 'positive' ? date : null;
}),
field('Thrive_Thru_5_Registrant__c', state => {
var age = dataValue('properties.age')(state);
var preg = dataValue('properties.Pregnant')(state);
return age < 5 || preg == 'Yes' ? 'Yes' : 'No';
}), //check mapping
field('HAWI_Registrant__c', state => {
var status = dataValue('properties.hiv_status')(state);
return status == 'positive' ? 'Yes' : 'No';
}),
//ANC
field('ANC_1__c', state => {
var date = dataValue('properties.ANC_1')(state);
return date && date !== '' ? date : undefined;
}),
field('ANC_2__c', state => {
var date = dataValue('properties.ANC_2')(state);
return date && date !== '' ? date : undefined;
}),
field('ANC_3__c', state => {
var date = dataValue('properties.ANC_3')(state);
return date && date !== '' ? date : undefined;
}),
field('ANC_4__c', state => {
var date = dataValue('properties.ANC_4')(state);
return date && date !== '' ? date : undefined;
}),
field('ANC_5__c', state => {
var date = dataValue('properties.ANC_5')(state);
return date && date !== '' ? date : undefined;
}),
field('Date_of_Birth__c', state => {
var date = dataValue('properties.DOB')(state);
return date && date !== '' ? date : undefined;
}),
//Immunization
field(
'Child_missed_immunization_type__c',
dataValue('form.TT5.Child_Information.Immunizations.immunization_type')
), //check
field('BCG__c', dataValue('properties.BCG')),
field('OPV_0__c', dataValue('properties.OPV_0')),
field('Measles_6__c', dataValue('properties.Measles_6')),
field('Measles_9__c', dataValue('properties.Measles_9')),
field('Measles_18__c', dataValue('properties.Measles_18')),
field('OPV_1__c', dataValue('properties.OPV_PCV_Penta_1')),
field('OPV_2__c', dataValue('properties.OPV_PCV_Penta_2')),
field('OPV_3__c', dataValue('properties.OPV_PCV_Penta_3')),
field('Rotavirus_1__c', dataValue('properties.rotavirus_1')),
field('Rotavirus_2__c', dataValue('properties.rotavirus_2')),
field('IPV__c', dataValue('properties.IPV')),
field('Vitamin_A_12__c', dataValue('properties.Vitamine_A')),
field('Vitamin_A_18__c', dataValue('properties.Vitamine_A_2')),
field('Vitamin_A_24__c', dataValue('properties.Vitamine_A_3')),
field('Deworming_12__c', dataValue('properties.Deworming_1')),
field('Deworming_18__c', dataValue('properties.Deworming_2')),
field('Deworming_24__c', dataValue('properties.Deworming_3')),
//ECD
// field('Did_you_counsel_caregiver_on__c',dataValue('properties.did_you_counsel_the_caregiver_on_delayed_milestones')),
field('Did_you_counsel_caregiver_on__c', state => {
var choice = dataValue(
'properties.did_you_counsel_the_caregiver_on_delayed_milestones'
)(state);
return state.cleanChoice(state, choice);
}),
// field('Delayed_Milestone__c',dataValue('properties.does_the_child_has_a_delayed_milestone')),
field('Delayed_Milestone__c', state => {
var choice = dataValue(
'properties.does_the_child_has_a_delayed_milestone'
)(state);
return state.cleanChoice(state, choice);
}),
// field('Child_has_2_or_more_play_items__c',dataValue('properties.does_the_child_has_2_or_more_play_items_at_home')),
field('Child_has_2_or_more_play_items__c', state => {
var choice = dataValue(
'properties.does_the_child_has_2_or_more_play_items_at_home'
)(state);
return state.cleanChoice(state, choice);
}),
// field('Child_has_3_or_more_picture_books__c',dataValue('properties.does_the_child_has_3_or_more_picture_books')),
field('Child_has_3_or_more_picture_books__c', state => {
var choice = dataValue(
'properties.does_the_child_has_3_or_more_picture_books'
)(state);
return state.cleanChoice(state, choice);
}),
field('Delayed_Milestones_Counselled_On__c', state => {
var ms = dataValue(
'properties.which_delayed_milestone_area_did_you_counsel_the_caregiver_on'
)(state);
return ms ? state.milestoneMap[ms] : undefined;
}),
field('Delayed_Milestone_Type__c', state => {
var ms = dataValue('properties.which_delayed_milestone')(state);
return ms ? state.milestoneTypeMap[ms] : undefined;
}),
//Death
field('Date_of_Death__c', dataValue('properties.Date_of_Death')),
field('Cause_of_Death__c', state => {
var death = dataValue('properties.cause_of_death_dead')(state);
return death ? death.toString().replace(/_/g, ' ') : death;
}), //check which case property to use - there are 2
field('Verbal_autopsy__c', dataValue('properties.verbal_autopsy')),
//Closing
field('Last_Modified_Date_CommCare__c', dataValue('date_modified')),
field('Case_Closed_Date__c',dataValue('date_closed')),
//field('Case_Closed_Date__c', state => {
// var closed = dataValue('date_closed')(state);
// var date = dataValue('date_modified')(state);
// return closed && closed == true ? date : undefined;
//}) //need case property
);
let sfRecord = expandReferences(originalMapping)(state);
Object.entries(sfRecord).forEach(([key, value]) => {
if (value === '') sfRecord[key] = undefined;
});
return { ...state, sfRecord };
});
// upsert data to SF
upsertIf(
state.data.properties.commcare_username !== 'test.2021' &&
state.data.properties.test_user !== 'Yes' ,
'Person__c', 'CommCare_ID__c', state => state.sfRecord);
upsertIf(
state.data.properties.commcare_username !== 'test.2021' &&
state.data.properties.test_user !== 'Yes' &&
state.data.properties.caretaker_case_id !== undefined && state.data.properties.caretaker_case_id !== '',
'Person__c', 'CommCare_ID__c',
fields(
relationship('Primary_Caregiver_Lookup__r', 'CommCare_ID__c', state => {
return caregiver = dataValue('properties.caretaker_case_id')(state);
}),
field('CommCare_ID__c', dataValue('case_id')),
)
);
upsertIf(
state.data.properties.commcare_username !== 'test.2021' &&
state.data.properties.test_user !== 'Yes' &&
state.data.properties.mother_case_id !== undefined && state.data.properties.mother_case_id !== '',
'Person__c', 'CommCare_ID__c',
fields(
relationship('Mother__r', 'CommCare_ID__c', state => {
return caregiver = dataValue('properties.mother_case_id')(state);
}),
field('CommCare_ID__c', dataValue('case_id')),
)
);
upsertIf(
state.data.properties.commcare_username !== 'test.2021' &&
state.data.properties.test_user !== 'Yes' &&
state.data.properties.head_of_household_case_id !== undefined && state.data.properties.head_of_household_case_id !== '',
'Household__c', 'CommCare_Code__c',
fields(
field('CommCare_Code__c', state => {
return (
dataValue('indices.parent.case_id')(state) ||
dataValue('properties.parent_id')(state)
)
}),
relationship('Head_of_Household__r', 'CommCare_ID__c', dataValue('properties.head_of_household_case_id')),
)
)