UNPKG

6.65 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 30 May 2017
28
29*/
30
31var openEHR = require('../openEHR/openEHR');
32var headingsLib = require('../headings/headings');
33
34var headings = headingsLib.headings;
35
36function formatSummary(nhsNo, session) {
37 var patient = session.data.$(['patients', nhsNo]);
38 //var patient = new this.documentStore.DocumentNode('ripplePatients', [nhsNo]);
39 var patientHeadings = patient.$('headings');
40 var results = {};
41
42 if (this.userDefined.rippleUser) {
43 this.userDefined.rippleUser.summaryHeadings.forEach(function(heading) {
44 if (typeof heading === 'object') {
45 results[heading.name] = [];
46 }
47 else {
48 results[heading] = [];
49 }
50 });
51 }
52 else {
53 results = {
54 allergies: [],
55 problems: [],
56 medications: [],
57 contacts: [],
58 transfers: []
59 };
60 }
61
62 var heading;
63 var summary;
64
65 for (heading in results) {
66 for (var host in openEHR.servers) {
67 patientHeadings.$([heading, host]).forEachChild(function(index, childNode) {
68 var text = childNode.$(headings[heading].textFieldName).value;
69 if (text !== null && text !== '') {
70 summary = {
71 sourceId: childNode.$('uid').value.split('::')[0],
72 source: openEHR.servers[host].sourceName,
73 text: text
74 }
75 results[heading].push(summary);
76 }
77 });
78 }
79 }
80
81 results.name = patient.$('name').value;
82 results.gender = patient.$('gender').value;
83 results.dateOfBirth = patient.$('dateOfBirth').value;
84 //results.nhsNumber = nhsNo;
85 results.id = patient.$('id').value;
86 results.address = patient.$('address').value;
87 results.pasNumber = patient.$('pasNo').value;
88 if (results.pasNumber === '') results.pasNumber = patient.$('IHINumber').value;
89 if (this.userDefined.rippleUser.nhsNumber) {
90 results.nhsNumber = results.pasNumber;
91 }
92 else {
93 results.nhsNumber = nhsNo;
94 }
95 results.gpName = patient.$('gpName').value;
96 results.gpAddress = patient.$('gpAddress').value;
97 results.telephone = patient.$('phone').value;
98
99 return results;
100}
101
102function getPatientSummary(patientId, session, openEHRSessions, callback) {
103
104 var q = this;
105
106 var ready = {};
107 if (this.userDefined.rippleUser) {
108 this.userDefined.rippleUser.summaryHeadings.forEach(function(heading) {
109 if (typeof heading === 'object') {
110 ready[heading.name] = heading.value;
111 }
112 else {
113 ready[heading] = false;
114 }
115 });
116 }
117 else {
118 ready = {
119 allergies: false,
120 medications: false,
121 problems: false,
122 contacts: false
123 };
124 }
125
126 for (var heading in ready) {
127 if (!ready[heading]) {
128 (function(heading) {
129 headingsLib.getHeading.call(q, patientId, heading, session, openEHRSessions, function() {
130 q.emit('headingReady', heading, ready, callback);
131 });
132 }(heading));
133 }
134 }
135}
136
137function patientSummary(patientId, session, callback) {
138
139 var patient = session.data.$(['patients', patientId]);
140 //var patient = new this.documentStore.DocumentNode('ripplePatients', [patientId]);
141 var nhsNo = patientId;
142 var openEHRId = patient.$('openEHRId').value
143 if (openEHRId !== '') nhsNo = openEHRId;
144
145 var patientHeadings = patient.$(['headings', 'allergies']);
146 if (patientHeadings.exists) {
147 // we've already cached the heading data for this patient so just output from cache
148 var summary = formatSummary.call(this, patientId, session);
149 if (callback) callback(summary);
150 return;
151 }
152
153 // headings need to be fetched
154
155 var q = this;
156
157 //console.log('*** patientSummary start sessions ***');
158 openEHR.startSessions(function(openEHRSessions) {
159 //console.log('*** patientSummary mapNHSNo ***');
160 openEHR.mapNHSNo(nhsNo, openEHRSessions, function() {
161 //console.log('*** patientSummary: run getPatientSummary');
162 getPatientSummary.call(q, patientId, session, openEHRSessions, function() {
163 //console.log('*** patientSummary - stopSessions');
164 openEHR.stopSessions(openEHRSessions);
165 var summary = formatSummary.call(q, patientId, session);
166 if (callback) callback(summary);
167
168 });
169 });
170 });
171
172}
173
174module.exports = {
175 init: function() {
176
177 var q = this;
178
179 this.on('headingReady', function(heading, ready, callback) {
180 //console.log(heading + ' ready!');
181 ready[heading] = true;
182 //console.log('*** ready status is now ' + JSON.stringify(ready));
183 if (ready.allergies && ready.medications && ready.problems && ready.contacts && ready.transfers) {
184 //console.log('** ok call the callback!');
185 callback.call(q);
186 return;
187 }
188 });
189
190 },
191 get: patientSummary,
192};