UNPKG

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