[de9008]: / web-app / server / src / app.js

Download this file

419 lines (306 with data), 12.4 kB

  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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const util = require('util');
const path = require('path');
const fs = require('fs');
let network = require('./fabric/network.js');
const app = express();
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(cors());
const configPath = path.join(process.cwd(), './config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configJSON);
//use these identities for general queries
const appAdmin_pat = config.appAdmin_pat;
const appAdmin_doc = config.appAdmin_doc;
//use this identity to query
//const appAdmin = config.appAdmin;
/*
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8081/RegisterPatient"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
*/
app.get('/api/printSomething', async (req, res) => {
let networkObj = await network.connectToNetwork(appAdmin_pat);
let response = await network.invoke(networkObj, true, 'printSomething', '{"Name":"Saxena","Emote":"Take the L"}');
let parsedResponse = await JSON.parse(response);
res.send(response);
});
//get Patient info, create patient object, and update state with their
app.post('/registerPatient', async (req, res) => {
console.log('req.body: ');
console.log(req.body);
let adharNo = req.body.adharNo;
let patientId = Date.now().toString();
req.body.patientId = patientId;
//check weather he is registered already or not
let networkObj = await network.connectToNetwork(appAdmin_pat);
let check = await network.invoke(networkObj, true, 'checkExist', req.body.adharNo);
let parsedCheck = await JSON.parse(check);
console.log(parsedCheck);
if (!(Object.keys(parsedCheck).length === 0 && obj.constructor === Object)){
res.send({"error":"The patient is already registered"});
}
//first create the identity for the patient and add to wallet
let response = await network.registerPatient(patientId, adharNo, req.body.name, req.body.age, req.body.phNo);
console.log('response from registerPatient: ');
console.log(response);
if (response.error) {
res.send(response.error);
} else {
console.log('req.body.adharNo');
console.log(req.body.adharNo);
networkObj = await network.connectToNetwork(patientId);
console.log('networkobj: ');
console.log(networkObj);
if (networkObj.error) {
res.send(networkObj.error);
}
console.log('network obj');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
let args = [req.body];
//connect to network and update the state
let invokeResponse = await network.invoke(networkObj, false, 'createPatient', args);
if (invokeResponse.error) {
res.send(invokeResponse.error);
} else {
console.log('after network.invoke ');
let parsedResponse = JSON.parse(invokeResponse);
parsedResponse.Success += `. Use patientId ${patientId} and password secret99 to login above.`;
res.send(parsedResponse);
}
}
});
//used as a way to login the Patient to the app and make sure they haven't voted before
app.post('/validatePatient', async (req, res) => {
console.log('req.body: ');
console.log(req.body);
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('networkobj: ');
console.log(util.inspect(networkObj));
if (networkObj.error) {
res.send(networkObj);
}
req.body = JSON.stringify(req.body);
let args = [req.body];
let invokeResponse = await network.invoke(networkObj, true, 'checkMyAsset', args);
if (invokeResponse.error) {
res.send(invokeResponse);
} else {
console.log('after network.invoke ');
let parsedResponse = await JSON.parse(invokeResponse);
// let response = `Patient with adharNo ${parsedResponse.adharNo} is logged in!.`
res.send(parsedResponse);
}
});
//get Doctor info, create doctor object, and update state with their
app.post('/registerDoctor', async (req, res) => {
console.log('req.body: ');
console.log(req.body);
let licenseId = req.body.licenseId;
let doctorId = Date.now().toString();
req.body.doctorId = doctorId;
//first create the identity for the patient and add to wallet
let response = await network.registerDoctor(doctorId, licenseId, req.body.name, req.body.age, req.body.phNo);
console.log('response from registerDoctor: ');
console.log(response);
if (response.error) {
res.send(response.error);
} else {
console.log('req.body.licenseId');
console.log(req.body.licenseId);
let networkObj = await network.connectToNetwork(doctorId);
console.log('networkobj: ');
console.log(networkObj);
if (networkObj.error) {
res.send(networkObj.error);
}
console.log('network obj');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
let args = [req.body];
//connect to network and update the state
let invokeResponse = await network.invoke(networkObj, false, 'createDoctor', args);
if (invokeResponse.error) {
res.send(invokeResponse.error);
} else {
console.log('after network.invoke ');
let parsedResponse = JSON.parse(invokeResponse);
parsedResponse.Success += `. Use doctorId ${doctorId} and password doctor99 to login above.`;
res.send(parsedResponse);
}
}
});
//used as a way to login the Doctor to the app and make sure they haven't voted before
app.post('/validateDoctor', async (req, res) => {
console.log('req.body: ');
console.log(req.body);
let networkObj = await network.connectToNetwork(req.body.doctorId);
console.log('networkobj: ');
console.log(util.inspect(networkObj));
if (networkObj.error) {
res.send(networkObj);
}
req.body = JSON.stringify(req.body);
let args = [req.body];
let invokeResponse = await network.invoke(networkObj, true, 'checkMyAsset', args);
if (invokeResponse.error) {
res.send(invokeResponse);
} else {
console.log('after network.invoke ');
let parsedResponse = await JSON.parse(invokeResponse);
// let response = `Doctor with adharNo ${parsedResponse.licenseId} is logged in!.`
res.send(parsedResponse);
}
});
app.post('/postReport', async (req, res) => {
console.log('req.body: ');
console.log(req.body);
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('networkobj: ');
console.log(util.inspect(networkObj));
if (networkObj.error) {
res.send(networkObj);
}
let reportId = Date.now().toString();
req.body.reportId = reportId;
req.body = JSON.stringify(req.body);
let args = [req.body];
let invokeResponse = await network.invoke(networkObj, false, 'createReport', args);
res.send(invokeResponse);
});
app.get('/getPatients', async (req, res) => {
let networkObj = await network.connectToNetwork(appAdmin_pat);
console.log('networkobj: ');
console.log(util.inspect(networkObj));
let response = await network.invoke(networkObj, true, 'getPatients', '');
let parsedResponse = await JSON.parse(response);
console.log(parsedResponse);
res.send(parsedResponse);
});
app.get('/getDoctors', async (req, res) => {
let networkObj = await network.connectToNetwork(appAdmin_doc);
//console.log('networkobj: ');
//console.log(util.inspect(networkObj));
let response = await network.invoke(networkObj, true, 'getDoctors', '');
let parsedResponse = await JSON.parse(response);
res.send(parsedResponse);
});
app.get('/getReports', async (req, res) => {
let networkObj = await network.connectToNetwork(appAdmin_pat);
//console.log('networkobj: ');
//console.log(util.inspect(networkObj));
let response = await network.invoke(networkObj, true, 'getReports', '');
let parsedResponse = await JSON.parse(response);
res.send(parsedResponse);
});
// app.post('/getReports', async (req, res) => {
// let networkObj = await network.connectToNetwork(appAdmin_pat);
// //console.log('networkobj: ');
// //console.log(util.inspect(networkObj));
// let response = await network.invoke(networkObj, true, 'getReports', req.body.patientId);
// let parsedResponse = await JSON.parse(response);
// res.send(parsedResponse);
// });
//This will be used to ask for access to a report of a patient
app.post('/requestAccess', async (req, res) => {
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('util inspecting');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
console.log('req.body');
console.log(req.body);
let args = [req.body];
let response = await network.invoke(networkObj, false, 'requestAccess', args);
if (response.error) {
res.send(response.error);
} else {
console.log('response: ');
console.log(response);
// let parsedResponse = await JSON.parse(response);
res.send(response);
}
});
//This will be used to grant access of a report to the doctor requesting access
app.post('/grantAccess', async (req, res) => {
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('util inspecting');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
console.log('req.body');
console.log(req.body);
let args = [req.body];
let response = await network.invoke(networkObj, false, 'grantAccess', args);
if (response.error) {
res.send(response.error);
} else {
console.log('response: ');
console.log(response);
// let parsedResponse = await JSON.parse(response);
res.send(response);
}
});
//This will be used to reject access of a report to the doctor requesting access
app.post('/rejectAccess', async (req, res) => {
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('util inspecting');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
console.log('req.body');
console.log(req.body);
let args = [req.body];
let response = await network.invoke(networkObj, false, 'rejectAccess', args);
if (response.error) {
res.send(response.error);
} else {
console.log('response: ');
console.log(response);
// let parsedResponse = await JSON.parse(response);
res.send(response);
}
});
//This will be used to reset the flags of a report of a patient
app.post('/resetAccess', async (req, res) => {
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('util inspecting');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
console.log('req.body');
console.log(req.body);
let args = [req.body];
let response = await network.invoke(networkObj, false, 'resetAccess', args);
if (response.error) {
res.send(response.error);
} else {
console.log('response: ');
console.log(response);
// let parsedResponse = await JSON.parse(response);
res.send(response);
}
});
app.post('/getReportData', async (req, res) => {
let networkObj = await network.connectToNetwork(req.body.patientId);
console.log('util inspecting');
console.log(util.inspect(networkObj));
req.body = JSON.stringify(req.body);
console.log('req.body');
console.log(req.body);
let args = [req.body];
let response = await network.invoke(networkObj, true, 'getReportData', args);
if (response.error) {
res.send(response.error);
} else {
console.log('response: ');
console.log(response);
// let parsedResponse = await JSON.parse(response);
res.send(response);
}
});
app.listen(process.env.PORT || 8080);