-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
225 lines (203 loc) · 5.3 KB
/
test.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
import { createRequire } from 'module';
import fs from 'fs';
import http from 'http';
import https from 'https';
import { v4 as uuidv4 } from 'uuid';
import InsulinAdministration from './src/InsulinAdministration.js';
import Observation from './src/Observation.js';
/*
* RUN THIS TEST AS:
* > node test.js > resultBundle.json
*/
const require = createRequire(import.meta.url);
const data = require('./data.json');
/* configs: */
// HAPI
/*
const patient = '7257379'; // HAPI FHIR
const postOptions = {
host: 'hapi.fhir.org',
port: '80',
path: '/baseR4',
method: 'POST',
headers: {
'Content-Type': 'application/fhir+json',
},
};
const postBundle = true;
const postKeys = true;
const postEntries = false;
*/
// Kanta STU3
/*
const patient = 'cafbe8e7-38fe-4807-b995-6770e5390365';
const postOptions = {
host: 'fhirsandbox.kanta.fi',
port: '443',
path: '/phr-resourceserver/baseStu3',
method: 'POST',
headers: {
'Content-Type': 'application/fhir+json',
},
};
const postBundle = false;
const postKeys = true;
const postEntries = true;
*/
// Kanta R4
/*
const patient = '77c2c8d2-11ce-41d5-8150-75489f8b0b0a';
const postOptions = {
host: 'fhirsandbox.kanta.fi',
port: '443',
path: '/phr-resourceserver/baseR4',
method: 'POST',
headers: {
'Content-Type': 'application/fhir+json',
},
};
const postBundle = true;
const postKeys = true;
const postEntries = true;
*/
const patient = '77c2c8d2-11ce-41d5-8150-75489f8b0b0a';
const postOptions = {
host: 'fhirsandbox.kanta.fi',
port: '443',
path: '/phr-resourceserver/baseR4',
method: 'POST',
headers: {
'Content-Type': 'application/fhir+json',
},
};
const postBundle = true;
const postKeys = true;
const postEntries = true;
const bundle = {
resourceType: "Bundle",
type: "batch",
entry: [],
};
const MAX_BUNDLE_SIZE = 300;
data.forEach((d) => {
let resource;
switch(d.type) {
case 'basal':
case 'bolus':
// Todo: handle more complex bolus types like extended and square wave
resource = new InsulinAdministration(patient, d);
break;
case 'cbg':
case 'smbg':
case 'wizard':
resource = new Observation(patient, d);
break;
case 'deviceEvent':
// ignore for now...
return;
default:
console.error(`Unhandled type ${d.type}`, d);
return;
}
if (bundle.entry.length < MAX_BUNDLE_SIZE) {
bundle.entry.push({
fullUrl: `urn:uuid:${uuidv4()}`,
resource,
request: {
url: `${resource.resourceType}/`,
method: 'POST',
ifNoneExist: 'identifier=' + resource.identifier[0]?.value,
},
});
}
});
try {
fs.writeFileSync('./bundle.json', JSON.stringify(bundle, null, 2));
} catch (err) {
console.error(err)
}
function postEntry(e) {
let responseChunks = [];
const isBatchBundle = (e.resourceType === 'Bundle') && (e.type === 'batch');
const request = (postOptions.port != 443 ? http : https).request({
...postOptions,
path: `${postOptions.path}/${
isBatchBundle
? '' // Post batch Bundles to the root.
: e.resourceType
}`,
}, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
responseChunks.push(chunk);
});
res.on('error', function(chunk) {
console.error('Error!', chunk);
});
res.on('end', function() {
const resBody = responseChunks.join('');
const contentType = res.headers['content-type'] || 'application/fhir+json';
const charsetSeparatorIndex = contentType.indexOf(';');
const mimeType = charsetSeparatorIndex
? contentType.substring(0, charsetSeparatorIndex)
: contentType;
switch(mimeType) {
case 'application/json':
case 'application/fhir+json':
const stored = JSON.parse(resBody);
if (isBatchBundle) {
// TODO: diff with the sent bundle
console.log(JSON.stringify(stored, null, 2));
} else {
const jsonied = JSON.parse(JSON.stringify(e));
Object.keys(jsonied).forEach((k) => {
if (stored[k] === undefined) {
console.error('Not stored', { [k]: e[k] }, mimeType);
}
});
}
break;
default:
console.error('Not parsing', contentType, resBody, res.statusCode);
}
});
});
// post the data
request.write(JSON.stringify(e));
request.on('error', function(error) {
console.error('Request error', error);
});
request.end();
}
const postedKeys = {};
if (postKeys) {
bundle.entry.forEach((e) => {
const { resource } = e;
const keys = [
...Object.keys(resource),
...(resource.code?.coding || []).map(c => `code ${c.code || c.display || JSON.stringify(c)}`),
...Object.keys(e.dosage || {})
].join();
if (!postedKeys[keys]) {
postEntry(resource);
postedKeys[keys] = true;
}
});
console.error('Posted keys', postedKeys, 'to', postOptions.host);
}
if (postBundle) {
postEntry(bundle);
console.error('Posted bundle to', postOptions.host);
}
if (postEntries) {
const MAX_ENTRY_COUNT = 1;
let i = 0;
const intervalID = setInterval(() => {
if ((i >= MAX_ENTRY_COUNT) || (i >= bundle.entry.length)) {
clearInterval(intervalID);
return;
}
postEntry(bundle.entry[i].resource);
i += 1;
}, 500);
}