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 openEHR = require('../openEHR/openEHR');
|
32 | var headingsLib = require('../headings/headings');
|
33 |
|
34 | var headings = headingsLib.headings;
|
35 |
|
36 | function formatSummary(nhsNo, session) {
|
37 | var patient = session.data.$(['patients', nhsNo]);
|
38 |
|
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 |
|
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 |
|
102 | function 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 |
|
137 | function patientSummary(patientId, session, callback) {
|
138 |
|
139 | var patient = session.data.$(['patients', patientId]);
|
140 |
|
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 |
|
148 | var summary = formatSummary.call(this, patientId, session);
|
149 | if (callback) callback(summary);
|
150 | return;
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 | var q = this;
|
156 |
|
157 |
|
158 | openEHR.startSessions(function(openEHRSessions) {
|
159 |
|
160 | openEHR.mapNHSNo(nhsNo, openEHRSessions, function() {
|
161 |
|
162 | getPatientSummary.call(q, patientId, session, openEHRSessions, function() {
|
163 |
|
164 | openEHR.stopSessions(openEHRSessions);
|
165 | var summary = formatSummary.call(q, patientId, session);
|
166 | if (callback) callback(summary);
|
167 |
|
168 | });
|
169 | });
|
170 | });
|
171 |
|
172 | }
|
173 |
|
174 | module.exports = {
|
175 | init: function() {
|
176 |
|
177 | var q = this;
|
178 |
|
179 | this.on('headingReady', function(heading, ready, callback) {
|
180 |
|
181 | ready[heading] = true;
|
182 |
|
183 | if (ready.allergies && ready.medications && ready.problems && ready.contacts && ready.transfers) {
|
184 |
|
185 | callback.call(q);
|
186 | return;
|
187 | }
|
188 | });
|
189 |
|
190 | },
|
191 | get: patientSummary,
|
192 | };
|