UNPKG

9.45 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 26 January 2017
28
29*/
30
31var mySQL = require('./mySQL');
32var template = require('qewd-template');
33var dateTime = require('./dateTime');
34
35var count;
36
37function 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
53function 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
69function 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 return patient;
89}
90
91function formatAddressData(address_1, address_2, address_3, address_4, address_5, postcode) {
92 var address = '';
93 var comma = ' ';
94 if (address_1) {
95 address = address_1;
96 comma = ', ';
97 }
98 if (address_2) {
99 address = address + comma + address_2;
100 comma = ', ';
101 }
102 if (address_3) {
103 address = address + comma + address_3;
104 comma = ', ';
105 }
106 if (address_4) {
107 address = address + comma + address_4;
108 comma = ', ';
109 }
110 if (address_5) {
111 address = address + comma + address_5;
112 comma = ', ';
113 }
114 if (postcode) {
115 address = address + comma + postcode;
116 comma = ', ';
117 }
118
119 return address;
120}
121
122function advancedSearch(params, callback) {
123
124 if (!params.surname || params.surname === '') {
125 if (callback) callback ({error: 'Missing or invalid surname'});
126 return;
127 }
128 if (!params.forename || params.forename === '') {
129 if (callback) callback ({error: 'Missing or invalid forename'});
130 return;
131 }
132 if (!params.dateOfBirth || params.dateOfBirth === '') {
133 if (callback) callback ({error: 'Missing or invalid dateOfBirth'});
134 return;
135 }
136 params.dateOfBirth = dateTime.toSqlPASFormat(params.dateOfBirth);
137
138 var query = 'SELECT * FROM patients P WHERE P.first_name LIKE \'{{forename}}%\' AND P.last_name LIKE \'{{surname}}%\' AND P.date_of_birth = \'{{dateOfBirth}}\'';
139 if (params.gender && params.gender !== '') {
140 query = query + ' AND P.gender EQUALS \'{{gender}}\'';
141 }
142 query = template.replace(query, params);
143 var q = this;
144
145 mySQL.query(query, function(rows) {
146 if(rows.error) {
147 if (callback) callback(rows);
148 return;
149 }
150 var results = [];
151 rows.forEach(function(row) {
152 results.push(formatPatientData.call(q, row));
153 });
154 if (callback) callback(results);
155 });
156}
157
158function searchByPatient(searchString, callback) {
159 // replace any commas with a space
160 searchString = searchString.replace(/,/g , ' ');
161 // replace multiple spaces, tabs etc with a single one
162 searchString = searchString.replace(/\s\s+/g, ' ');
163 // now split up the search string into its pieces
164 var pieces = searchString.split(' ');
165
166 if (pieces.length === 0) {
167 callback({error: 'Invalid search string'});
168 return;
169 }
170
171 var firstName;
172 var lastName;
173 var nhsNumber;
174 var dateOfBirth;
175
176 if (pieces.length === 1) {
177 lastName = pieces[0];
178 firstName = '';
179 if (Number.isInteger(parseInt(lastName))) {
180 nhsNumber = lastName;
181 lastName = '';
182 firstName = '';
183 dateOfBirth = '';
184 }
185 }
186 else {
187 firstName = pieces[0];
188 lastName = pieces[1] || '';
189 dateOfBirth = pieces[2] || '';
190 }
191
192 var query;
193 if (nhsNumber) {
194 query = 'SELECT * FROM patients P WHERE P.nhs_number = \'{{nhsNumber}}\'';
195 }
196
197 else {
198 nhsNumber = '';
199
200 if (lastName === '') {
201 callback({error: 'Last Name not defined'});
202 return;
203 }
204
205 query = 'SELECT * FROM patients P WHERE P.last_name LIKE \'{{lastName}}%\'';
206 if (firstName && firstName !== '') {
207 query = query + ' AND P.first_name LIKE \'{{firstName}}%\'';
208 }
209 if (dateOfBirth && dateOfBirth !== '') {
210 query = query + ' AND P.date_of_birth = \'{{dateOfBirth}}\'';
211 }
212 //if (params.gender && params.gender !== '') {
213 // query = query + ' AND P.gender EQUALS \'{{gender}}\'';
214 //}
215 }
216
217 var params = {
218 firstName: firstName,
219 lastName: lastName,
220 dateOfBirth: dateOfBirth,
221 nhsNumber: nhsNumber
222 };
223
224 var q = this;
225 query = template.replace(query, params);
226
227 mySQL.query(query, function(rows) {
228 if(rows.error) {
229 if (callback) callback(rows);
230 return;
231 }
232 var patientDetails = [];
233 var noOfPatients = 0;
234 rows.forEach(function(row) {
235 noOfPatients++;
236 var record = formatPatientData.call(q, row);
237 var patient = {
238 source: 'local',
239 sourceId: record.id,
240 name: record.name,
241 address: record.address,
242 dateOfBirth: record.dateOfBirth,
243 gender: record.gender,
244 nhsNumber: record.nhsNumber
245 };
246 patientDetails.push(patient);
247 });
248 var results = {
249 totalPatients: noOfPatients,
250 patientDetails: patientDetails
251 };
252 if (callback) callback(results);
253 });
254}
255
256function getAllPatients(callback) {
257
258 var q = this;
259 var query = 'SELECT * FROM patients';
260 mySQL.query(query, function(patients) {
261 if(patients.error) {
262 callback(patients);
263 return;
264 }
265 var results = {};
266 patients.forEach(function(row) {
267 results[row.nhs_number] = formatPatientData.call(q, row);
268 });
269 if (callback) callback(results);
270 });
271}
272
273function getPatients(callback) {
274
275 var q = this;
276
277 var getAllPatientsFn = function() {
278 getAllPatients.call(q, callback);
279 };
280
281 count = 0;
282 getDepartments.call(this, function(error) {
283 if (error) {
284 callback({error: error});
285 return;
286 }
287 q.emit('mySQLResultsReady', getAllPatientsFn);
288 });
289
290 getGPs.call(this, function(error) {
291 if (error) {
292 callback({error: error});
293 return;
294 }
295 q.emit('mySQLResultsReady', getAllPatientsFn);
296 });
297
298}
299
300module.exports = {
301 init: function() {
302 var q = this;
303
304 this.on('mySQLResultsReady', function(callback) {
305 count++;
306 console.log('mySQLResultsReady event - count = ' + count);
307 if (count === 2) {
308 if (callback) callback.call(q);
309 return;
310 }
311 });
312 },
313 getPatients: getPatients,
314 advancedSearch: advancedSearch,
315 searchByPatient: searchByPatient
316};