-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFhirUtilities.js
632 lines (582 loc) · 18.6 KB
/
FhirUtilities.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
import _ from 'lodash';
let get = _.get;
let set = _.set;
let has = _.has;
let uniq = _.uniq;
export const FhirUtilities = {
addPatientFilterToQuery(patientId, currentQuery, practitionerId){
let returnQuery = {};
if(typeof currentQuery === "object"){
Object.assign(returnQuery, currentQuery);
}
if(practitionerId){
returnQuery = {};
} else {
if(patientId){
returnQuery = {$or: [
{"patient.reference": "Patient/" + patientId},
{"patient.reference": "urn:uuid:" + patientId},
{"subject.reference": "Patient/" + patientId},
{"subject.reference": "urn:uuid:" + patientId},
{"patient.reference": { $regex: ".*Patient/" + patientId}},
{"subject.reference": { $regex: ".*Patient/" + patientId}},
{"agent.who.reference": "Patient/" + patientId}
]}
} else {
returnQuery = {$or: [
{"patient.reference": "Patient/public"},
{"patient.reference": "urn:uuid:Patient/public"},
{"patient.reference": { $regex: ".*Patient/public"}},
{"subject.reference": { $regex: ".*Patient/public"}}
]}
}
}
return returnQuery
},
trimTrailingSlash(url){
let resultingUrl;
if(url[url.length - 1] === "/"){
resultingUrl = url.substring(0, url.length - 1);
} else {
resultingUrl = url;
}
return resultingUrl;
},
pluckReferenceId(reference){
let identifier = ""
let referenceParts = [];
if(reference){
if(reference.includes("urn:uuid:")){
// get the last part of the string
identifier = reference.substring(9, reference.length)
}
// guard against empty strings
if(reference.includes("/")){
// split the string apart according to slashes
referenceParts = reference.split("/");
// get the last part of the string
identifier = referenceParts[referenceParts.length - 1];
}
}
return identifier;
},
pluckReferenceBase(reference){
let identifier = ""
let referenceParts = [];
if(reference){
if(reference.includes("urn:uuid:")){
// get the last part of the string
identifier = reference.substring(9, reference.length)
}
// guard against empty strings
if(reference.includes("/")){
// split the string apart according to slashes
referenceParts = reference.split("/");
// get the first part of the string
identifier = referenceParts[0];
}
}
return identifier;
},
stringifyAddress(address){
// var assembledAddress = "3928 W. Cornelia Ave, Chicago, IL";
let assembledAddress = '';
if(get(address, 'line[0]')){
assembledAddress = get(address, 'line[0]');
}
if(get(address, 'city')){
// guard in case the other elements weren't set,
// in which case we don't want to lead with a comma
if(assembledAddress.length > 0){
assembledAddress = assembledAddress + ', ';
}
assembledAddress = assembledAddress + get(address, 'city');
}
if(get(address, 'state')){
if(assembledAddress.length > 0){
assembledAddress = assembledAddress + ', ';
}
assembledAddress = assembledAddress + get(address, 'state');
}
if(get(address, 'postalCode')){
if(assembledAddress.length > 0){
assembledAddress = assembledAddress + ', ';
}
assembledAddress = assembledAddress + get(address, 'postalCode');
}
if(get(address, 'country')){
if(assembledAddress.length > 0){
assembledAddress = assembledAddress + ', ';
}
assembledAddress = assembledAddress + get(address, 'country');
}
return assembledAddress;
},
assembleName(humanName, options){
// patient name has gone through a number of revisions, and we need to search a few different spots, and assemble as necessary
let resultingNameString = "";
let nameText = get(humanName, 'text', '');
if(nameText.length > 0){
// some systems will store the name as it is to be displayed in the text field
// if that's present, use it
resultingNameString = get(humanName, 'text', '');
} else {
// the majority of systems out there are SQL based and make a design choice to store as 'first' and 'last' name
// critiques of that approach can be saved for a later time
// but suffice it to say that we need to assemble the parts
if(!get(options, 'noPrefix')){
if(get(humanName, 'prefix[0]')){
resultingNameString = get(humanName, 'prefix[0]') + ' ';
}
}
if(get(humanName, 'given[0]')){
resultingNameString = resultingNameString + get(humanName, 'given[0]') + ' ';
}
if(get(humanName, 'family')){
// R4 - droped the array of family names; one authoritative family name per humanName
resultingNameString = resultingNameString + get(humanName, 'family') + ' ';
} else if (get(humanName, 'family[0]')){
// DSTU2 and STU3 - allows an array of family names
resultingNameString = resultingNameString + get(humanName, 'family[0]') + ' ';
}
if(get(humanName, 'suffix[0]')){
resultingNameString = resultingNameString + ' ' + get(humanName, 'suffix[0]');
}
}
// remove any whitespace from the name
return resultingNameString.trim();
},
pluckName(fhirPatientResource, options){
if(!options) {
options = {};
}
// we assume the first listed name by default (>95% of the use cases)
let selectedIndex = 0;
let resultingNameString = "";
// assuming the data isnt anonymized
if(Array.isArray(get(fhirPatientResource, 'name'))){
// we should parse through the available names
fhirPatientResource.name.forEach(function(name, index){
// to see if any of them are marked official
if(get(name, 'use') === "official"){
selectedIndex = index;
}
})
// assemble the name from the first listed name or whatever is marked official
if(has(fhirPatientResource, 'name[0].text')){
resultingNameString = get(fhirPatientResource, 'name[0].text');
} else {
resultingNameString = FhirUtilities.assembleName(fhirPatientResource.name[selectedIndex], options)
}
}
// remove any whitespace from the name
return resultingNameString.trim();
},
pluckPhone(telecomArray){
let result = "";
if(Array.isArray(telecomArray)){
telecomArray.forEach(function(telecomRecord){
if(get(telecomRecord, 'system') === 'phone'){
result = get(telecomRecord, 'value');
}
})
}
return result;
},
pluckEmail(telecomArray){
let result = "";
if(Array.isArray(telecomArray)){
telecomArray.forEach(function(telecomRecord){
if(get(telecomRecord, 'system') === 'email'){
result = get(telecomRecord, 'value');
}
})
}
return result;
},
pluckCodeableConcept(field){
let result = "";
if(get(field, 'text')){
result = get(field, 'text', '');
} else if(get(field, 'coding[0].display')) {
result = get(field, 'coding[0].display', '');
} else {
result = get(field, 'coding[0].code', '');
}
return result;
},
pluckReference(field){
let result = "";
if(get(field, 'display')){
result = get(field, 'display');
} else {
result = get(field, 'reference', '');
}
return result;
},
pluckFirstIdentifier(identifiers){
let result = "";
if(Array.isArray(identifiers)){
result = get(identifiers[0], 'value', '')
}
return result;
},
generatePatientReference(patient){
let patientReference = {
display: "",
reference: ""
};
if(has(patient, 'id')){
patientReference.reference = "Patient/" + get(patient, "id")
}
patientReference.display = FhirUtilities.pluckName(patient)
return patientReference;
},
generateDateQuery(chainPrefix, startDate, endDate ){
let dateQuery = '';
if(chainPrefix){
dateQuery = chainPrefix;
}
if(startDate){
dateQuery = dateQuery + 'date=gt' + startDate
}
if(startDate && endDate){
dateQuery = dateQuery + '&';
}
if(chainPrefix){
dateQuery = dateQuery + chainPrefix;
}
if(endDate){
dateQuery = dateQuery + 'date=lt' + endDate
}
if(startDate || endDate){
dateQuery = dateQuery;
}
return dateQuery;
},
pluralizeResourceName(resourceType){
let pluralized = '';
switch (resourceType) {
case 'Binary':
pluralized = 'Binaries';
break;
case 'Library':
pluralized = 'Libraries';
break;
case 'SupplyDelivery':
pluralized = 'SupplyDeliveries';
break;
case 'ImagingStudy':
pluralized = 'ImagingStudies';
break;
case 'FamilyMemberHistory':
pluralized = 'FamilyMemberHistories';
break;
case 'ResearchStudy':
pluralized = 'ResearchStudies';
break;
default:
pluralized = resourceType + 's';
break;
}
return pluralized;
},
singularizeResourceName(resourceTypeString){
var singularized = '';
switch (resourceTypeString) {
case 'Binaries':
singularized = 'Binary';
break;
case 'Libraries':
singularized = 'Library';
break;
case 'SupplyDeliveries':
singularized = 'SupplyDelivery';
break;
case 'ImagingStudies':
singularized = 'ImagingStudy';
break;
case 'FamilyMemberHistories':
singularized = 'FamilyMemberHistory';
break;
case 'ResearchStudies':
singularized = 'ResearchStudy';
break;
default:
singularized = resourceTypeString.substring(0, resourceTypeString.length - 1)
break;
}
return singularized;
},
parseCapabilityStatement(capabilityStatement, canSearch){
console.log('FhirUtilities.parseCapabilityStatement.', capabilityStatement)
if(!canSearch){
canSearch = {};
}
if(["CapabilityStatement", "Conformance"].includes(get(capabilityStatement, 'resourceType')) ){
console.log('Found CapabilityStatement');
if(get(capabilityStatement, 'rest[0].mode') === "server"){
console.log('CapabilityStatement claims it is a server.');
if(Array.isArray(get(capabilityStatement, 'rest[0].resource'))){
let resourceArray = get(capabilityStatement, 'rest[0].resource');
console.log('Loading resource array from CapabilityStatement.');
if(Array.isArray(resourceArray)){
resourceArray.forEach(function(resource){
let resourceType = get(resource, 'type');
canSearch[resourceType] = false;
let interactionArray = get(resource, 'interaction');
if(Array.isArray(interactionArray)){
interactionArray.forEach(function(interaction){
if(["read", "search-type"].includes(get(interaction, 'code'))){
canSearch[resourceType] = true;
}
})
}
})
}
}
}
}
console.log("Generated the following ehrServerCapabilities object:", canSearch);
return canSearch;
},
pluralizeResourceName(resourceType){
var pluralized = '';
switch (resourceType) {
case 'Binary':
pluralized = 'Binaries';
break;
case 'Library':
pluralized = 'Libraries';
break;
case 'SupplyDelivery':
pluralized = 'SupplyDeliveries';
break;
case 'ImagingStudy':
pluralized = 'ImagingStudies';
break;
case 'FamilyMemberHistory':
pluralized = 'FamilyMemberHistories';
break;
case 'ResearchStudy':
pluralized = 'ResearchStudies';
break;
default:
pluralized = resourceType + 's';
break;
}
return pluralized;
},
isFhirResource(text){
let result = false;
let resourceTypes = [
"Account",
"ActivityDefinition",
"AdverseEvent",
"AllergyIntolerance",
"Appointment",
"AppointmentResponse",
"AuditEvent",
"Basic",
"Binary",
"BiologicallyDerivedProduct",
"BodyStructure",
"Bundle",
"CapabilityStatement",
"CarePlan",
"CareTeam",
"CatelogEntry",
"ChargeItem",
"ChargeItemDefinition",
"Claim",
"ClaimResponse",
"ClinicalImpression",
"CodeSystem",
"Communication",
"CommunicationResponse",
"CompartmentDefinition",
"Composition",
"ConceptMap",
"Condition",
"Consent",
"Contract",
"Coverage",
"CoverageEligibilityRequest",
"CoverageEligibilityResponse",
"DetectedIssue",
"Device",
"DeviceDefinition",
"DeviceMetric",
"DeviceRequest",
"DeviceUseStatement",
"DiagnosticReport",
"DocumentManifest",
"DocumentReferences",
"EffectiveEvidenceSynthesis",
"Encounter",
"Endpoint",
"EnrollmentRequest",
"EnrollmentResponse",
"EpisodeOfCare",
"EventDefinition",
"Evidence",
"EvidenceVariable",
"EvidenceScenario",
"ExplanationOfBenefit",
"FamilyMemberHistory",
"Flag",
"Goal",
"GraphDefinition",
"Group",
"GuidanceResposne",
"HealthcareService",
"ImagingStudy",
"Immunization",
"ImmunizationEvaluation",
"ImmunizationRecommendation",
"ImplementationGuide",
"InsurancePlan",
"Invoice",
"Library",
"List",
"Location",
"Measure",
"MeasureReport",
"Media",
"Medication",
"MedicationAdministration",
"MedicationDispense",
"MedicationKnowledge",
"MedicationRequest",
"MedicationStatement",
"MedicationOrder",
"MedicinalProduct",
"MessageDefinition",
"MessageHeader",
"MolecularSequence",
"NamingSystem",
"NutritionOrder",
"Observation",
"OperationDefinition",
"OperationOutcome",
"Organization",
"Parameters",
"Patient",
"PaymentNotice",
"PaymentReconciliation",
"Person",
"PlanDefinition",
"Practitioner",
"PractitionerRole",
"Procedure",
"Provenance",
"ProcedureRequest",
"Questionnaire",
"QuestionnaireResponse",
"RelatedPerson",
"RequestGroup",
"ResearchStudy",
"ResearchSubject",
"RiskAssessment",
"Schedule",
"SearchParameter",
"ServiceRequest",
"Slot",
"Specimen",
"StructureDefinition",
"StructureMap",
"Subscription",
"Substance",
"SupplyDelivery",
"SupplyRequest",
"Task",
"TerminologyCapabilities",
"TestReport",
"TestScript",
"ValueSet",
"VisionPrescription"
]
if(resourceTypes.includes(text)){
result = true;
}
return result;
},
consentIntoAccessControl(consentRecord){
// https://www.hl7.org/fhir/valueset-consent-action.html
let result = {
id: '',
role: 'unknown',
resource: 'Basic',
action: 'access',
attributes: ['*'],
securityLevel: ''
};
// which resourceType does this Consent record refer to
if(get(consentRecord, 'id')){
result.id = get(consentRecord, 'id');
}
// which resourceType does this Consent record refer to
if(get(consentRecord, 'provision.provision[0].class[0].code')){
result.resource = get(consentRecord, 'provision.provision[0].class[0].code');
} else if(get(consentRecord, 'provision[0].provision[0].class[0].code')){
result.resource = get(consentRecord, 'provision[0].provision[0].class[0].code');
}
// // should we base the action on the category coding?
// if(get(consentRecord, 'category[0].coding[0].code') === "IDSCL"){
// result.action = "read";
// }
// should we base the action on the category coding?
if(get(consentRecord, 'provision.provision[0].action[0].coding')){
if(Array.isArray(get(consentRecord, 'provision.provision[0].action[0].coding'))){
let actionString = "";
consentRecord.provision.provision[0].action[0].coding.forEach(function(action, index){
if(index !== 0){
actionString = actionString + ", ";
}
actionString = actionString + get(action, 'code')
})
result.action = actionString;
}
} else if(get(consentRecord, 'provision[0].provision[0].action[0].coding')){
if(Array.isArray(get(consentRecord, 'provision[0].provision[0].action[0].coding'))){
let actionString = "";
consentRecord.provision[0].provision[0].action[0].coding.forEach(function(action, index){
if(index !== 0){
actionString = actionString + ", ";
}
actionString = actionString + get(action, 'code')
})
result.action = actionString;
}
}
// should we base the action on the category coding?
if(get(consentRecord, 'provision.provision[0].actor[0].role.coding[0].display')){
result.role = get(consentRecord, 'provision.provision[0].actor[0].role.coding[0].display');
} else if(get(consentRecord, 'provision[0].provision[0].actor[0].role.coding[0].display')){
result.role = get(consentRecord, 'provision[0].provision[0].actor[0].role.coding[0].display');
}
if(get(consentRecord, 'provision.provision[0].securityLevel[0].coding[0].display')){
result.qualifier = "EQUALS"
result.args = "securityLevel"
result.securityLevel = get(consentRecord, 'provision.provision[0].securityLevel[0].coding[0].display');
result.condition = {
Fn: "EQUALS",
args: {
securityLevel: get(consentRecord, 'provision.provision[0].securityLevel[0].coding[0].display')
}
}
} else if(get(consentRecord, 'provision[0].provision[0].securityLevel[0].coding[0].display')){
result.qualifier = "EQUALS"
result.args = "securityLevel"
result.securityLevel = get(consentRecord, 'provision[0].provision[0].securityLevel[0].coding[0].display');
result.condition = {
Fn: "EQUALS",
args: {
securityLevel: get(consentRecord, 'provision[0].provision[0].securityLevel[0].coding[0].display')
}
}
}
return result;
}
}
export default FhirUtilities;