-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupsert_brace_inventory.js
230 lines (221 loc) · 6.77 KB
/
upsert_brace_inventory.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
fn(state => {
//NOTE: Here we add functions for converting/transformating data
const braceMap = {
dobbs_or_mitchell: 'dobbs_or_mitchell',
iowa: 'Iowa',
miraclefeet: 'MiracleFeet',
steenbeek: 'Steenbeek',
other: 'Other',
ankle_foot_orthosis_afo: 'ankle_foot_orthosis_afo',
};
//Added By Beth
const discardedClinics = [
'FRA01', // 'hospital_escuela',
'togo_test_clinic',
'test_bangladesh',
'pakistan_test',
'bol_test',
'brazil_test',
'cam_test',
'bong_test',
'brazzaville_test',
'ecu_test',
'Facorc_test',
'gambia_test',
'gambia_test_clinic',
'guat_test',
'guinea_test_clinic',
'hon_test',
'uptest',
'indonesia_test',
'test',
'majunga_test_clinic',
'madagascar_test',
'morocco_test',
'morocco_test_clinic',
'mya_test',
'nepal_test',
'nica_prueba',
'nigeria_test',
'nigeria_test_clinic',
'par_test',
'philippines_test',
'sl_test',
'test_som',
'ss_test',
'sri_lanka_test',
'test_tanzania_clinic',
'test_uganda_clinic',
'test_clinic1',
'TestMAJ01',
'senegal_test_clinic',
'uganda_test_clinic',
'mali_test',
'bangladesh_test_clinic',
'bolivia_test_clinic',
'brazil_test_clinic',
'cambodia_test_clinic',
'congo_test_clinic',
'ecuador_test_clinic',
'gautemala_test_clinic',
'guinea_test_clinic',
'honduras_test_clinic',
'india_test_clinic',
'indonesia_test_clinic',
'liberia_test_clinic',
'madagascar_test_clinic',
'mali_test_clinic',
'morocco_test_clinic',
'myanmar_test_clinic',
'nepal_test_clinic',
'nicaraugua_test_clinic',
'nigeria_test_clinic',
'test_nigeria',
'paraguary_test_clinic',
'phillipines_test_clinic',
'senegal_test_clinic',
'sierra_leone_test_clinic',
'somalia_test_clinic',
'south_sudan_test_clinic',
'sri_lanka_test_clinic',
'tanzania_test_clinic',
'uganda_test_clinic',
'test_clinic1',
'test_clinic2',
'test_clinic3',
'test_clinic4',
'haiti_test_clinic',
'sierra_leone_test_clinic',
'peru_test_clinic',
'mali_test_clinic',
'bafata_test_clinic',
];
const discardedCountries = [
'Honduras',
'Dominican Republic'];
return { ...state, braceMap, discardedClinics, discardedCountries };
});
fn(state => {
const { clinic_code } = state.data.form.calcs.case_properties;
const { test_clinic } = state.data.form.calcs.case_properties;
const { test_user } = state.data.form.calcs.case_properties;
if (
state.discardedClinics.includes(clinic_code) ||
state.discardedCountries.includes(dataValue('state.data.form.case.update.patient_country')(state)) ||
test_clinic==='Yes' || test_user==='Yes'
){
console.log('Test clinic or test user detected; no data uploaded to Salesforce.');
return state;
}
return execute(
//Get Partner clinic id from Patient's clinic
query(
`SELECT Account.ParentId FROM Contact
WHERE CommCare_Case_ID__c = '${dataValue('form.case.@case_id')(
state
)}'`
),
fn(state => ({
...state,
data: {
...state.data,
parentClinicId: state.references[0].records[0].Account.ParentId,
},
})),
//Then get related Partner Brace Inventory
query(
state => `SELECT Id FROM Partner_Brace_Inventory__c
WHERE Partner__c = '${state.data.parentClinicId}'
AND Active__c = TRUE
LIMIT 1` //Returns active PBI record
),
fn(state => ({
...state,
data: {
...state.data,
// inventoryId: state.references[0].records[0].Id,
inventoryId: state.references[0].records && state.references[0].records.length !== 0
? state.references[0].records[0].Id
: undefined,
//save id of Partner_Brace_Inventory__c to map later
},
})),
//NOTE: Here we upsert our target object in Salesforce & define mappings
upsertIf(
state.data.form.subcase_0.case.update.brace_type &&
state.data.form.subcase_0.case.update.brace_type !== '', //only upsert if brace_type is defined
'Partner_Brace_Distribution__c',
'CommCare_Case_ID__c',
fields(
field('CommCare_Case_ID__c', dataValue('id')), //make the form Id the uid for this object
// Old uid for Visit upserts
// field('New_Visit_UID__c', state => {
// var icrId = state.data.form.subcase_0.case.update.visit_original_id;
// var caseId = state.data.form.subcase_0.case['@case_id'];
// return icrId && icrId !== '' ? icrId : caseId;
// }),
relationship(
'Patient__r',
'CommCare_Case_ID__c',
dataValue('form.case.@case_id')
),
field('Brace_Type__c', state => {
const ref = state.data.form.subcase_0.case.update.brace_type;
return !ref
? state.data.form.brace.brace_type_india
: ref
? state.braceMap[ref]
: 'Not Defined';
}),
field('Partner_Brace_Inventory__c', dataValue('inventoryId')),
field(
'Brace_Given__c',
dataValue('form.subcase_0.case.update.miraclefeet_brace_given')
),
field(
'Bar_Condition__c',
dataValue('form.subcase_0.case.update.miraclefeet_bar_condition')
),
field(
'Shoe_Condition__c',
dataValue('form.subcase_0.case.update.miraclefeet_shoes_condition')
),
field(
'Bar_Size__c',
dataValue('form.subcase_0.case.update.miraclefeet_bar_size')
),
field('Shoe_Size__c', state => {
const mf_shoe =
state.data.form.subcase_0.case.update.miraclefeet_shoe_size;
const mf_brace = state.data.form.brace.miraclefeet_brace;
var shoe = '';
if (
typeof mf_brace === 'undefined' &&
typeof mf_shoe === 'undefined'
) {
shoe = '';
} else if (
typeof mf_brace.miraclefeet_shoe_size_india === 'undefined' &&
typeof mf_shoe === 'undefined'
) {
shoe = '';
} else if (
typeof mf_brace.miraclefeet_shoe_size_india === 'undefined'
) {
shoe = mf_shoe;
//mf_shoe.charAt(0).toUpperCase() + mf_shoe.slice(1).replace('_', ' ');
} else {
shoe = mf_brace;
// mf_brace.miraclefeet_shoe_size_india.charAt(0).toUpperCase() +
// mf_brace.miraclefeet_shoe_size_india.slice(1).replace('_', ' ');
}
return shoe;
}),
field('visit_date__c', state => {
var date = dataValue('form.case.update.visit_date')(state);
return date && date !== '' ? new Date(date).toISOString() : date;
})
)
)
)(state);
});