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 | var mySQL = require('./mySQL');
|
32 | var template = require('qewd-template');
|
33 | var dateTime = require('./dateTime');
|
34 |
|
35 | var count;
|
36 |
|
37 | function getDepartments(callback) {
|
38 | var deptsCache = new this.documentStore.DocumentNode('rippleMedicalDepts');
|
39 |
|
40 | var query = 'SELECT * FROM medical_departments';
|
41 | mySQL.query(query, function(depts) {
|
42 | if(depts.error) {
|
43 | callback(depts.error);
|
44 | return;
|
45 | }
|
46 | depts.forEach(function(dept) {
|
47 | deptsCache.$(dept.id).setDocument(dept);
|
48 | });
|
49 | if (callback) callback();
|
50 | });
|
51 | }
|
52 |
|
53 | function getGPs(callback) {
|
54 | var gpCache = new this.documentStore.DocumentNode('rippleGPs');
|
55 |
|
56 | var query = 'SELECT * FROM general_practitioners';
|
57 | mySQL.query(query, function(gps) {
|
58 | if(gps.error) {
|
59 | callback(gps.error);
|
60 | return;
|
61 | }
|
62 | gps.forEach(function(gp) {
|
63 | gpCache.$(gp.id).setDocument(gp);
|
64 | });
|
65 | if (callback) callback();
|
66 | });
|
67 | }
|
68 |
|
69 | function formatPatientData(row) {
|
70 |
|
71 | var gpCache = new this.documentStore.DocumentNode('rippleGPs');
|
72 | var deptCache = new this.documentStore.DocumentNode('rippleMedicalDepts');
|
73 | var gpData = gpCache.$(row.gp_id);
|
74 | var gp = gpData.getDocument();
|
75 |
|
76 | var patient = {};
|
77 | patient.id = row.nhs_number;
|
78 | patient.nhsNumber = row.nhs_number;
|
79 | patient.name = row.first_name + ' ' + row.last_name;
|
80 | patient.address = formatAddressData(row.address_1, row.address_2, row.address_3, row.address_4, row.address_5, row.postcode);
|
81 | patient.dateOfBirth = new Date(row.date_of_birth).getTime();
|
82 | patient.gender = row.gender || '';
|
83 | patient.phone = row.phone || '';
|
84 | patient.gpName = gp.gp_name || '';
|
85 | patient.gpAddress = formatAddressData(gp.address_1, gp.address_2, gp.address_3, gp.address_4, gp.address_5, gp.postcode);
|
86 | patient.pasNo = row.pas_number || '';
|
87 | patient.department = deptCache.$([row.department_id, 'department']).value;
|
88 |
|
89 | return patient;
|
90 | }
|
91 |
|
92 | function formatAddressData(address_1, address_2, address_3, address_4, address_5, postcode) {
|
93 | var address = '';
|
94 | var comma = ' ';
|
95 | if (address_1) {
|
96 | address = address_1;
|
97 | comma = ', ';
|
98 | }
|
99 | if (address_2) {
|
100 | address = address + comma + address_2;
|
101 | comma = ', ';
|
102 | }
|
103 | if (address_3) {
|
104 | address = address + comma + address_3;
|
105 | comma = ', ';
|
106 | }
|
107 | if (address_4) {
|
108 | address = address + comma + address_4;
|
109 | comma = ', ';
|
110 | }
|
111 | if (address_5) {
|
112 | address = address + comma + address_5;
|
113 | comma = ', ';
|
114 | }
|
115 | if (postcode) {
|
116 | address = address + comma + postcode;
|
117 | comma = ', ';
|
118 | }
|
119 |
|
120 | return address;
|
121 | }
|
122 |
|
123 | function advancedSearch(params, callback) {
|
124 |
|
125 | if (!params.surname || params.surname === '') {
|
126 | if (callback) callback ({error: 'Missing or invalid surname'});
|
127 | return;
|
128 | }
|
129 | if (!params.forename || params.forename === '') {
|
130 | if (callback) callback ({error: 'Missing or invalid forename'});
|
131 | return;
|
132 | }
|
133 | if (!params.dateOfBirth || params.dateOfBirth === '') {
|
134 | if (callback) callback ({error: 'Missing or invalid dateOfBirth'});
|
135 | return;
|
136 | }
|
137 | params.dateOfBirth = dateTime.toSqlPASFormat(params.dateOfBirth);
|
138 |
|
139 | var query = 'SELECT * FROM patients P WHERE P.first_name LIKE \'{{forename}}%\' AND P.last_name LIKE \'{{surname}}%\' AND P.date_of_birth = \'{{dateOfBirth}}\'';
|
140 | if (params.gender && params.gender !== '') {
|
141 | query = query + ' AND P.gender EQUALS \'{{gender}}\'';
|
142 | }
|
143 | query = template.replace(query, params);
|
144 | var q = this;
|
145 |
|
146 | mySQL.query(query, function(rows) {
|
147 | if(rows.error) {
|
148 | if (callback) callback(rows);
|
149 | return;
|
150 | }
|
151 | var results = [];
|
152 | rows.forEach(function(row) {
|
153 | results.push(formatPatientData.call(q, row));
|
154 | });
|
155 | if (callback) callback(results);
|
156 | });
|
157 | }
|
158 |
|
159 | function searchByPatient(searchString, callback) {
|
160 |
|
161 | searchString = searchString.replace(/,/g , ' ');
|
162 |
|
163 | searchString = searchString.replace(/\s\s+/g, ' ');
|
164 |
|
165 | var pieces = searchString.split(' ');
|
166 |
|
167 | if (pieces.length === 0) {
|
168 | callback({error: 'Invalid search string'});
|
169 | return;
|
170 | }
|
171 |
|
172 | var firstName;
|
173 | var lastName;
|
174 | var nhsNumber;
|
175 | var dateOfBirth;
|
176 |
|
177 | if (pieces.length === 1) {
|
178 | lastName = pieces[0];
|
179 | firstName = '';
|
180 | if (Number.isInteger(parseInt(lastName))) {
|
181 | nhsNumber = lastName;
|
182 | lastName = '';
|
183 | firstName = '';
|
184 | dateOfBirth = '';
|
185 | }
|
186 | }
|
187 | else {
|
188 | firstName = pieces[0];
|
189 | lastName = pieces[1] || '';
|
190 | dateOfBirth = pieces[2] || '';
|
191 | }
|
192 |
|
193 | var query;
|
194 | if (nhsNumber) {
|
195 | query = 'SELECT * FROM patients P WHERE P.nhs_number = \'{{nhsNumber}}\'';
|
196 | }
|
197 |
|
198 | else {
|
199 | nhsNumber = '';
|
200 |
|
201 | if (lastName === '') {
|
202 | callback({error: 'Last Name not defined'});
|
203 | return;
|
204 | }
|
205 |
|
206 | query = 'SELECT * FROM patients P WHERE P.last_name LIKE \'{{lastName}}%\'';
|
207 | if (firstName && firstName !== '') {
|
208 | query = query + ' AND P.first_name LIKE \'{{firstName}}%\'';
|
209 | }
|
210 | if (dateOfBirth && dateOfBirth !== '') {
|
211 | query = query + ' AND P.date_of_birth = \'{{dateOfBirth}}\'';
|
212 | }
|
213 |
|
214 |
|
215 |
|
216 | }
|
217 |
|
218 | var params = {
|
219 | firstName: firstName,
|
220 | lastName: lastName,
|
221 | dateOfBirth: dateOfBirth,
|
222 | nhsNumber: nhsNumber
|
223 | };
|
224 |
|
225 | var q = this;
|
226 | query = template.replace(query, params);
|
227 |
|
228 | mySQL.query(query, function(rows) {
|
229 | if(rows.error) {
|
230 | if (callback) callback(rows);
|
231 | return;
|
232 | }
|
233 | var patientDetails = [];
|
234 | var noOfPatients = 0;
|
235 | rows.forEach(function(row) {
|
236 | noOfPatients++;
|
237 | var record = formatPatientData.call(q, row);
|
238 | var patient = {
|
239 | source: 'local',
|
240 | sourceId: record.id,
|
241 | name: record.name,
|
242 | address: record.address,
|
243 | dateOfBirth: record.dateOfBirth,
|
244 | gender: record.gender,
|
245 | nhsNumber: record.nhsNumber
|
246 | };
|
247 | patientDetails.push(patient);
|
248 | });
|
249 | var results = {
|
250 | totalPatients: noOfPatients,
|
251 | patientDetails: patientDetails
|
252 | };
|
253 | if (callback) callback(results);
|
254 | });
|
255 | }
|
256 |
|
257 | function getPatientDetails(nhsNumber, callback) {
|
258 |
|
259 | var q = this;
|
260 |
|
261 | var query = 'SELECT * FROM patients P WHERE P.nhs_number = ' + nhsNumber;
|
262 | mySQL.query(query, function(patients) {
|
263 | if(patients.error) {
|
264 | callback(patients);
|
265 | return;
|
266 | }
|
267 | var results = {};
|
268 | patients.forEach(function(row) {
|
269 | results[row.nhs_number] = formatPatientData.call(q, row);
|
270 | });
|
271 | if (callback) callback(results);
|
272 | });
|
273 |
|
274 | }
|
275 |
|
276 | function getOnePatient(nhsNumber, callback) {
|
277 |
|
278 | var q = this;
|
279 |
|
280 | var getOnePatientFn = function() {
|
281 | getPatientDetails.call(q, nhsNumber, callback);
|
282 | };
|
283 |
|
284 |
|
285 | count = 0;
|
286 | getDepartments.call(this, function(error) {
|
287 | if (error) {
|
288 | callback({error: error});
|
289 | return;
|
290 | }
|
291 | q.emit('mySQLResultsReady', getOnePatientFn);
|
292 | });
|
293 |
|
294 | getGPs.call(this, function(error) {
|
295 | if (error) {
|
296 | callback({error: error});
|
297 | return;
|
298 | }
|
299 | q.emit('mySQLResultsReady', getOnePatientFn);
|
300 | });
|
301 |
|
302 | }
|
303 |
|
304 | function getAllPatients(callback) {
|
305 |
|
306 | var q = this;
|
307 | var query = 'SELECT * FROM patients';
|
308 | mySQL.query(query, function(patients) {
|
309 | if(patients.error) {
|
310 | callback(patients);
|
311 | return;
|
312 | }
|
313 | var results = {};
|
314 | patients.forEach(function(row) {
|
315 | results[row.nhs_number] = formatPatientData.call(q, row);
|
316 | });
|
317 | if (callback) callback(results);
|
318 | });
|
319 | }
|
320 |
|
321 | function getPatients(callback) {
|
322 |
|
323 | var q = this;
|
324 |
|
325 | var getAllPatientsFn = function() {
|
326 | getAllPatients.call(q, callback);
|
327 | };
|
328 |
|
329 | count = 0;
|
330 | getDepartments.call(this, function(error) {
|
331 | if (error) {
|
332 | callback({error: error});
|
333 | return;
|
334 | }
|
335 | q.emit('mySQLResultsReady', getAllPatientsFn);
|
336 | });
|
337 |
|
338 | getGPs.call(this, function(error) {
|
339 | if (error) {
|
340 | callback({error: error});
|
341 | return;
|
342 | }
|
343 | q.emit('mySQLResultsReady', getAllPatientsFn);
|
344 | });
|
345 |
|
346 | }
|
347 |
|
348 | module.exports = {
|
349 | init: function() {
|
350 | var q = this;
|
351 |
|
352 | this.on('mySQLResultsReady', function(callback) {
|
353 | count++;
|
354 | console.log('mySQLResultsReady event - count = ' + count);
|
355 | if (count === 2) {
|
356 | if (callback) callback.call(q);
|
357 | return;
|
358 | }
|
359 | });
|
360 | },
|
361 | getPatients: getPatients,
|
362 | advancedSearch: advancedSearch,
|
363 | searchByPatient: searchByPatient,
|
364 | getOnePatient: getOnePatient
|
365 | };
|