-
Notifications
You must be signed in to change notification settings - Fork 3
/
cds-hook.js
141 lines (115 loc) · 4.29 KB
/
cds-hook.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
const express = require('express');
const moment = require('moment');
const fetch = require('node-fetch');
const asyncHandler = require('express-async-handler')
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
console.log(process.cwd());
// Services
const services = {
"services": [
{
"name": "PHI 533 CDS Hook Demo",
"title": "PHI 533 CDS Hook Demo",
"id": "phi533-prescribe",
"description": "Perform Public Health action based on medication prescription",
"hook": "medication-prescribe"
}
]
};
// Cards
// This patient qualifies for enrolment in a Public Health study. Click [here](https://www.doh.wa.gov/) to enroll.
const publicHealthResponse = (recoms) => {
if(recoms.bmi === undefined)
return {"cards": []};
var bmiMessage = "The patient's BMI is " + recoms.bmi.value + ". The patient is " + recoms.bmi.status + ".";
bmiMessage = bmiMessage + "<br>The patient has a " + recoms.bmi.risk;
bmiMessage = bmiMessage + "<br>The patient's ideal weight is: " + recoms.ideal_weight;
var cards = {
"cards": [
{
"summary": "👨🏻⚕️ BMI Information and Recommendations",
"detail": bmiMessage,
"source": {
"label": "WA DOH"
},
"indicator": "info",
"suggestions": []
}
]
}
return (cards);
};
const FHIR_SERVER_PREFIX = 'https://api.hspconsortium.org/cdshooksdstu2/open';
const BMI_SERVER_PREFIX = 'https://bmi.p.rapidapi.com/';
const buildObsURL = (patientId, text) => `${FHIR_SERVER_PREFIX}/Observation?patient=${patientId}&code:text=${text}&_sort:desc=date&_count=1`;
const buildPatientURL = (patientId) => `${FHIR_SERVER_PREFIX}/Patient/${patientId}`;
const getAsync = async (url) => await (await fetch(url)).json();
const bmiPostAsync = async (payload) => {
return await (await fetch(BMI_SERVER_PREFIX, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
'X-RapidAPI-Host': 'bmi.p.rapidapi.com',
'X-RapidAPI-Key': '2da80fad1cmsh95e44479f45f557p1d5c9bjsn5dbb9a6d483b',
'Accept': 'application/json'
}
})).json();
};
app.use(cors());
app.use(bodyParser.json());
app.get('/', asyncHandler(async (req, res, next) => {
res.send('PHI 533 CDS Hook Running!');
}));
app.get('/cds-services', asyncHandler(async (req, res, next) => {
res.send(services);
}));
/**
* Actual CDS Hook logic here
*/
app.post('/cds-services/phi533-prescribe', asyncHandler(async (req, res, next) => {
console.log("CDS Request: \n" + JSON.stringify(req.body, null, ' '));
// Extracts useful information from the data sent from the Sandbox
const hook = req.body.hook; // Type of hook
const fhirServer = req.body.fhirServer; // URL for FHIR Server endpoint
const patient = req.body.patient; // Patient Identifier
// Chosen Problem to Treat
const med = req.body.context.medications.entry[0].resource;
const reason = (med.reasonCodeableConcept === undefined ? "" : med.reasonCodeableConcept.text);
console.log("Useful parameters:");
console.log("hook: " + hook);
console.log("fhirServer: " + fhirServer);
console.log("patient: " + patient);
console.log("reason: " + reason);
const patientReq = await getAsync(buildPatientURL('SMART-1551992'));
const weightReq = await getAsync(buildObsURL('SMART-1551992', 'weight'));
const heightReq = await getAsync(buildObsURL('SMART-1551992', 'height'));
const gender = patientReq.gender;
const birthDate = patientReq.birthDate;
const weight = weightReq.entry[0].resource.valueQuantity.value;
const height = heightReq.entry[0].resource.valueQuantity.value;
const age = moment().diff(birthDate, 'years');
console.log('Age: ', age);
console.log('Gender: ', gender);
console.log('Weight: ', weight);
console.log('Height: ', height);
const bmiData = await bmiPostAsync({
age: 24,
sex: 'f',
weight: {
value: "85.00",
unit: "kg"
},
height: {
value: "170.00",
unit: "cm"
}
});
console.log("BMI Data:" + JSON.stringify(bmiData, null, ' '));
const bmiInfo = publicHealthResponse(bmiData);
console.log("Responding with: \n" + JSON.stringify(bmiInfo, null, ' '));
res.send(bmiInfo);
}));
app.listen(3003, () => console.log('Example app listening on port 3003!'))