forked from HL7-DaVinci/crd-request-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrefetchTemplate.js
More file actions
121 lines (106 loc) · 4.88 KB
/
PrefetchTemplate.js
File metadata and controls
121 lines (106 loc) · 4.88 KB
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
// Prefetch Template Source:
// https://build.fhir.org/ig/HL7/davinci-crd/hooks.html#prefetch
export class PrefetchTemplate {
static generatePrefetchMap() {
const prefetchMap = new Map();
const PRACTITIONER_PREFETCH = new PrefetchTemplate('{{context.userId}}');
const REQUEST_PREFETCH = new PrefetchTemplate(
'MedicationRequest/{{context.medications.MedicationRequest.id}}'
);
const PATIENT_PREFETCH = new PrefetchTemplate('{{context.patientId}}');
const PHARMACY_PREFETCH = new PrefetchTemplate('HealthcareService/{{context.pharmacyId}}');
const ALL_REQUESTS_PREFETCH = new PrefetchTemplate(
'MedicationRequest?subject={{context.patientId}}&_include=MedicationRequest:medication'
);
// prefetchMap.set("Coverage", COVERAGE_PREFETCH_QUERY);
prefetchMap.set('request', REQUEST_PREFETCH);
prefetchMap.set('practitioner', PRACTITIONER_PREFETCH);
prefetchMap.set('patient', PATIENT_PREFETCH);
prefetchMap.set('pharmacy', PHARMACY_PREFETCH);
prefetchMap.set('medicationRequests', ALL_REQUESTS_PREFETCH);
// prefetchMap.set("ServiceRequest", SERVICE_REQUEST_BUNDLE);
// prefetchMap.set("Encounter", ENCOUNTER_BUNDLE);
return prefetchMap;
}
static generateParamElementMap() {
const paramElementMap = new Map();
// TODO - this should just be inferred based on context. Or rather
// the instructions from the hook about what context to fill in
// Quite literally, the "context" here refers to the "context" of the
// cds-hook, which as of now is just hard-coded in buildRequest.js
// Rather than do this, which searches the request resource for information,
// the cds-hook should be constructed and then the context used to actually make
// the appropriate requests.
paramElementMap.set('context.userId', ['requester', 'reference']);
paramElementMap.set('context.draftOrders.DeviceRequest.id', ['id']);
paramElementMap.set('context.medications.MedicationRequest.id', ['id']);
paramElementMap.set('context.medications.MedicationDispense.id', ['id']);
paramElementMap.set('context.draftOrders.NutritionOrder.id', ['id']);
paramElementMap.set('context.draftOrders.ServiceRequest.id', ['id']);
paramElementMap.set('context.draftOrders.context.appointments.Appointment.id', ['id']);
paramElementMap.set('context.draftOrders.context.encounterId', ['id']);
paramElementMap.set('context.patientId', ['subject', 'reference']);
paramElementMap.set('context.pharmacyId', ['id']);
return paramElementMap;
}
static generateQueries(requestBundle, patientReference, userReference, pharmacyId, ...prefetchKeys) {
var resolvedQueries = new Map();
for (var i = 0; i < prefetchKeys.length; i++) {
var prefetchKey = prefetchKeys[i];
var query = prefetchMap.get(prefetchKey).getQuery();
// Regex source: https://regexland.com/all-between-specified-characters/
var parametersToFill = query.match(/(?<={{).*?(?=}})/gs);
var resolvedQuery = query.slice();
for (var j = 0; j < parametersToFill.length; j++) {
var unresolvedParameter = parametersToFill[j];
var resolvedParameter;
if (requestBundle) {
if (unresolvedParameter === 'context.pharmacyId') {
resolvedParameter = pharmacyId;
} else {
resolvedParameter = PrefetchTemplate.resolveParameter(unresolvedParameter, requestBundle);
}
} else {
if (unresolvedParameter === 'context.patientId') {
resolvedParameter = patientReference;
} else if (unresolvedParameter === 'context.userId') {
resolvedParameter = userReference;
} else if (unresolvedParameter === 'context.pharmacyId') {
resolvedParameter = pharmacyId;
}
}
resolvedQuery = resolvedQuery.replace('{{' + unresolvedParameter + '}}', resolvedParameter);
}
resolvedQueries.set(prefetchKey, resolvedQuery);
}
return resolvedQueries;
}
// Source: https://www.tutorialspoint.com/accessing-nested-javascript-objects-with-string-key
static getProp(object, path) {
if (path.length === 1) {
return object[path[0]];
} else if (path.length === 0) {
throw new Error('Invalid property.');
} else {
if (object[path[0]]) return PrefetchTemplate.getProp(object[path[0]], path.slice(1));
else {
object[path[0]] = {};
return PrefetchTemplate.getProp(object[path[0]], path.slice(1));
}
}
}
static resolveParameter(unresolvedParameter, requestBundle) {
const paramField = paramElementMap.get(unresolvedParameter);
const resolvedParameter = PrefetchTemplate.getProp(requestBundle, paramField);
return resolvedParameter;
}
query;
constructor(query) {
this.query = query;
}
getQuery() {
return this.query;
}
}
const prefetchMap = PrefetchTemplate.generatePrefetchMap();
const paramElementMap = PrefetchTemplate.generateParamElementMap();