-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfhir-transformer.js
201 lines (185 loc) · 5.88 KB
/
fhir-transformer.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
import uniqid from "uniqid";
import dayjs from "dayjs";
import Utils from "../utilities/index.js";
export default class FHIRTrasformer {
GK_SYSTEM = "https://github.com/GATEKEEPER-OU/coolsim/";
constructor(date = null) {
this._startDate = date === null ? dayjs() : dayjs(date);
}
convert(doc) {
if (!doc) {
throw new Error(`Error: nothing to convert`);
}
// build fhir
if (Array.isArray(doc)) { return this._convertBulk(doc) }
return this._convert(doc);
}
_getCurrentDay(day = 0) {
return this._startDate.add(day, 'day').format();
}
_convertBulk(docs) {
// cycle docs and covert
return docs.map(d => this._convert(d));
}
_convert(doc) {
let id = doc.simulation.concat("-", uniqid());
let date = this._getCurrentDay(doc.day);
let entry = [];
let person = this._person(id, doc);
// generate array of activities
let activities = this._actions(id, date, doc.agent, doc.activities.activities);
// generate array of conditions
let conditions = this._conditions(id, date, doc.agent, doc.conditions);
// generate array of questionnaire answers
let questionnaires = this._questionnaires(id, date, doc.agent, doc.stats);
entry = entry.concat(person, ...activities, ...conditions, ...questionnaires);
return entry;
}
_person(id, doc) {
// let coding = this._getCode(doc.agent, "patient")
const identifier = this._getIdentifier(
`identifier=${this.GK_SYSTEM}patient`,
id // value
);
return this._entry(id, {
"resourceType": "Patient",
"identifier": [ identifier ],
"active": true,
"gender": doc.gender,
// "age": doc.age
// @todo extend with age
})
}
// Action ?== Observation
_actions(id, date, agent, actions) {
if(!actions || !Object.keys(actions)){
console.log(actions);
throw `actions is mandatory and must be an object`
}
// if not defined, return an array of observations for each activity of daily living
return Object.keys(actions).map(actionName => {
const duration = actions[actionName];
const codingActivity = this._getCode(actionName, "activity");
const codingUnit = this._getCode(duration, "unit/h");
return this._entry(id, {
"resourceType": "Observation",
"effectiveDateTime": date,
"code": [ codingActivity ],
"component": [
{
"code": [codingActivity],
"valueQuantity": codingUnit
}
],
"status": "preliminary",
"subject": {
"display": agent,
// "reference": id // @todo should be agentId ???
"reference": agent,
}
});
});
}
_conditions(id, date, agent, conditions) {
if(!conditions || !Object.keys(conditions)){
console.log(conditions);
throw `conditions is mandatory and must be an object`
}
// if not defined, return an array of observations for each activity of daily living
return Object.keys(conditions).map(conditionName => {
const condition = conditions[conditionName];
// console.log("codingCondition",conditionName);
const codingCondition = this._getCode(conditionName,"condition");
// console.log("codingStage",condition.status);
const codingStage = this._getCode(condition.status, "condition_stage");
return this._entry(id, {
"resourceType": "Condition",
"recordedDate": date,
"severity": condition.severity,
"code": codingCondition,
"stage": codingStage,
"subject": {
"display": agent,
// "reference": id // @todo should be agentId ???
"reference": agent,
}
})
});
}
_questionnaires(id, date, agent, stats) {
if(!stats || !Object.keys(stats)){
console.log(stats);
throw `stats is mandatory and must be an object`;
}
// if not defined, return an array of observations for each activity of daily living
return Object.keys(stats).map(statName => {
const stat = stats[statName];
const codingSurvey = this._getCode(statName, "questionnaire");
const severityLevel = Utils.rate.rateToSeverity(stat.severity)
let codingAnswer = this._getCode(severityLevel, statName+"_state/severity_level");
return this._entry(id, {
"resourceType": "QuestionnaireResponse",
"text": `What is your ${statName} state?`,
"recordedDate": date,
"status":"completed",
"identifier":[
codingSurvey
],
// @todo check with Alessio
// "contained": [
// // this._person(...)
// {
// "resourceType": "Patient",
// "id": id,
// "identifier": [
// {
// "display": agent,
// "reference": id
// }
// ]
// }
// ],
// // -------------------------
"item":[
{
"linkId": "1",
"answer": [
{
"valueCoding": codingAnswer
}
]
}
]
})
});
}
_entry(id, resource) {
return {
fullUrl: id,
// request: {}, // @todos
resource
};
}
// @todo
_getIdentifier(system, value) {
return {
system,
value
}
}
// retrieves or generates a coding system in FHIR format
_getCode(term, type = "element") {
// @todo if coding is defined return
// if coding is not defined, generates a coding
if(!term || !(typeof term === "string" || typeof term === "number") ){
console.log(`_getCode, typeof term = ${typeof term}, got ${term}`)
throw `term is mandatory and must be a string, got ${term} of type ${typeof term}`;
}
// console.log("term",term);
return {
code: term.toString().trim().toLowerCase().replaceAll(" ", "-"),
display: term,
system: this.GK_SYSTEM + type
}
}
}