forked from AMPATH/etl-rest-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eid-rest-formatter.js
489 lines (486 loc) · 17.4 KB
/
eid-rest-formatter.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
'use strict';
var moment=require('moment');
var config = require('./conf/config');
var rp = require('./request-config');
var _ = require('underscore');
module.exports=function(){
function getRestResource(path){
var link=config.rest.uri+config.rest.port+path;
return link;
}
function removeWhiteSpace(string){
return string.replace(/\s+/g, '');
}
function checkStatusOfViralLoad(viralLoadPayload){
var status=0;
var hasNumbersOnly = /^[0-9]*(?:\.\d{1,2})?$/;
var hasLessThanSymbol=/</g;
if(_.isEmpty(viralLoadPayload)) return -1;
var viralLoadResult = removeWhiteSpace(viralLoadPayload.FinalResult);
if(_.isEmpty(viralLoadResult)) {
return -1;
}
if(hasNumbersOnly.test(viralLoadResult)){
status=1;
}
else if(hasLessThanSymbol.test(viralLoadResult)){
status=0;
}
else{
status=2;
}
return status;
}
function isViralLoadError(viralLoadPayload){
var isError=false;
var hasNumbersOnly = /^[0-9]*(?:\.\d{1,2})?$/;
var hasLessThanSymbol=/</g;
var viralLoadResult = removeWhiteSpace(viralLoadPayload.FinalResult);
if(!hasNumbersOnly.test(viralLoadResult) && !hasLessThanSymbol.test(viralLoadResult)){
isError=true;
}
return isError;
}
function cd4PanelHasErrors(cd4Payload){
var hasErrors=false;
var exceptions=[];
var hasNumbersOnly = /^[0-9]*(?:\.\d{1,2})?$/;
var hasLessThanSymbol=/</g;
if(_.isEmpty(cd4Payload)) return false;
var AVGCD3percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3percentLymph);
var AVGCD3AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3AbsCnt);
var AVGCD3CD4percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3CD4percentLymph);
var AVGCD3CD4AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3CD4AbsCnt);
var CD45AbsCntResult=removeWhiteSpace(cd4Payload.CD45AbsCnt);
if(!hasNumbersOnly.test(AVGCD3percentLymphResult)){
exceptions.push(AVGCD3percentLymphResult);
}
if(!hasNumbersOnly.test(AVGCD3AbsCntResult)){
exceptions.push(AVGCD3AbsCntResult);
}
if(!hasNumbersOnly.test(AVGCD3CD4percentLymphResult)){
exceptions.push(AVGCD3CD4percentLymphResult);
}
if(!hasNumbersOnly.test(AVGCD3CD4AbsCntResult)){
exceptions.push(AVGCD3CD4AbsCntResult);
}
if(!hasNumbersOnly.test(CD45AbsCntResult)){
exceptions.push(CD45AbsCntResult);
}
if(exceptions.length>0){
hasErrors=true;
}
return hasErrors;
}
function cd4PanelHasValidData(cd4Payload){
var hasValidData=false;
var validData=[];
var hasNumbersOnly = /^[0-9]*(?:\.\d{1,2})?$/;
var hasLessThanSymbol=/</g;
if(_.isEmpty(cd4Payload)) return false;
var AVGCD3percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3percentLymph);
var AVGCD3AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3AbsCnt);
var AVGCD3CD4percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3CD4percentLymph);
var AVGCD3CD4AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3CD4AbsCnt);
var CD45AbsCntResult=removeWhiteSpace(cd4Payload.CD45AbsCnt);
if(hasNumbersOnly.test(AVGCD3percentLymphResult)){
validData.push(AVGCD3percentLymphResult);
}
if(hasNumbersOnly.test(AVGCD3AbsCntResult)){
validData.push(AVGCD3AbsCntResult);
}
if(hasNumbersOnly.test(AVGCD3CD4percentLymphResult)){
validData.push(AVGCD3CD4percentLymphResult);
}
if(hasNumbersOnly.test(AVGCD3CD4AbsCntResult)){
validData.push(AVGCD3CD4AbsCntResult);
}
if(hasNumbersOnly.test(CD45AbsCntResult)){
validData.push(CD45AbsCntResult);
}
if(validData.length>0){
hasValidData=true;
}
return hasValidData;
}
function generateCd4ValidData(cd4Payload){
var cd4Data={};
var hasNumbersOnly = /^[0-9]*(?:\.\d{1,2})?$/;
var hasLessThanSymbol=/</g;
var AVGCD3percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3percentLymph);
var AVGCD3AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3AbsCnt);
var AVGCD3CD4percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3CD4percentLymph);
var AVGCD3CD4AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3CD4AbsCnt);
var CD45AbsCntResult=removeWhiteSpace(cd4Payload.CD45AbsCnt);
if(hasNumbersOnly.test(AVGCD3percentLymphResult)){
cd4Data.AVGCD3percentLymph=cd4Payload.AVGCD3percentLymph;
}
if(hasNumbersOnly.test(AVGCD3AbsCntResult)){
cd4Data.AVGCD3AbsCnt=cd4Payload.AVGCD3AbsCnt;
}
if(hasNumbersOnly.test(AVGCD3CD4percentLymphResult)){
cd4Data.AVGCD3CD4percentLymph=cd4Payload.AVGCD3CD4percentLymph;
}
if(hasNumbersOnly.test(AVGCD3CD4AbsCntResult)){
cd4Data.AVGCD3CD4AbsCnt=cd4Payload.AVGCD3CD4AbsCnt;
}
if(hasNumbersOnly.test(CD45AbsCntResult)){
cd4Data.CD45AbsCnt=cd4Payload.CD45AbsCnt;
}
return cd4Data;
}
function generateCd4Exceptions(cd4Payload){
var cd4Exceptions={};
var hasNumbersOnly = /^[0-9]*(?:\.\d{1,2})?$/;
var hasLessThanSymbol=/</g;
var AVGCD3percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3percentLymph);
var AVGCD3AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3AbsCnt);
var AVGCD3CD4percentLymphResult=removeWhiteSpace(cd4Payload.AVGCD3CD4percentLymph);
var AVGCD3CD4AbsCntResult=removeWhiteSpace(cd4Payload.AVGCD3CD4AbsCnt);
var CD45AbsCntResult=removeWhiteSpace(cd4Payload.CD45AbsCnt);
if(!hasNumbersOnly.test(AVGCD3percentLymphResult)){
cd4Exceptions.AVGCD3percentLymph=cd4Payload.AVGCD3percentLymph;
}
if(!hasNumbersOnly.test(AVGCD3AbsCntResult)){
cd4Exceptions.AVGCD3AbsCnt=cd4Payload.AVGCD3AbsCnt;
}
if(!hasNumbersOnly.test(AVGCD3CD4percentLymphResult)){
cd4Exceptions.AVGCD3CD4percentLymph=cd4Payload.AVGCD3CD4percentLymph;
}
if(!hasNumbersOnly.test(AVGCD3CD4AbsCntResult)){
cd4Exceptions.AVGCD3CD4AbsCnt=cd4Payload.AVGCD3CD4AbsCnt;
}
if(!hasNumbersOnly.test(CD45AbsCntResult)){
cd4Exceptions.CD45AbsCnt=cd4Payload.CD45AbsCnt;
}
return cd4Exceptions;
}
function convertViralLoadPayloadToRestConsumableObs(viralLoad,patientUuId){
var date=moment(viralLoad.DateCollected).format();
var body={
person:patientUuId,
obsDatetime:date,
concept:"a8982474-1350-11df-a1f1-0026b9348838",
value:viralLoad.FinalResult
};
if(viralLoad['orderNumber'] in viralLoad){
getOrderByOrderNumber(viralLoad['orderNumber'])
.then(function(response){
body.order=response.uuid;
resolve(body);
})
.catch(function(error){
reject(error);
})
}
else{
return body;
}
}
function convertViralLoadWithLessThanToRestConsumableObs(viralLoad,patientUuId){
var date=moment(viralLoad.DateCollected).format();
var body={
person:patientUuId,
obsDatetime:date,
concept:"a8982474-1350-11df-a1f1-0026b9348838",
value:0
};
if(viralLoad['orderNumber'] in viralLoad){
getOrderByOrderNumber(viralLoad['orderNumber'])
.then(function(response){
body.order=response.uuid;
resolve(body)
})
.catch(function(error){
reject(error);
})
}
else{
return body;
}
}
function convertViralLoadExceptionToRestConsumableObs(viralLoad,patientUuId){
var date=moment(viralLoad.DateCollected).format();
var body={
person:patientUuId,
obsDatetime:date,
concept:"457c741d-8f71-4829-b59d-594e0a618892"
};
var labExceptions=getLabExceptions();
if(viralLoad.FinalResult.toUpperCase() in labExceptions){
var labTestConcept="a8982474-1350-11df-a1f1-0026b9348838";
var codedConceptValue=labExceptions[viralLoad.FinalResult.toUpperCase()];
var codedPayload=generateCodedPayload(patientUuId,labTestConcept,codedConceptValue,date);
body.concept=codedPayload.concept;
body.groupMembers=codedPayload.groupMembers;
}
else{
var value=viralLoad.FinalResult;
var labTestConcept="a8982474-1350-11df-a1f1-0026b9348838";
var nonCodedPayload=generateNonCodedPayload(patientUuId,labTestConcept,value,date);
body.concept=nonCodedPayload.concept;
body.groupMembers=nonCodedPayload.groupMembers
}
if(viralLoad['orderNumber'] in viralLoad){
getOrderByOrderNumber(viralLoad['orderNumber'])
.then(function(response){
body.order=response.uuid;
resolve(body)
})
.catch(function(error){
reject(error);
})
}
else{
return body;
}
}
function convertCD4PayloadTORestConsumableObs(CD4payload,patientUuId){
var date=moment(CD4payload.DateCollected).format();
var body={
concept:"a896cce6-1350-11df-a1f1-0026b9348838",
person:patientUuId,
obsDatetime:date,
groupMembers:[]
};
if("AVGCD3percentLymph" in CD4payload){
var conceptUuId="a89c4220-1350-11df-a1f1-0026b9348838";
var value=CD4payload.AVGCD3percentLymph;
var AVGCD3percentLymph=generateCD4PanelSingleObject(patientUuId,conceptUuId,value,date);
body.groupMembers.push(AVGCD3percentLymph);
}
if("AVGCD3AbsCnt" in CD4payload){
var conceptUuId="a898fcd2-1350-11df-a1f1-0026b9348838";
var value=CD4payload.AVGCD3AbsCnt;
var AVGCD3AbsCnt=generateCD4PanelSingleObject(patientUuId,conceptUuId,value,date);
body.groupMembers.push(AVGCD3AbsCnt);
}
if("AVGCD3CD4percentLymph" in CD4payload){
var conceptUuId="a8970a26-1350-11df-a1f1-0026b9348838";
var value=CD4payload.AVGCD3CD4percentLymph;
var AVGCD3CD4percentLymph=generateCD4PanelSingleObject(patientUuId,conceptUuId,value,date);
body.groupMembers.push(AVGCD3CD4percentLymph);
}
if("AVGCD3CD4AbsCnt" in CD4payload){
var conceptUuId="a8a8bb18-1350-11df-a1f1-0026b9348838";
var value=CD4payload.AVGCD3CD4AbsCnt;
var AVGCD3CD4AbsCnt=generateCD4PanelSingleObject(patientUuId,conceptUuId,value,date);
body.groupMembers.push(AVGCD3CD4AbsCnt);
}
if("CD45AbsCnt" in CD4payload){
var conceptUuId="a89c4914-1350-11df-a1f1-0026b9348838";
var value=CD4payload.CD45AbsCnt;
var CD45AbsCnt=generateCD4PanelSingleObject(patientUuId,conceptUuId,value,date);
body.groupMembers.push(CD45AbsCnt);
}
if(CD4payload['orderNumber'] in CD4payload){
getOrderByOrderNumber(CD4payload['orderNumber'])
.then(function(response){
body.order=response.uuid;
resolve(body)
})
.catch(function(error){
reject(error);
})
}
else{
return body;
}
}
function convertCD4ExceptionTORestConsumableObs(CD4payload,patientUuId){
var date=moment(CD4payload.DateCollected).format();
var body={
concept:"457c741d-8f71-4829-b59d-594e0a618892",
person:patientUuId,
obsDatetime:date,
groupMembers:[]
};
var AVGCD3percentLymph=cd4ExceptionGroupMemberGenerator(patientUuId,CD4payload,"AVGCD3percentLymph","a89c4220-1350-11df-a1f1-0026b9348838",date);
if(AVGCD3percentLymph.length>0){
_.each(AVGCD3percentLymph,function(groupMember){
body.groupMembers.push(groupMember);
});
}
var AVGCD3AbsCnt=cd4ExceptionGroupMemberGenerator(patientUuId,CD4payload,"AVGCD3AbsCnt","a898fcd2-1350-11df-a1f1-0026b9348838",date);
if(AVGCD3AbsCnt.length>0){
_.each(AVGCD3AbsCnt,function(groupMember){
body.groupMembers.push(groupMember);
});
}
var AVGCD3CD4percentLymph=cd4ExceptionGroupMemberGenerator(patientUuId,CD4payload,"AVGCD3CD4percentLymph","a8970a26-1350-11df-a1f1-0026b9348838",date);
if(AVGCD3CD4percentLymph.length>0){
_.each(AVGCD3CD4percentLymph,function(groupMember){
body.groupMembers.push(groupMember);
});
}
var AVGCD3CD4AbsCnt=cd4ExceptionGroupMemberGenerator(patientUuId,CD4payload,"AVGCD3CD4AbsCnt","a8a8bb18-1350-11df-a1f1-0026b9348838",date);
if(AVGCD3CD4AbsCnt.length>0){
_.each(AVGCD3CD4AbsCnt,function(groupMember){
body.groupMembers.push(groupMember);
});
}
var CD45AbsCnt=cd4ExceptionGroupMemberGenerator(patientUuId,CD4payload,"CD45AbsCnt","a89c4914-1350-11df-a1f1-0026b9348838",date);
if(CD45AbsCnt.length>0){
_.each(CD45AbsCnt,function(groupMember){
body.groupMembers.push(groupMember);
});
}
if(CD4payload['orderNumber'] in CD4payload){
getOrderByOrderNumber(CD4payload['orderNumber'])
.then(function(response){
body.order=response.uuid;
resolve(body)
})
.catch(function(error){
reject(error);
})
}
else{
return body;
}
}
function cd4ExceptionGroupMemberGenerator(patientUuId,CD4payload,typeOfTest,labTestConcept,date){
var groupMembers=[];
var labExceptions=getLabExceptions();
if(typeOfTest in CD4payload){
if(CD4payload[typeOfTest].toUpperCase() in labExceptions){
var codedConceptValue=labExceptions[CD4payload[typeOfTest].toUpperCase()];
var codedPayload=generateCodedPayload(patientUuId,labTestConcept,codedConceptValue,date);
_.each(codedPayload.groupMembers,function(groupMember){
groupMembers.push(groupMember);
});
}
else{
var value=CD4payload[typeOfTest];
var nonCodedPayload=generateNonCodedPayload(patientUuId,labTestConcept,value,date);
_.each(nonCodedPayload.groupMembers,function(groupMember){
groupMembers.push(groupMember);
});
}
}
return groupMembers;
}
function convertDNAPCRPayloadTORestConsumableObs(DNAPCRPayload,patientUuId){
var body={
concept:"a898fe80-1350-11df-a1f1-0026b9348838",
person:patientUuId
};
var date=moment(DNAPCRPayload.DateCollected).format();
body.obsDatetime=date;
if(DNAPCRPayload.FinalResult.toUpperCase()=="NEGATIVE"){
body.value="a896d2cc-1350-11df-a1f1-0026b9348838";
}
else if(DNAPCRPayload.FinalResult.toUpperCase()=="POSITIVE"){
body.value="a896f3a6-1350-11df-a1f1-0026b9348838";
}
if(DNAPCRPayload['orderNumber'] in DNAPCRPayload){
getOrderByOrderNumber(DNAPCRPayload['orderNumber'])
.then(function(response){
body.order=response.uuid;
resolve(body)
})
.catch(function(error){
reject(error);
})
}
else{
return body;
}
}
function getLabExceptions(){
return {
"POOR SAMPLE QUALITY":"a89c3f1e-1350-11df-a1f1-0026b9348838",
"NOT DONE":"a899ea48-1350-11df-a1f1-0026b9348838",
"INDETERMINATE":"a89a7ae4-1350-11df-a1f1-0026b9348838",
"BELOW DETECTABLE LIMIT":"a89c3f1e-1350-11df-a1f1-0026b9348838",
"UNABLE TO COLLECT SAMPLE":"a8afcec6-1350-11df-a1f1-0026b9348838",
"SPECIMEN NOT RECEIVED":"0271c15e-4f7f-4a18-9b45-2d7e5b6f7057",
"ORDERED FOR WRONG PATIENT":"3366412a-e279-4428-a671-37221804c6e6"
}
}
function generateCodedPayload(patientUuId,labTestConcept,codedConceptValue,date){
var payload={
concept:"457c741d-8f71-4829-b59d-594e0a618892",
groupMembers:[
{
concept:"f67ff075-f91e-4b71-897a-9ded87b34984",
person:patientUuId,
value:labTestConcept,
obsDatetime:date
},
{
concept:"5026a3ee-0612-48bf-b9a3-a2944ddc3e04",
person:patientUuId,
value:codedConceptValue,
obsDatetime:date
}
]
}
return payload;
}
function generateNonCodedPayload(patientUuId,labTestConcept,value,date){
var payload={
concept:"457c741d-8f71-4829-b59d-594e0a618892",
groupMembers:[
{
concept:"f67ff075-f91e-4b71-897a-9ded87b34984",
person:patientUuId,
value:labTestConcept,
obsDatetime:date
},
{
concept:"a8a06fc6-1350-11df-a1f1-0026b9348838",
person:patientUuId,
value:value,
obsDatetime:date
}
]
}
return payload;
}
function generateCD4PanelSingleObject(patientUuId,conceptUuId,value,date){
var payload={
concept:conceptUuId,
person:patientUuId,
value:value,
obsDatetime:date
}
return payload;
}
function getOrderByOrderNumber(orderNo){
var uri=getRestResource('/amrs/ws/rest/v1/order/'+orderNo);
var queryString={
v:'full'
}
return new Promise(function(resolve,reject){
rp.getRequestPromise(queryString,uri)
.then(function(response){
resolve(response);
})
.catch(function(error){
reject(error);
})
});
}
return{
convertViralLoadPayloadToRestConsumableObs:convertViralLoadPayloadToRestConsumableObs,
convertCD4PayloadTORestConsumableObs:convertCD4PayloadTORestConsumableObs,
convertDNAPCRPayloadTORestConsumableObs:convertDNAPCRPayloadTORestConsumableObs,
convertViralLoadExceptionToRestConsumableObs:convertViralLoadExceptionToRestConsumableObs,
convertCD4ExceptionTORestConsumableObs:convertCD4ExceptionTORestConsumableObs,
generateCD4PanelSingleObject:generateCD4PanelSingleObject,
getLabExceptions:getLabExceptions,
cd4ExceptionGroupMemberGenerator:cd4ExceptionGroupMemberGenerator,
checkStatusOfViralLoad:checkStatusOfViralLoad,
generateNonCodedPayload:generateNonCodedPayload,
generateCodedPayload:generateCodedPayload,
removeWhiteSpace:removeWhiteSpace,
cd4PanelHasValidData:cd4PanelHasValidData,
getOrderByOrderNumber:getOrderByOrderNumber,
getRestResource:getRestResource,
convertViralLoadWithLessThanToRestConsumableObs:convertViralLoadWithLessThanToRestConsumableObs,
cd4PanelHasErrors:cd4PanelHasErrors,
generateCd4Exceptions: generateCd4Exceptions,
generateCd4ValidData: generateCd4ValidData
}
}();