-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFhirResource.js
176 lines (112 loc) · 4.88 KB
/
getFhirResource.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
const logging = require('./logging')
const dotenv = require('dotenv');
const { response } = require('express');
const keycloakAuth = require('./keycloakAuth')
dotenv.config();
const getFhirResource = async (method, fhirResource, resourceId, query, bodyContent) => {
const accessToken = await keycloakAuth.accessToken.get();
if(method == "GET") {
const options = {
method: 'GET',
headers: {
Authorization: ` Bearer ${accessToken}`
}
}
const url = process.env.HAPI_BASE_URL + (fhirResource ? `/${fhirResource}` : '') + (resourceId ? `/${resourceId}` : '') + (query ? `?${query}` : '')
try {
logging('Info', `Querying fhir server initiated (Url: ${url})`)
const urlResponse = await fetch(url, options)
const jsonResponse = await urlResponse.json();
//return single resource if you have resource id
if(resourceId) {
logging('Info', `Resources returned based on the single resource query. Resource: ${fhirResource}, Resource id: ${resourceId}`)
if(jsonResponse) {
return {
response: jsonResponse,
status: true
}
} else {
logging('Info', `Exitting since there is no resources.`)
return {
status: false
};
}
} else {
logging('Info', `Resources returned based on the query ${jsonResponse.total}`)
if(jsonResponse.total > 0) {
return {
response: jsonResponse,
status: true
}
} else {
logging('Info', `Exitting since there is no resources. Returned resource count: ${jsonResponse.total}`)
return {
status: false
};
}
}
} catch(error) {
logging('Error', `Unable to query the fhir server. Error: ${error}`)
return false;
}
} else if (method == "PATCH") {
const options = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json-patch+json',
Authorization: ` Bearer ${accessToken}`
},
body: bodyContent
}
const url = process.env.HAPI_BASE_URL + (fhirResource ? `/${fhirResource}` : '') + (resourceId ? `/${resourceId}` : '') + (query ? `?${query}` : '')
try {
logging('Info', `Querying fhir server initiated (Url: ${url})`)
const urlResponse = await fetch(url, options)
const jsonResponse = await urlResponse.json();
//return single resource if you have resource id
logging('Info', `Updating resource. Resource: ${fhirResource}, Resource id: ${resourceId}`)
if(jsonResponse) {
logging('Info', `Successfully updated the resource. Resource: ${fhirResource}, Resource id: ${resourceId}`)
return {
response: jsonResponse,
status: true
}
} else {
logging('Info', `Exitting since there is no resources.`)
return {
status: false
};
}
} catch(error) {
logging('Error', `Unable to query the fhir server. Error: ${error}`)
return false;
}
} else if (method == "POST") {
const options = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json-patch+json',
Authorization: ` Bearer ${accessToken}`
},
body: JSON.stringify(bodyContent)
}
const url = process.env.HAPI_BASE_URL + (fhirResource ? `/${fhirResource}` : '') + (resourceId ? `/${resourceId}` : '') + (query ? `?${query}` : '')
try {
logging('Info', `POSTing to fhir server initiated (Url: ${url})`)
const urlResponse = await fetch(url, options)
const jsonResponse = await urlResponse.json();
//return single resource if you have resource id
logging('Info', `POSTed resource. Resource: ${fhirResource}, Resource id: ${resourceId}`)
if(jsonResponse) {
logging('Info', `Successfully updated the resource. Resource: ${fhirResource}, Resource id: ${resourceId}`)
} else {
logging('Info', `Something went wrong in posting`)
}
} catch(error) {
logging('Error', `Unable to POST to the server. Error: ${error}`)
return false;
}
}
}
module.exports = getFhirResource