-
Notifications
You must be signed in to change notification settings - Fork 1
/
adfice-webserver.js
582 lines (515 loc) · 20.8 KB
/
adfice-webserver.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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2021-2024 Stichting Open Electronics Lab
// vim: set sts=4 shiftwidth=4 expandtab :
"use strict";
const http = require('http');
const ws = require('ws');
const express = require('express');
const session = require('express-session');
const ejs = require('ejs');
const util = require('util');
const showdown = require('showdown');
const adfice_factory = require('./adfice.js');
const autil = require('./adfice-util');
const cookie_secret = autil.uuid4_new_string();
const DEBUG = ((process.env.DEBUG !== undefined) &&
(process.env.DEBUG !== "0"));
// this should change very rarely, but ideally we get it dynamically
var iss = "";
function log_debug(server, msg) {
if (DEBUG) {
server.logger.log(msg);
}
}
log_debug({
logger: console
}, `DEBUG: ${DEBUG}`);
async function create_webserver(hostname, port, logger, etl, etl_opts_path) {
let adfice = adfice_factory.adfice_init();
async function get_data_for_patient(req, res) {
let patient_id = req.query.id || req.query.patient || 0;
let patient_advice = await adfice.get_advice_for_patient(patient_id);
let help_phone = await adfice.get_help_phone();
let data = {
patient_id: patient_id,
patient_advice: patient_advice,
help_phone: help_phone,
};
return data;
}
async function json_advice(req, res) {
res.json(await get_data_for_patient(req, res));
}
async function render_validation_advice(req, res) {
let data = await get_data_for_patient(req, res);
data['md'] = new showdown.Converter();
data['lang'] = 'nl';
res.render("patient-validation", data); // .ejs
}
let app = express();
let max_session_ms = 8 * 60 * 60 * 1000; // 8 hours
var session_manager = session({
secret: cookie_secret,
cookie: {
maxAge: max_session_ms
}
});
app.use(session_manager);
const server = http.createServer(app);
server.wss = new ws.Server({
noServer: true
});
app.use("/static", express.static('static'));
app.use("/assets", express.static('static'));
app.use('/favicon.ico', express.static('static/favicon.ico'));
app.use("/start", express.static('static/start.html'));
app.use("/prep", express.static('static/prep.html'));
app.use("/consult", express.static('static/consult.html'));
app.use("/advise", express.static('static/advise.html'));
app.use("/finalize", express.static('static/finalize.html'));
app.set('view engine', 'ejs');
app.get("/advice", json_advice);
async function render_index(req, res) {
res.render("index"); //.ejs
}
app.get("/", render_index);
app.get("/index", render_index);
app.get("/index.html", render_index);
app.get("/patient", express.static('static/start.html'));
app.get("/patient-validation", render_validation_advice);
app.use("/load-error", express.static('static/load-error.html'));
app.use("/loading", express.static('static/loading.html'));
let standalone_flag = await adfice.get_env_var('STANDALONE');
if (standalone_flag) { // note that any value for STANDALONE seems to evaluate to TRUE
// gets basic info to create session and patient
app.use("/dataentry", express.static('static/dataentry.html'));
// allow user to add data about patient
app.use("/dataentry2", express.static('static/dataentry2.html'));
app.get('/user-entered', async function(req, res) {
let id = null;
let birth_date = req.query.birthdate;
let participant = req.query.participant;
let mrn = 'Deelnemer-' + participant;
let user_id = autil.string_to_hash(req.query.user);
try{
id = await adfice.id_for_mrn(mrn);
} catch (error) {
console.log(error);
}
if(!id){
if(!birth_date || !participant || !user_id){
res.redirect('/dataentry?error=missing');
return;
}
let patient = {
mrn: mrn,
ehr_pid: null,
bsn: null,
refresh_token: 'none',
birth_date: birth_date,
participant_number: participant
};
try {
id = await adfice.write_patient_from_json(patient);
} catch (error) {
console.log(error);
res.redirect('/dataentry?error=creeateFailed');
return;
}
}
let doctor_id = await adfice.doctor_id_for_user(user_id);
log_debug(server, 'setting doctor id:', doctor_id);
req.session.doctor_id = doctor_id;
res.redirect('/dataentry2?id=' + id); // TODO go to second data entry page to handle rest of patient data
});
app.get('/user-entered-patients', async function(req, res) {
let user_id = req.query.user;
let doctor_id = await adfice.doctor_id_for_user(user_id);
log_debug(server, 'setting doctor id:', doctor_id);
req.session.doctor_id = doctor_id;
});
}
app.get('/auth', async function(req, res) {
let code = req.query.code;
let state = req.query.state;
let etl_opts = await autil.from_json_file(etl_opts_path);
let adfice_url = 'https://' + req.get('host') + '/auth';
let token_json = await etl.getToken(code, state, adfice_url, etl_opts);
let id = null;
let mrn = null;
let fhir = null;
if (!token_json || Object.keys(token_json).length === 0) {
res.redirect('/load-error?err=Authorisatiefout');
return;
}
let decoded_state = Buffer.from(state, 'base64').toString('utf-8');
let state_json = JSON.parse(decoded_state);
let user_id = token_json.user;
if (state_json.patient_id) {
id = state_json.patient_id;
mrn = state_json.mrn;
fhir = state_json.ehr_pid;
} else {
let etl_patient = await etl.etl(token_json, etl_opts);
if (!etl_patient || Object.keys(etl_patient).length === 0) {
res.redirect('/load-error?err=Error%20met%20laden%20van%20patientdata');
return;
}
try {
id = await adfice.write_patient_from_json(etl_patient);
mrn = etl_patient.mrn;
fhir = etl_patient.ehr_pid;
} catch (error) {
res.redirect('/load-error?err=Error%20met%20laden%20van%20patientdata');
return;
}
}
if (!id) {
res.redirect('/load-error?err=Error%20met%20laden%20van%20patientdata');
return;
}
await adfice.add_log_event_access(user_id, id);
let doctor_id = await adfice.doctor_id_for_user(user_id);
log_debug(server, 'setting doctor id:', doctor_id);
req.session.doctor_id = doctor_id;
// For testing, allow the session timeout to be set to a shorter value. It cannot be set to a longer value.
let tsec = state_json.tsec;
if (tsec < max_session_ms / 1000) {
req.session.cookie.maxAge = tsec * 1000;
}
res.redirect('/start?id=' + id);
});
app.get('/load', async function(req, res) {
res.set('Cache-Control', 'no-cache, must-revalidate, max-age=-1');
res.set('Pragma', 'no-cache, must-revalidate');
res.set('Expires', '-1');
let mrn = req.query.mrn;
let fhir = req.query.fhir;
let user_id = req.query.user;
// ISSuer identifier authorization server
if(req.query.iss){
iss = req.query.iss;
}
let etl_opts = await autil.from_json_file(etl_opts_path);
let adfice_url = 'https://' + req.get('host') + '/auth';
let id = null;
let error_string = '';
if (fhir == null || fhir == '' || typeof(fhir) != 'string' /* 2 FHIRs in URL */ ) {
fhir = null;
error_string += "no FHIR id ";
}
if (mrn == null || mrn == '' || typeof(mrn) != 'string' /* 2 MRNs in URL */ ) {
mrn = null;
error_string += "no MRN ";
}
if (user_id == null || user_id == '' || typeof(user_id) != 'string' /* 2 user */ ) {
user_id = null;
error_string += "no user ID";
}
if ((!fhir && !mrn) || !user_id) {
let p_str = '?err=' + error_string;
res.redirect('/load-error' + p_str);
return;
}
if (fhir) {
id = await adfice.id_for_fhir(fhir);
} else {
id = await adfice.id_for_mrn(mrn);
};
let encoded_err = null;
try {
let etl_opts = await autil.from_json_file(etl_opts_path);
let auth = await etl.getAuth(etl_opts, adfice_url, req.url, id, req.query.tsec);
if (Object.keys(auth).length == 0) {
res.redirect('/load-error?err=authorisatiefout');
} else {
res.set(auth.headers);
res.redirect(auth.url);
}
} catch (err) {
encoded_err = encodeURIComponent('' + err);
res.redirect('/load-error?err=' + encoded_err);
}
});
// // sketch of post support
// app.use(express.urlencoded({extended:false}));
// app.post('/load', function (req, res) {
// mrn = req.body.mrn;
// // ...
// });
server.receivers = {};
function msg_header(message, kind, id) {
message.kind = kind;
let id_key = `${kind}_id`;
message[id_key] = id;
message.viewers = server.receivers[kind][id].size;
}
function send_all(kind, id, message) {
msg_header(message, kind, id);
let msg_string = JSON.stringify(message, null, 4);
if (DEBUG > 0) {
console.log('sending all:\n', msg_string);
}
server.receivers[kind][id].forEach((rws) => {
rws.send(msg_string);
});
}
function hello_all(kind, id) {
let message = {};
message.type = 'hello';
message.info = 'world';
send_all(kind, id, message);
}
function make_error_message(text, kind, patient_id) {
let err_msg = {};
err_msg.type = 'error_message';
err_msg.text = text;
err_msg.time = Date.now();
try {
msg_header(err_msg, kind, patient_id);
} catch (error) {
console.log(error);
}
return err_msg;
}
function send_error(ws, text, kind, patient_id) {
let err_msg = make_error_message(text, kind, patient_id);
let msg_string = JSON.stringify(err_msg, null, 4);
ws.send(msg_string);
}
function has_user_activity(message) {
if (message.type != 'ping') {
return true;
}
if (message.reset_session_timeout) {
return true;
}
return false;
}
async function handle_patient_message(ws, doctor_id, patient_id, kind,
message, timeout_ms_remaining) {
if (('box_states' in message) ||
('field_entries' in message)) {
let selections = message['box_states'];
let freetexts = message['field_entries'];
await adfice.set_advice_for_patient(
patient_id, doctor_id, selections, freetexts);
}
if (message.type == 'definitive') {
let result = await adfice.finalize_and_export(patient_id);
if (result.error) {
send_error(ws, result.error, kind, patient_id);
} else {
let new_msg = await patient_advice_message(kind,
patient_id);
send_all(kind, patient_id, new_msg);
}
} else if (message.type == 'patient_renew') {
await adfice.add_log_event_renew(doctor_id, patient_id);
await adfice.set_load_state(patient_id,1);
let etl_opts = await autil.from_json_file(etl_opts_path);
let refresh_data = await adfice.get_refresh_data(patient_id);
let etl_patient = await etl.renew(refresh_data, etl_opts);
if (!etl_patient || Object.keys(etl_patient).length == 0) {
let err_text_en = "etl_renew failed to retrieve new patient data for " + patient_id;
send_error(ws, err_text_en, kind, patient_id);
await adfice.set_load_state(patient_id,0);
} else {
let returned_patient = await adfice.renew_patient(patient_id, etl_patient);
if (returned_patient != patient_id) {
let err_text_en = "etl_renew( " +
patient_id +
" ) unexpectedly returned " +
returned_patient;
console.log(err_text_en);
send_error(ws, err_text_en, kind, patient_id);
} else {
let new_msg = await patient_advice_message(kind,
patient_id);
// console.log(new_msg);
send_all(kind, patient_id, new_msg);
}
await adfice.set_load_state(patient_id,0);
}
} else if (message.type == 'was_printed') {
await adfice.add_log_event_print(doctor_id, patient_id);
} else if (message.type == 'was_copied_patient') {
await adfice.add_log_event_copy_patient_text(doctor_id,
patient_id);
} else if (message.type == 'was_copied_ehr') {
await adfice.add_log_event_copy_ehr_text(doctor_id,
patient_id);
} else if (message.type == 'ping') {
let pong = {};
pong.type = 'pong';
pong.sent = message.sent;
pong.recv = Date.now();
pong.timeout_ms_remaining = timeout_ms_remaining;
msg_header(pong, kind, patient_id);
let msg_string = JSON.stringify(pong, null, 4);
if (DEBUG > 2) {
console.log('sending reply:\n', msg_string);
}
ws.send(msg_string);
} else if (message.type == 'submit_missings') {
await adfice.update_prediction_with_user_values(
patient_id, message['submit_missings']);
} else if (message.type == 'submit_birthdate') {
await adfice.update_birthdate(
patient_id, message['submit_birthdate']);
} else if (message.type == 'submit_single_med') {
await adfice.add_single_med(
patient_id, message['submit_single_med']);
} else if (message.type == 'submit_multi_med') {
await adfice.add_multi_med(
patient_id, message['submit_multi_med']);
} else if (message.type == 'remove_med') {
await adfice.remove_med(
message['remove_med']['atc_code'], patient_id);
} else if (message.type == 'submit_problems') {
await adfice.add_problems(
patient_id, message['submit_problems']);
} else if (message.type == 'submit_labs') {
await adfice.add_labs(
patient_id, message['submit_labs']);
} else if (message.type == 'remove_lab') {
await adfice.remove_lab(
message['remove_lab']['lab_test_name'], patient_id);
} else if (message.type == 'submit_meas') {
await adfice.add_meas(
patient_id, message['submit_meas']);
} else if (message.type == 'submit_assess') {
await adfice.data_assessed(
patient_id, message['submit_assess']);
} else {
send_all(kind, patient_id, message);
}
}
var global_patient_advice_message_count = 0;
async function patient_advice_message(kind, id) {
++global_patient_advice_message_count;
/* viewer_id should come from the session */
let viewer_id = global_patient_advice_message_count;
let patient_advice = await adfice.get_advice_for_patient(id);
if (patient_advice.patient_id != id) {
return make_error_message('patient id not valid', kind, id);
}
let freetexts = patient_advice.free_texts;
let selections = patient_advice.selected_advice || {};
let message = {};
msg_header(message, kind, id);
message.type = 'init';
message.info = 'hello';
message.viewer_id = viewer_id;
message['field_entries'] = freetexts;
message['box_states'] = selections;
message['is_final'] = patient_advice.is_final;
if ('debug_info' in patient_advice) {
message['debug_info'] = patient_advice.debug_info;
}
return message;
}
async function init_patient_data(ws, kind, id) {
let message = await patient_advice_message(kind, id);
let msg_string = JSON.stringify(message, null, 4);
if (DEBUG > 0) {
console.log('sending init:\n', msg_string);
}
ws.send(msg_string);
}
server.on('upgrade', function upgrade(request, socket, head) {
// reconnect session/cookie info to request using express API
// TODO: consider pull request on express-session to add this
session_manager(request, {}, function() {});
const pathname = request.url;
server.wss.handleUpgrade(request, socket, head,
async function done(ws) {
let path_parts = pathname.split('/');
let id = path_parts.pop();
let kind = path_parts.pop();
if (kind == 'advice') { // JSON version
kind = 'patient'; // EJS version
}
if (DEBUG > 0) {
console.log(`adding receiver[${kind}][${id}]`);
}
if (server.receivers[kind] == null) {
server.receivers[kind] = {};
}
if (server.receivers[kind][id] == null) {
server.receivers[kind][id] = new Set();
}
server.receivers[kind][id].add(ws);
ws.on('close', function clear() {
if (DEBUG > 0) {
console.log(`removing receiver[${kind}][${id}]`);
}
server.receivers[kind][id].delete(ws);
hello_all(kind, id);
});
ws.on('message', async function incoming(data) {
if (DEBUG > 2) {
console.log('received: ', data);
}
if (request.session.cookie.maxAge < 1) {
ws.close();
return;
}
try {
if (!request.session) {
throw 'No session?';
}
let doctor_id = request.session.doctor_id;
if (DEBUG > 0) {
console.log('got doctor id:', doctor_id);
}
if (!doctor_id) {
const err_msg_txt = 'No doctor_id in session';
if (DEBUG > 0) {
console.log(JSON.stringify({
err_msg: err_msg_txt,
data: data,
request: request,
}));
}
send_error(ws, err_msg_txt, kind, id);
throw err_msg_txt;
}
let message = JSON.parse(data);
let user_activity = has_user_activity(message);
if (user_activity) {
request.session.touch();
}
if (DEBUG > 0 && message.type != 'ping') {
console.log('received: ', data);
}
message.viewers = server.receivers[kind][id].size;
let id_key = `${kind}_id`;
let timeout_ms_remaining = request.session.cookie.maxAge;
if (message[id_key] == id) {
let patient_id = id;
handle_patient_message(ws, doctor_id, patient_id,
kind, message, timeout_ms_remaining);
}
} catch (error) {
console.log(error);
}
});
if (kind == 'patient') {
await init_patient_data(ws, kind, id);
}
hello_all(kind, id);
});
});
server.listen(port, () => {
console.log(`server is listening on ${port}`);
});
server.on('close', function() {
adfice.shutdown();
console.log(`closing server running on ${port}`);
});
return server;
}
module.exports = {
create_webserver: create_webserver,
}