Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.62 KB | None | 0 0
  1. web/emr/index.js
  2.  
  3. const {enrollAdmin, getHistoryFromBlockchain} = require('../fabric');
  4. const {
  5. extractPatients, extractPatientData, readPatient,
  6. registerPatient, getAllPatients,
  7. sharePatientData, sharePatientCommentData, isValidPatient
  8. } = require('../patient');
  9. const {
  10. extractDoctors, extractDoctorPermission, validateDoctor,
  11. readDoctor, registerDoctor, addPatientDataAccessPermission,
  12. checkPatientDataAccessPermission, getPatientDataFile,
  13. getAllDoctors
  14. } = require('../doctor');
  15. const config = require('config');
  16. const fs = require('fs');
  17.  
  18. const loadDb = async (mode, orgName) => {
  19. try {
  20. await enrollAdmin();
  21. await extractPatients(mode, orgName);
  22. await extractDoctors(mode, orgName);
  23. await extractDoctorPermission(mode);
  24. await extractPatientData(mode);
  25. } catch (err) {
  26. throw new Error(err);
  27. }
  28. };
  29.  
  30. const getPatient = async (id, password) => {
  31. try {
  32.  
  33. let isValid = await isValidPatient(id, password);
  34. if (Boolean(isValid)) {
  35. return await readPatient(id);
  36. } else {
  37. return {"message": "Invalid Credentials"};
  38. }
  39. } catch (err) {
  40. throw new Error(err);
  41. }
  42. };
  43.  
  44. const getDoctor = async (id, password) => {
  45.  
  46. try {
  47. const validateResponse = await validateDoctor(id, password);
  48. if (validateResponse.message === "Success") {
  49. return await readDoctor(id);
  50. } else
  51. return validateResponse;
  52. } catch (err) {
  53. throw new Error(err);
  54. }
  55. };
  56.  
  57. const getPatients = async () => {
  58. return await getAllPatients();
  59. };
  60.  
  61. const getDoctors = async () => {
  62. return await getAllDoctors();
  63. };
  64.  
  65. const getPatientData = async (doctorID, patientID, category, filename) => {
  66. try {
  67. return await getPatientDataFile(doctorID, patientID, category, filename);
  68. } catch (err) {
  69. throw new Error(err);
  70. }
  71. };
  72.  
  73. const getPatientComment = async (doctorID, patientID, category) => {
  74. console.log("Called getPatientComment with doctorID: " + doctorID + " patientID: " + patientID + " category: " + category);
  75. try {
  76. return await getPatientDataFile(doctorID, patientID, category, config.get("CommentsFile"));
  77. } catch (err) {
  78. throw new Error(err);
  79. }
  80. };
  81.  
  82. const addPatientComment = async (patientID, doctorID, category, timeFrom, timeTo, data) => {
  83. console.log("Called addPatientComment with doctorID: " + doctorID + " patientID: " + patientID + " category: " + category);
  84. try {
  85. let new_content = new Date() + "<br/>" + data;
  86. let old_content = await getPatientComment(doctorID, patientID, category);
  87. console.log("old content of the file: " + old_content);
  88. new_content = old_content + new_content + "<br/>";
  89. return await sharePatientCommentData(patientID, doctorID, "write", category, timeFrom, timeTo, new_content);
  90. } catch (err) {
  91. throw new Error(err);
  92. }
  93. };
  94.  
  95. const getPatientHistory = async (patientId) => {
  96. try {
  97. return getHistoryFromBlockchain("getPatientHistory", patientId);
  98. } catch (err) {
  99. throw new Error(err);
  100. }
  101.  
  102. };
  103.  
  104. const getData = (patientID, category, filename) => {
  105. //TODO: This method should get from AWS (similar to getPatientData).
  106. return "Not Implemented";
  107. };
  108.  
  109. const postPatient = async (patientID, patientName, orgName) => {
  110. try {
  111. return await registerPatient({'id': patientID, 'name': patientName}, orgName);
  112. } catch (err) {
  113. throw new Error(err);
  114. }
  115. };
  116.  
  117. const postDoctor = async (doctorID, doctorName, orgName) => {
  118. try {
  119. return await registerDoctor({'id': doctorID, 'name': doctorName}, orgName);
  120. } catch (err) {
  121. throw new Error(err);
  122. }
  123. };
  124.  
  125. const addPatientData = async (patientID, doctorID, right, category, timeFrom, timeTo) => {
  126. try {
  127. return await sharePatientData(patientID, doctorID, right, category, timeFrom, timeTo);
  128. } catch (err) {
  129. throw new Error(err);
  130. }
  131. };
  132.  
  133. const addPermission = async (patientID, doctorID, permission, category, fromTS, toTS) => {
  134. try {
  135. return await addPatientDataAccessPermission(
  136. patientID, doctorID, permission,
  137. category, fromTS, toTS
  138. );
  139. } catch (err) {
  140. throw new Error(err);
  141. }
  142. };
  143.  
  144. const checkPermission = async (doctorID, patientID, category) => {
  145. try {
  146. return await checkPatientDataAccessPermission(doctorID, patientID, category);
  147. } catch (exc) {
  148. throw new Error(exc);
  149. }
  150. };
  151.  
  152. const uploadPatientData = async (patientID, doctorID, right,
  153. category, date_from, date_to) => {
  154. try {
  155. return await sharePatientData(
  156. patientID, doctorID, right,
  157. category, date_from, date_to
  158. );
  159. } catch (err) {
  160. throw new Error(err);
  161. }
  162. };
  163.  
  164. const getPatientFileNames = async (patientID, category) => {
  165. return fs.readdirSync(`data/patient_files/${patientID}/${category}`)
  166. };
  167.  
  168. module.exports = {
  169. 'loadDb': loadDb,
  170. 'getPatient': getPatient,
  171. 'getPatients': getPatients,
  172. 'getDoctor': getDoctor,
  173. 'getDoctors': getDoctors,
  174. 'getPatientData': getPatientData,
  175. 'getPatientHistory': getPatientHistory,
  176. 'getData': getData,
  177. 'postPatient': postPatient,
  178. 'postDoctor': postDoctor,
  179. 'uploadPatientData': uploadPatientData,
  180. 'addPermission': addPermission,
  181. 'checkPermission': checkPermission,
  182. 'addPatientData': addPatientData,
  183. 'getPatientFileNames': getPatientFileNames,
  184. 'getPatientComment': getPatientComment,
  185. 'addPatientComment': addPatientComment
  186. };
  187.  
  188. ----------------------------------------------------------------------------------------------------------------------------
  189. web/routes/emr.js
  190. const express = require('express');
  191. const emr = require('../emr');
  192. const router = express.Router();
  193.  
  194. // load data from existing database
  195. router.get('/load_db', async (req, res) => {
  196. const mode = req.query.mode;
  197. const orgName = req.query.orgName;
  198.  
  199. if (mode === undefined)
  200. res.error("Mode is undefined");
  201.  
  202. if (orgName === undefined)
  203. res.error("Org Name is undefined");
  204.  
  205. try {
  206. const result = await emr.loadDb(mode, orgName);
  207. res.send(result);
  208. } catch (err) {
  209. console.error(err);
  210. res.send(err);
  211. }
  212. });
  213.  
  214. router.get('/patient/:id', async (req, res) => {
  215. const result = await emr.getPatient(req.params.id);
  216. res.json(result);
  217. });
  218.  
  219. router.get('/authPatient/:cred', async (req, res) => {
  220. let authenticate = req.params.cred.split("&");
  221. const result = await emr.getPatient(authenticate[0], authenticate[1]);
  222. res.json(result);
  223. });
  224.  
  225. router.get('/patients/', async (req, res) => {
  226. try {
  227. const result = await emr.getPatients();
  228. res.json(result);
  229. } catch (exc) {
  230. res.status(500).send(exc);
  231. }
  232. });
  233.  
  234. /*
  235. router.get('/doctor/:id', async (req, res) => {
  236. const result = await emr.getDoctor(req.params.id);
  237. console.log(result);
  238. res.json(result);
  239. });
  240. */
  241.  
  242. router.get('/doctorportal/:cred', async (req, res) => {
  243.  
  244. const credString = req.params.cred.split('&');
  245. const username = credString[0];
  246. const password = credString[1];
  247. if (password.length === 0)
  248. res.json({"message": "Password empty"});
  249. else {
  250. const result = await emr.getDoctor(username, password);
  251. console.log(result);
  252. res.json(result);
  253. }
  254. });
  255.  
  256. router.get('/doctors/', async (req, res) => {
  257. const result = await emr.getDoctors();
  258. res.json(result);
  259. });
  260.  
  261. router.get('/patient_data/:doctorID/:patientID/:category/:filename', async (req, res) => {
  262. const doctorID = req.params.doctorID;
  263. const patientID = req.params.patientID;
  264. const category = req.params.category;
  265. const filename = req.params.filename;
  266. try {
  267. let result = await emr.getPatientData(doctorID, patientID, category, filename);
  268. res.end(result);
  269. } catch (exc) {
  270. res.status(500).send(exc);
  271. }
  272. });
  273.  
  274. router.get('/data/:patientID/:category/:filename', (req, res) => {
  275. const patientID = req.params.patientID;
  276. const category = req.params.category;
  277. const filename = req.params.filename;
  278.  
  279. emr.getData(patientID, category, filename, (result) =>
  280. res.send(result)
  281. )
  282. });
  283.  
  284. router.post('/register_patient', async (req, res) => {
  285. const patientID = req.body['patient_id'];
  286. const patientName = req.body['patient_name'];
  287. const orgName = req.body['org_name'];
  288.  
  289. const result = await emr.postPatient(patientID, patientName, orgName);
  290. res.send(result);
  291. });
  292.  
  293. router.post('/register_doctor', async (req, res) => {
  294. const doctorID = req.body['doctor_id'];
  295. const doctorName = req.body['doctor_name'];
  296. const orgName = req.body['org_name'];
  297.  
  298. const result = await emr.postDoctor(doctorID, doctorName, orgName);
  299. res.send(result);
  300. });
  301.  
  302. router.post('/share_patient_data', async (req, res) => {
  303. const patientID = req.body['patient_id'];
  304. const doctorID = req.body['doctor_id'];
  305. const right = req.body['right'];
  306. const category = req.body['category'];
  307. const date_from = req.body['date_from'];
  308. const date_to = req.body['date_to'];
  309.  
  310. const result = await emr.uploadPatientData(
  311. patientID, doctorID, right,
  312. category, date_from, date_to
  313. );
  314.  
  315. res.send(result)
  316. });
  317.  
  318. router.get('/show_history/:patientID', async (req, res) => {
  319.  
  320. const patientID = req.params.patientID;
  321. let result = await emr.getPatientHistory(patientID);
  322. res.json(result);
  323. });
  324.  
  325. router.post('/check_permission', async (req, res) => {
  326. const patientID = req.body['patient_id'];
  327. const doctorID = req.body['doctor_id'];
  328. const category = req.body['category'];
  329.  
  330. const result = await emr.checkPermission(doctorID, patientID, category);
  331. res.send(result);
  332. });
  333.  
  334. router.post('/add_patient_data', async (req, res) => {
  335. const patientID = req.body['patient_id'];
  336. const doctorID = req.body['doctor_id'];
  337. const dataLink = req.body['data_link'];
  338. const category = req.body['category'];
  339. const data = req.body['data'];
  340.  
  341. const result = await emr.addPatientData(patientID, doctorID, dataLink, category, data);
  342. res.send(result);
  343. });
  344.  
  345. router.post('/get_patient_file_names', async (req, res) => {
  346. const patientID = req.body['patient_id'];
  347. const category = req.body['category'];
  348.  
  349. const result = await emr.getPatientFileNames(patientID, category);
  350. res.json(result);
  351. });
  352.  
  353. router.post('/add_patient_comment', async (req, res) => {
  354. const patientID = req.body['patient_id'];
  355. const doctorID = req.body['doctor_id'];
  356. const category = req.body['category'];
  357. const date_from = req.body['date_from'];
  358. const date_to = req.body['date_to'];
  359. const comment = req.body['data'];
  360.  
  361. const result = await emr.addPatientComment(
  362. patientID, doctorID, category, date_from, date_to, comment);
  363. res.send(result)
  364. });
  365.  
  366. router.get('/patient_comment/:doctorID/:patientID/:category', async (req, res) => {
  367. const doctorID = req.params.doctorID;
  368. const patientID = req.params.patientID;
  369. const category = req.params.category;
  370. try {
  371. let result = await emr.getPatientComment(doctorID, patientID, category);
  372. res.end(result);
  373. } catch (exc) {
  374. res.status(500).send(exc);
  375. }
  376. });
  377.  
  378. module.exports = router;
  379. ----------------------------------------------------------------------------------------------------------
  380. web/patient/index.js
  381.  
  382. const {registerUser, addDataElement, readUser, getAllUsers, getPublicKey} = require('../fabric');
  383. const {extractPatientsFromMysql, extractPatientDataFromMysql, checkValidation} = require('./mysql');
  384. const {addPatientDataAccessPermission} = require('../doctor');
  385. const CryptoJS = require("crypto-js");
  386. const crypto = require('crypto');
  387. const fs = require('fs');
  388. const config = require('config');
  389. const request = require('async-request');
  390.  
  391. const extractPatientData = async (mode) => {
  392. let patientData;
  393.  
  394. switch (mode) {
  395. case "mysql":
  396. patientData = await extractPatientDataFromMysql();
  397. break
  398. }
  399.  
  400. try {
  401. await Promise.all(
  402. patientData.map(
  403. (row) => addPatientDataElement(
  404. row.patientID,
  405. row.doctorID,
  406. row.filename,
  407. row.category,
  408. row.data
  409. )
  410. )
  411. );
  412. } catch (err) {
  413. console.error(err);
  414. }
  415.  
  416. return patientData;
  417. };
  418.  
  419. const extractPatients = async (mode, orgName) => {
  420. let patients;
  421.  
  422. switch (mode) {
  423. case "mysql":
  424. patients = await extractPatientsFromMysql();
  425. break
  426. }
  427.  
  428. try {
  429. await Promise.all(
  430. patients.map((patient) => registerPatient(patient, orgName))
  431. );
  432. } catch (err) {
  433. console.error(err);
  434. }
  435.  
  436. return patients;
  437. };
  438.  
  439. const isValidPatient = async (id, password) => {
  440. return await checkValidation(id, password);
  441. };
  442.  
  443. const readPatient = async (patientID) => {
  444. try {
  445. return readUser("readPatient", patientID);
  446. } catch (e) {
  447. console.log('Error in readPatient', e);
  448. throw e;
  449. }
  450. };
  451.  
  452. const registerPatient = async (patient, orgName) => {
  453. return await registerUser('createPatient', patient, orgName)
  454. };
  455.  
  456. const addPatientDataElement = async (patientID, doctorID, dataLink, category, data) => {
  457. // TODO: data has to retrieved from dataLink instead of passing it.
  458. return await addDataElement(
  459. 'addDataItem',
  460. patientID,
  461. doctorID,
  462. dataLink,
  463. category,
  464. crypto.createHash('md5').update(data).digest("hex"),
  465. )
  466. };
  467.  
  468. const getAllPatients = async () => {
  469. const users = await getAllUsers();
  470.  
  471. let patients = users.filter((user) =>
  472. user.label.startsWith('p')
  473. );
  474.  
  475. patients = await Promise.all(patients.map((patient) =>
  476. readPatient(patient.label)
  477. ));
  478.  
  479. return patients;
  480. };
  481.  
  482. //TODO: add extract data from db part
  483. const sharePatientData = async (patientID, doctorID, right, category, dateFrom, dateTo) => {
  484. let filenames = [];
  485.  
  486. try {
  487. const isPermAdded = await addPatientDataAccessPermission(
  488. patientID, doctorID, right,
  489. category, dateFrom, dateTo
  490. );
  491.  
  492. if (right === 'remove')
  493. return filenames;
  494.  
  495. if (!isPermAdded)
  496. return filenames;
  497.  
  498. const publicKey = await getPublicKey(doctorID);
  499. const cloudIP = config.get("CloudServer.ip");
  500. const cloudPort = config.get("CloudServer.port");
  501. const endPoint = 'add_patient_data';
  502.  
  503. const patientFiles = fs.readdirSync(`data/patient_files/${patientID}/${category}`);
  504.  
  505. for (let filename of patientFiles) {
  506. let fileData = fs.readFileSync(`data/patient_files/${patientID}/${category}/${filename}`);
  507.  
  508. let base64data = Buffer.from(fileData).toString('base64');
  509. let dataToAws = CryptoJS.AES.encrypt(base64data, publicKey).toString();
  510.  
  511. const url = `${cloudIP}:${cloudPort}/${endPoint}`;
  512. const options = {
  513. method: 'POST',
  514. data: {
  515. "doctor_id": doctorID,
  516. "patient_id": patientID,
  517. "category": category,
  518. "content": dataToAws,
  519. "filename": filename
  520. }
  521. };
  522.  
  523. const response = await request(url, options);
  524. if (response.body.includes('No permission')) {
  525. return "No permission to access file";
  526. } else {
  527. filenames.push(filename);
  528. await addPatientDataElement(
  529. patientID,
  530. doctorID,
  531. filename,
  532. category,
  533. new Buffer(fileData, 'binary')
  534. )
  535. }
  536. }
  537. } catch (exc) {
  538. console.log('caught an error in catch: ', exc);
  539. }
  540. return filenames;
  541. };
  542.  
  543. //TODO: add extract data from db part
  544. const sharePatientCommentData = async (patientID, doctorID, right, category, dateFrom, dateTo, data) => {
  545. let filenames = [];
  546. const comments_filename = config.get("CommentsFile");
  547. try {
  548. const isPermAdded = await addPatientDataAccessPermission(
  549. patientID, doctorID, right,
  550. category, dateFrom, dateTo
  551. );
  552.  
  553. if (right === 'remove')
  554. return filenames;
  555.  
  556. if (!isPermAdded)
  557. return filenames;
  558.  
  559. const publicKey = await getPublicKey(doctorID);
  560. const cloudIP = config.get("CloudServer.ip");
  561. const cloudPort = config.get("CloudServer.port");
  562. const endPoint = 'add_patient_data';
  563.  
  564. let base64data = Buffer.from(data).toString('base64');
  565. let dataToAws = CryptoJS.AES.encrypt(base64data, publicKey).toString();
  566. const url = `${cloudIP}:${cloudPort}/${endPoint}/${doctorID}/${patientID}/${category}/${comments_filename}`;
  567. const options = {
  568. method: 'POST',
  569. data: {
  570. "doctor_id": doctorID,
  571. "patient_id": patientID,
  572. "category": category,
  573. "content": dataToAws,
  574. "filename": comments_filename
  575. }
  576. };
  577.  
  578. const response = await request(url, options);
  579. if (response.body.includes('No permission')) {
  580. return "No permission to access file";
  581. } else {
  582. filenames.push(comments_filename);
  583. await addPatientDataElement(
  584. patientID,
  585. doctorID,
  586. comments_filename,
  587. category,
  588. new Buffer(data, 'binary')
  589. )
  590. }
  591. } catch
  592. (exc) {
  593. console.log('caught an error in catch: ', exc);
  594. }
  595. return filenames;
  596. }
  597. ;
  598.  
  599. module.exports = {
  600. 'extractPatients': extractPatients,
  601. 'extractPatientData': extractPatientData,
  602. 'readPatient': readPatient,
  603. 'registerPatient': registerPatient,
  604. 'addPatientDataElement': addPatientDataElement,
  605. 'getAllPatients': getAllPatients,
  606. 'sharePatientData': sharePatientData,
  607. 'isValidPatient': isValidPatient,
  608. 'sharePatientCommentData': sharePatientCommentData
  609. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement