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