UNPKG

11.6 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
5 | |
6 | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
7 | All rights reserved. |
8 | |
9 | http://rippleosi.org |
10 | Email: code.custodian@rippleosi.org |
11 | |
12 | Author: Rob Tweed, M/Gateway Developments Ltd |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 8 March 2017
28
29*/
30
31var patientHeadingTable = require('../patients/patientHeadingTable');
32var mpv = require('../patients/mpv');
33
34var test = {
35 "totalPatients": "5",
36 "patientDetails": [{
37 "source": "local",
38 "sourceId": "9999999000",
39 "name": "Marian Walsh",
40 "address": "51, Douglas Road, Dublin, D8",
41 "dateOfBirth": -806983200000,
42 "gender": "Female",
43 "nhsNumber": "9999999000",
44 "vitalsHeadline": {
45 "source": "c4hOpenEHR",
46 "sourceId": "260d0fdd-7fde-44f7-971d-366ad6a229b8::ripple_osi.ehrscape.c4h::1",
47 "latestEntry": 1456145840706,
48 "totalEntries": "17"
49 },
50 "ordersHeadline": {
51 "source": "c4hOpenEHR",
52 "sourceId": "29d11186-7c0d-4ced-b35d-510b06c9eff3::ripple_osi.ehrscape.c4h::1",
53 "latestEntry": 1460716236687,
54 "totalEntries": "70"
55 },
56 "medsHeadline": {
57 "source": "c4hOpenEHR",
58 "sourceId": "d252dd73-3292-4bbb-a0d7-6c870636a1a6::ripple_osi.ehrscape.c4h::1",
59 "latestEntry": 1476372000767,
60 "totalEntries": "21"
61 },
62 "resultsHeadline": {
63 "source": "c4hOpenEHR",
64 "sourceId": "e557afc8-b386-42a0-8d23-40c27bd35a5b::ripple_osi.ehrscape.c4h::1",
65 "latestEntry": 1427062262518,
66 "totalEntries": "2"
67 },
68 "treatmentsHeadline": {
69 "source": "c4hOpenEHR",
70 "sourceId": "b9f06818-f458-43e0-93e7-576cecf459b9::ripple_osi.ehrscape.c4h::1",
71 "latestEntry": 1475841942123,
72 "totalEntries": "18"
73 }
74 }, {
75 "source": "local",
76 "sourceId": "9999999019",
77 "name": "Rachel Walsh",
78 "address": "34, Summer Hill, Dublin, D8",
79 "dateOfBirth": -790045200000,
80 "gender": "Female",
81 "nhsNumber": "9999999019",
82 "vitalsHeadline": {
83 "source": "c4hOpenEHR",
84 "sourceId": null,
85 "latestEntry": null,
86 "totalEntries": "0"
87 },
88 "ordersHeadline": {
89 "source": "c4hOpenEHR",
90 "sourceId": null,
91 "latestEntry": null,
92 "totalEntries": "0"
93 },
94 "medsHeadline": {
95 "source": "c4hOpenEHR",
96 "sourceId": null,
97 "latestEntry": null,
98 "totalEntries": "0"
99 },
100 "resultsHeadline": {
101 "source": "c4hOpenEHR",
102 "sourceId": null,
103 "latestEntry": null,
104 "totalEntries": "0"
105 },
106 "treatmentsHeadline": {
107 "source": "c4hOpenEHR",
108 "sourceId": null,
109 "latestEntry": null,
110 "totalEntries": "0"
111 }
112 }, {
113 "source": "local",
114 "sourceId": "9999999022",
115 "name": "Tim Walsh",
116 "address": "84, High Street, Cork, CK",
117 "dateOfBirth": 632016000000,
118 "gender": "Male",
119 "nhsNumber": "9999999022",
120 "vitalsHeadline": {
121 "source": "c4hOpenEHR",
122 "sourceId": null,
123 "latestEntry": null,
124 "totalEntries": "0"
125 },
126 "ordersHeadline": {
127 "source": "c4hOpenEHR",
128 "sourceId": null,
129 "latestEntry": null,
130 "totalEntries": "0"
131 },
132 "medsHeadline": {
133 "source": "c4hOpenEHR",
134 "sourceId": "2ed64710-d50e-44ae-820c-0b8e8ea9f6b5::ripple_osi.ehrscape.c4h::2",
135 "latestEntry": 1446480737574,
136 "totalEntries": "1"
137 },
138 "resultsHeadline": {
139 "source": "c4hOpenEHR",
140 "sourceId": null,
141 "latestEntry": null,
142 "totalEntries": "0"
143 },
144 "treatmentsHeadline": {
145 "source": "c4hOpenEHR",
146 "sourceId": null,
147 "latestEntry": null,
148 "totalEntries": "0"
149 }
150 }, {
151 "source": "local",
152 "sourceId": "9999999057",
153 "name": "Marian Walsh",
154 "address": "24, Low Street, Dublin, D8",
155 "dateOfBirth": -560649600000,
156 "gender": "Female",
157 "nhsNumber": "9999999057",
158 "vitalsHeadline": {
159 "source": "c4hOpenEHR",
160 "sourceId": null,
161 "latestEntry": null,
162 "totalEntries": "0"
163 },
164 "ordersHeadline": {
165 "source": "c4hOpenEHR",
166 "sourceId": null,
167 "latestEntry": null,
168 "totalEntries": "0"
169 },
170 "medsHeadline": {
171 "source": "c4hOpenEHR",
172 "sourceId": null,
173 "latestEntry": null,
174 "totalEntries": "0"
175 },
176 "resultsHeadline": {
177 "source": "c4hOpenEHR",
178 "sourceId": null,
179 "latestEntry": null,
180 "totalEntries": "0"
181 },
182 "treatmentsHeadline": {
183 "source": "c4hOpenEHR",
184 "sourceId": null,
185 "latestEntry": null,
186 "totalEntries": "0"
187 }
188 }, {
189 "source": "local",
190 "sourceId": "9999999083",
191 "name": "Cillian Walsh",
192 "address": "75, Mallow View, Galway, GW",
193 "dateOfBirth": -472435200000,
194 "gender": "Male",
195 "nhsNumber": "9999999083",
196 "vitalsHeadline": {
197 "source": "c4hOpenEHR",
198 "sourceId": null,
199 "latestEntry": null,
200 "totalEntries": "0"
201 },
202 "ordersHeadline": {
203 "source": "c4hOpenEHR",
204 "sourceId": null,
205 "latestEntry": null,
206 "totalEntries": "0"
207 },
208 "medsHeadline": {
209 "source": "c4hOpenEHR",
210 "sourceId": null,
211 "latestEntry": null,
212 "totalEntries": "0"
213 },
214 "resultsHeadline": {
215 "source": "c4hOpenEHR",
216 "sourceId": null,
217 "latestEntry": null,
218 "totalEntries": "0"
219 },
220 "treatmentsHeadline": {
221 "source": "c4hOpenEHR",
222 "sourceId": null,
223 "latestEntry": null,
224 "totalEntries": "0"
225 }
226 }]
227};
228
229var pas;
230
231function search(searchString, session, callback) {
232
233 console.log('** in search: searchString = ' + searchString);
234
235 var q = this;
236 var headings = {
237 vitalsHeadline: 'vitals',
238 ordersHeadline: 'laborders',
239 medsHeadline: 'medications',
240 resultsHeadline: 'labresults',
241 treatmentsHeadline: 'procedures',
242 };
243
244 if (!pas) pas = require('../' + this.userDefined.rippleUser.pasModule);
245 pas.searchByPatient.call(this, searchString, function(results) {
246 if (results.error) {
247 if (callback) callback(results);
248 return;
249 }
250 if (results.totalPatients === 0) {
251 if (callback) callback(results);
252 return;
253 }
254
255 // now fetch the headline data
256 var patient;
257 var heading;
258
259 var patients = results.patientDetails;
260 var totalPatients = results.totalPatients;
261 var totalHeadings = 5;
262 var headingsCount = {};
263 var patientCount = 0;
264 console.log('fetching headline data');
265 patients.forEach(function(patient) {
266 console.log('** fetching headline counts data for patient ' + patient.nhsNumber);
267 headingsCount[patient.nhsNumber] = 0;
268 for (heading in headings) {
269 (function(patient, heading) {
270 var nhsNo = patient.nhsNumber;
271 var headingName = headings[heading];
272 console.log('* fetching heading ' + headingName + ' for patient ' + nhsNo);
273
274 if (headingName === 'vitals') {
275 headingsCount[nhsNo]++;
276 patient[heading] = {
277 source: 'c4hOpenEHR',
278 sourceId: null,
279 latestEntry: null,
280 totalEntries: 0
281 };
282 return;
283 }
284
285 // fetch the heading data for this patient
286 patientHeadingTable.call(q, nhsNo, headingName, session, function(headingResults) {
287
288 // calculate the heading headline info
289
290 patient[heading] = {
291 source: 'c4hOpenEHR',
292 sourceId: null,
293 latestEntry: null,
294 totalEntries: 0
295 };
296
297 // invoke callback if all done
298
299 headingsCount[nhsNo]++;
300 if (headingsCount[nhsNo] === totalHeadings) {
301 patientCount++;
302 if (patientCount === totalPatients) {
303 // clear the headings from the session to ensure patient summary retrieved in full if patient selected
304 session.data.$(['patients', nhsNo, 'headings']).delete();
305 if (callback) callback(results);
306 }
307 }
308 });
309 }(patient, heading));
310 }
311 });
312 });
313
314}
315
316function searchByPatient(args, callback) {
317 console.log('** in searchByPatient!');
318
319 var searchString = args.req.body.searchString;
320 if (!searchString || searchString === '') {
321 callback({error: 'Missing search string'});
322 return;
323 }
324
325 var session = args.session;
326 var patients = session.data.$('patients');
327
328 var q = this;
329 if (!patients.exists) {
330 mpv.init.call(this);
331 mpv.getPatients.call(this, args, function() {
332 search.call(q, searchString, session, callback);
333 });
334 return;
335 }
336
337 search.call(this, searchString, session, callback);
338
339 //var results = test;
340 //if (callback) callback(results);
341}
342
343module.exports = searchByPatient;