UNPKG

9.49 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 patients = require('./patients/patients');
32var terminology = require('./terminology');
33var search = require('./search/search');
34var user = require('./user/user');
35var initialise = require('./user/initialise');
36var token = require('./auth0/token');
37var mpv = require('./patients/mpv');
38var openEHR = require('./openEHR/openEHR');
39
40module.exports = {
41
42 init: function() {
43 if (this.initialised) return;
44 var q = this;
45
46 if (this.isFirst) {
47 console.log('************');
48 console.log('**** clearing down ripple cache Globals ********');
49 console.log('************');
50 new this.documentStore.DocumentNode('ripplePAS').delete();
51 //new this.documentStore.DocumentNode('ripplePatients').delete();
52 new this.documentStore.DocumentNode('rippleGPs').delete();
53 new this.documentStore.DocumentNode('rippleMedicalDepts').delete();
54 new this.documentStore.DocumentNode('rippleNHSNoMap').delete();
55 }
56 patients.init.call(this);
57 this.initialised = true;
58
59 },
60
61 restModule: true,
62
63 handlers: {
64
65 checkToken: function(messageObj, session, send, finished) {
66
67 /*
68 See if the incoming QEWD token from the browser's cookie is valid
69 and not yet expired
70
71 If it's still for an active QEWD session, then link the new
72 socket QEWD session with the cookie-based QEWD session
73
74 */
75
76 var token = messageObj.params.token;
77 var status = this.sessions.authenticate(token);
78 var expired = false;
79 if (status.error) expired = true;
80
81 if (!expired) {
82 // link the sessions
83 session.authenticated = true;
84 session.timeout = 3600;
85 session.updateExpiry();
86 var cookieSession = status.session;
87 cookieSession.data.$('ewd-session').$('socketSession').value = session.token;
88 session.data.$('ewd-session').$('cookieSession').value = token;
89 }
90 else {
91 // we'll use the new socket session
92 // the UI will set the token into the cookie
93 }
94
95 finished({expired: expired});
96 },
97
98 authenticate: function(messageObj, session, send, finished) {
99 session.authenticated = true;
100 session.timeout = 3600;
101 session.updateExpiry();
102 // populate the patient / PAS cache
103 mpv.getPatients.call(this, {session: session});
104
105 // set up new sessions to openEHR systems...
106 openEHR.startSessions(function(openEHRSessions) {
107 console.log('*** openEHR sessions created: ' + JSON.stringify(openEHRSessions));
108 session.data.$('openEHR').setDocument(openEHRSessions);
109 finished({ok: true});
110 });
111 },
112
113 patients: function(messageObj, finished) {
114 patients.api.call(this, messageObj, finished);
115 },
116 terminology: function(messageObj, finished) {
117 terminology.call(this, messageObj, finished);
118 },
119 search: function(messageObj, finished) {
120 search.call(this, messageObj, finished);
121 },
122 user: function(messageObj, finished) {
123 user.call(this, messageObj, finished);
124 },
125 initialise: function(messageObj, finished) {
126 initialise.call(this, messageObj, finished);
127 },
128 token: function(messageObj, finished) {
129 token.call(this, messageObj, finished);
130 },
131 'auth0-register': function(messageObj, finished) {
132 // handle callback from Auth0 - create session, store JWT data and return token to browser
133 var session = this.sessions.create('qewd-ripple', 3600);
134 session.authenticated = true;
135 session.data.$('auth0').setDocument(messageObj.params);
136 finished({token: session.token});
137 },
138
139 'webrtc:confirmSession': function(messageObj, session, send, finished) {
140 // if it got here, the user has a valid session
141
142 finished({
143 ok: true
144 });
145 },
146
147 'schemaEditor:save': function(messageObj, session, send, finished) {
148 var no = messageObj.params.no;
149 var name = messageObj.params.name;
150 var schema = messageObj.params.schema;
151
152 // compare incoming schema with current version if it exists
153
154 if (typeof no !== 'undefined') {
155 // fetch existing schema for selected option
156 var lookup = session.data.$('schemas').$(no);
157 console.log('lookup: ' + JSON.stringify(lookup));
158 var version = lookup.$('version').value;
159 var currentSchema = new this.documentStore.DocumentNode('rippleSchemas', [name, 'version', version, 'schema']).getDocument(true);
160
161 // temporarily save new schema to serialise its contents
162
163 var temp = session.data.$('temp');
164 temp.delete();
165 temp.setDocument(schema);
166 var normalisedNewSchema = temp.getDocument(true);
167 temp.delete();
168 console.log('existing schema: ' + JSON.stringify(currentSchema));
169 console.log('new schema: ' + JSON.stringify(normalisedNewSchema));
170 if (JSON.stringify(currentSchema) === JSON.stringify(normalisedNewSchema)) {
171 finished({ok: false, reason: 'No change to schema'});
172 return;
173 }
174
175 }
176
177 var example = messageObj.params.example;
178 var schemaDB = new this.documentStore.DocumentNode('rippleSchemas', [name]);
179 var versionNo = schemaDB.$('versionCounter').increment();
180 var newVersion = schemaDB.$('version').$(versionNo);
181 newVersion.$('schema').setDocument(schema);
182 newVersion.$('example').value = JSON.stringify(example); // stored as a string to prevent re-ordering on round-trips
183
184 // append to session lookup by number array
185
186 var lookup = session.data.$('schemas').getDocument(true);
187 lookup.push({
188 name: name,
189 version: versionNo
190 });
191 session.data.$('schemas').delete();
192 session.data.$('schemas').setDocument(lookup);
193
194 finished({ok: true, name: name, version: versionNo});
195 },
196
197 'schemaEditor:getSchemas': function(messageObj, session, send, finished) {
198 var schemaDB = new this.documentStore.DocumentNode('rippleSchemas');
199 var schemas = [];
200 var lookup = [];
201 var count = 0;
202 schemaDB.forEachChild(function(schemaName, schema) {
203 var versions = schema.$('version');
204 versions.forEachChild(function(versionNo) {
205 //var name = schemaName + ' (version ' + versionNo + ')';
206 schemas.push({
207 no: count,
208 name: schemaName,
209 version: versionNo
210 });
211 lookup.push({
212 name: schemaName,
213 version: versionNo
214 });
215 count++;
216 });
217 });
218 if (messageObj.params.cleardown) session.data.$('schemas').delete();
219 session.data.$('schemas').setDocument(lookup);
220 finished(schemas);
221 },
222
223 'schemaEditor:getSchema': function(messageObj, session, send, finished) {
224 var no = parseInt(messageObj.params.no);
225 var lookup = session.data.$('schemas').$(no);
226 var name = lookup.$('name').value;
227 var version = lookup.$('version').value;
228 var schemaDB = new this.documentStore.DocumentNode('rippleSchemas', [name, 'version', version]);
229 var json = schemaDB.getDocument(true);
230 finished(json);
231 },
232
233 'schemaEditor:saveForm': function(messageObj, session, send, finished) {
234 var formDB = new this.documentStore.DocumentNode('rippleRecords');
235 var index = formDB.increment();
236 formDB.$(index).setDocument(messageObj.params.json);
237 finished({ok: true});
238 }
239
240 }
241};
242