forked from smart-on-fhir/growth-chart-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-fhir-data.js
188 lines (162 loc) · 5.7 KB
/
load-fhir-data.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
window.GC = window.GC || {};
GC.get_data = function() {
var dfd = $.Deferred();
FHIR.oauth2.ready(onReady, onError);
function onError(){
console.log("Loading error", arguments);
dfd.reject({
responseText: "Loading error. See console for details."
});
};
function onErrorWithWarning(msg){
console.log("Loading error", arguments);
dfd.reject({
responseText: msg,
showMessage: true,
messageType: 'warning',
})
};
function onReady(smart){
var hidePatientHeader = (smart.tokenResponse.need_patient_banner === false);
GC.Preferences.prop("hidePatientHeader", hidePatientHeader);
function defaultOnFail(promise, defaultValue) {
var deferred = $.Deferred();
$.when(promise).then(
function (data) {
deferred.resolve(data);
},
function () {
deferred.resolve(defaultValue);
}
);
return deferred.promise();
};
var ptFetch = smart.patient.read();
var vitalsFetch = smart.patient.api.fetchAll({type: "Observation", query: {code: {$or: ['http://loinc.org|3141-9',
'http://loinc.org|8302-2', 'http://loinc.org|8287-5',
'http://loinc.org|39156-5', 'http://loinc.org|18185-9',
'http://loinc.org|37362-1', 'http://loinc.org|11884-4']}}});
var familyHistoryFetch = defaultOnFail(smart.patient.api.fetchAll({type: "FamilyMemberHistory"}), []);
$.when(ptFetch, vitalsFetch, familyHistoryFetch).done(onData);
function onData(patient, vitals, familyHistories){
// check patient gender
if (!isKnownGender(patient.gender)) onErrorWithWarning(GC.str('STR_Error_UnknownGender'));
var vitalsByCode = smart.byCode(vitals, 'code');
var t0 = new Date().getTime();
// Initialize an empty patient structure
var p = {
demographics: { },
vitals:{
lengthData: [],
weightData: [],
BMIData: [],
headCData: []
},
boneAge: [],
familyHistory: {
father : {
height: null,
isBio : false
},
mother : {
height: null,
isBio : false
}
}
};
// For debugging/exploration, a global handle on the output
console.log("Check out the parsed FHIR data: window.patient, window.vitalsByCode, window.familyHistories");
window.patient = patient;
window.vitalsByCode = vitalsByCode;
window.familyHistories = familyHistories;
var fname = patient.name[0].given.join(" ");
var lname = patient.name[0].family.join(" ");
p.demographics.name = fname + " " + lname;
p.demographics.birthday = patient.birthDate;
p.demographics.gender = patient.gender;
var gestAge = vitalsByCode['18185-9'];
if (gestAge === undefined) {
//handle an alternate mapping of Gest Age used by Cerner
gestAge = vitalsByCode['11884-4'];
}
if (gestAge && gestAge.length > 0) {
var weeks = 0, qty = gestAge[0].valueString ?
gestAge[0].valueString.value || '40W 0D' :
gestAge[0].valueQuantity ?
gestAge[0].valueQuantity.value || 40 :
40;
if (typeof qty == 'string') {
qty.replace(/(\d+)([WD])\s*/gi, function(token, num, code) {
num = parseFloat(num);
if (code.toUpperCase() == 'D') {
num /= 7;
}
weeks += num;
});
} else {
weeks = qty;
}
p.demographics.gestationalAge = weeks;
p.demographics.weeker = weeks;
}
var units = smart.units;
process(vitalsByCode['3141-9'], units.kg, p.vitals.weightData);
process(vitalsByCode['8302-2'], units.cm, p.vitals.lengthData);
process(vitalsByCode['8287-5'], units.cm, p.vitals.headCData);
process(vitalsByCode['39156-5'], units.any, p.vitals.BMIData);
processBA(vitalsByCode['37362-1'], p.boneAge);
function isKnownGender(gender) {
switch (gender) {
case 'male':
case 'female':
return true;
break;
}
return false;
}
function process(observationValues, toUnit, arr){
observationValues && observationValues.forEach(function(v){
arr.push({
agemos: months(v.effectiveDateTime, patient.birthDate),
value: toUnit(v.valueQuantity)
})
});
};
function processBA(boneAgeValues, arr){
boneAgeValues && boneAgeValues.forEach(function(v){
arr.push({
date: v.effectiveDateTime,
boneAgeMos: units.any(v.valueQuantity)
})
});
};
function months(d){
return -1 * new XDate(d).diffMonths(new XDate(p.demographics.birthday));
}
$.each(familyHistories, function(index, fh){
if (fh.resourceType === "FamilyMemberHistory") {
var code = fh.relationship.coding[0].code;
$.each(fh.extension || [], function(index, ext){
if (ext.url === "http://fhir-registry.smarthealthit.org/StructureDefinition/family-history#height") {
var ht = units.cm(ext.valueQuantity);
var r = null;
if (code === 'FTH') {
r = p.familyHistory.father;
} else if (code === 'MTH') {
r = p.familyHistory.mother;
}
if (r) {
r.height = ht;
r.isBio = true;
}
}
});
}
});
window.data = p;
console.log("Check out the patient's growth data: window.data");
dfd.resolve(p);
}
}
return dfd.promise();
};