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 patients = require('./patients/patients');
|
32 | var terminology = require('./terminology');
|
33 | var search = require('./search/search');
|
34 | var user = require('./user/user');
|
35 | var initialise = require('./user/initialise');
|
36 | var token = require('./auth0/token');
|
37 | var mpv = require('./patients/mpv');
|
38 | var openEHR = require('./openEHR/openEHR');
|
39 | var contentStoreRest = require('qewd-content-store').rest;
|
40 | var createSession = require('./sessions/create');
|
41 |
|
42 | var documents = require('./documents/docs');
|
43 |
|
44 | module.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 |
|
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 |
|
63 | this.initialised = true;
|
64 |
|
65 | },
|
66 |
|
67 | restModule: true,
|
68 |
|
69 | handlers: {
|
70 |
|
71 | checkToken: function(messageObj, session, send, finished) {
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
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 |
|
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 |
|
98 |
|
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 |
|
109 | mpv.getPatients.call(this, {session: session});
|
110 |
|
111 |
|
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 |
|
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 |
|
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 |
|
167 |
|
168 | if (typeof no !== 'undefined') {
|
169 |
|
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 |
|
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);
|
197 |
|
198 |
|
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 |
|
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 |
|