Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- web/emr/index.js
- const {enrollAdmin, getHistoryFromBlockchain} = require('../fabric');
- const {
- extractPatients, extractPatientData, readPatient,
- registerPatient, getAllPatients,
- sharePatientData, sharePatientCommentData, isValidPatient
- } = require('../patient');
- const {
- extractDoctors, extractDoctorPermission, validateDoctor,
- readDoctor, registerDoctor, addPatientDataAccessPermission,
- checkPatientDataAccessPermission, getPatientDataFile,
- getAllDoctors
- } = require('../doctor');
- const config = require('config');
- const fs = require('fs');
- const loadDb = async (mode, orgName) => {
- try {
- await enrollAdmin();
- await extractPatients(mode, orgName);
- await extractDoctors(mode, orgName);
- await extractDoctorPermission(mode);
- await extractPatientData(mode);
- } catch (err) {
- throw new Error(err);
- }
- };
- const getPatient = async (id, password) => {
- try {
- let isValid = await isValidPatient(id, password);
- if (Boolean(isValid)) {
- return await readPatient(id);
- } else {
- return {"message": "Invalid Credentials"};
- }
- } catch (err) {
- throw new Error(err);
- }
- };
- const getDoctor = async (id, password) => {
- try {
- const validateResponse = await validateDoctor(id, password);
- if (validateResponse.message === "Success") {
- return await readDoctor(id);
- } else
- return validateResponse;
- } catch (err) {
- throw new Error(err);
- }
- };
- const getPatients = async () => {
- return await getAllPatients();
- };
- const getDoctors = async () => {
- return await getAllDoctors();
- };
- const getPatientData = async (doctorID, patientID, category, filename) => {
- try {
- return await getPatientDataFile(doctorID, patientID, category, filename);
- } catch (err) {
- throw new Error(err);
- }
- };
- const getPatientComment = async (doctorID, patientID, category) => {
- console.log("Called getPatientComment with doctorID: " + doctorID + " patientID: " + patientID + " category: " + category);
- try {
- return await getPatientDataFile(doctorID, patientID, category, config.get("CommentsFile"));
- } catch (err) {
- throw new Error(err);
- }
- };
- const addPatientComment = async (patientID, doctorID, category, timeFrom, timeTo, data) => {
- console.log("Called addPatientComment with doctorID: " + doctorID + " patientID: " + patientID + " category: " + category);
- try {
- let new_content = new Date() + "<br/>" + data;
- let old_content = await getPatientComment(doctorID, patientID, category);
- console.log("old content of the file: " + old_content);
- new_content = old_content + new_content + "<br/>";
- return await sharePatientCommentData(patientID, doctorID, "write", category, timeFrom, timeTo, new_content);
- } catch (err) {
- throw new Error(err);
- }
- };
- const getPatientHistory = async (patientId) => {
- try {
- return getHistoryFromBlockchain("getPatientHistory", patientId);
- } catch (err) {
- throw new Error(err);
- }
- };
- const getData = (patientID, category, filename) => {
- //TODO: This method should get from AWS (similar to getPatientData).
- return "Not Implemented";
- };
- const postPatient = async (patientID, patientName, orgName) => {
- try {
- return await registerPatient({'id': patientID, 'name': patientName}, orgName);
- } catch (err) {
- throw new Error(err);
- }
- };
- const postDoctor = async (doctorID, doctorName, orgName) => {
- try {
- return await registerDoctor({'id': doctorID, 'name': doctorName}, orgName);
- } catch (err) {
- throw new Error(err);
- }
- };
- const addPatientData = async (patientID, doctorID, right, category, timeFrom, timeTo) => {
- try {
- return await sharePatientData(patientID, doctorID, right, category, timeFrom, timeTo);
- } catch (err) {
- throw new Error(err);
- }
- };
- const addPermission = async (patientID, doctorID, permission, category, fromTS, toTS) => {
- try {
- return await addPatientDataAccessPermission(
- patientID, doctorID, permission,
- category, fromTS, toTS
- );
- } catch (err) {
- throw new Error(err);
- }
- };
- const checkPermission = async (doctorID, patientID, category) => {
- try {
- return await checkPatientDataAccessPermission(doctorID, patientID, category);
- } catch (exc) {
- throw new Error(exc);
- }
- };
- const uploadPatientData = async (patientID, doctorID, right,
- category, date_from, date_to) => {
- try {
- return await sharePatientData(
- patientID, doctorID, right,
- category, date_from, date_to
- );
- } catch (err) {
- throw new Error(err);
- }
- };
- const getPatientFileNames = async (patientID, category) => {
- return fs.readdirSync(`data/patient_files/${patientID}/${category}`)
- };
- module.exports = {
- 'loadDb': loadDb,
- 'getPatient': getPatient,
- 'getPatients': getPatients,
- 'getDoctor': getDoctor,
- 'getDoctors': getDoctors,
- 'getPatientData': getPatientData,
- 'getPatientHistory': getPatientHistory,
- 'getData': getData,
- 'postPatient': postPatient,
- 'postDoctor': postDoctor,
- 'uploadPatientData': uploadPatientData,
- 'addPermission': addPermission,
- 'checkPermission': checkPermission,
- 'addPatientData': addPatientData,
- 'getPatientFileNames': getPatientFileNames,
- 'getPatientComment': getPatientComment,
- 'addPatientComment': addPatientComment
- };
- ----------------------------------------------------------------------------------------------------------------------------
- web/routes/emr.js
- const express = require('express');
- const emr = require('../emr');
- const router = express.Router();
- // load data from existing database
- router.get('/load_db', async (req, res) => {
- const mode = req.query.mode;
- const orgName = req.query.orgName;
- if (mode === undefined)
- res.error("Mode is undefined");
- if (orgName === undefined)
- res.error("Org Name is undefined");
- try {
- const result = await emr.loadDb(mode, orgName);
- res.send(result);
- } catch (err) {
- console.error(err);
- res.send(err);
- }
- });
- router.get('/patient/:id', async (req, res) => {
- const result = await emr.getPatient(req.params.id);
- res.json(result);
- });
- router.get('/authPatient/:cred', async (req, res) => {
- let authenticate = req.params.cred.split("&");
- const result = await emr.getPatient(authenticate[0], authenticate[1]);
- res.json(result);
- });
- router.get('/patients/', async (req, res) => {
- try {
- const result = await emr.getPatients();
- res.json(result);
- } catch (exc) {
- res.status(500).send(exc);
- }
- });
- /*
- router.get('/doctor/:id', async (req, res) => {
- const result = await emr.getDoctor(req.params.id);
- console.log(result);
- res.json(result);
- });
- */
- router.get('/doctorportal/:cred', async (req, res) => {
- const credString = req.params.cred.split('&');
- const username = credString[0];
- const password = credString[1];
- if (password.length === 0)
- res.json({"message": "Password empty"});
- else {
- const result = await emr.getDoctor(username, password);
- console.log(result);
- res.json(result);
- }
- });
- router.get('/doctors/', async (req, res) => {
- const result = await emr.getDoctors();
- res.json(result);
- });
- router.get('/patient_data/:doctorID/:patientID/:category/:filename', async (req, res) => {
- const doctorID = req.params.doctorID;
- const patientID = req.params.patientID;
- const category = req.params.category;
- const filename = req.params.filename;
- try {
- let result = await emr.getPatientData(doctorID, patientID, category, filename);
- res.end(result);
- } catch (exc) {
- res.status(500).send(exc);
- }
- });
- router.get('/data/:patientID/:category/:filename', (req, res) => {
- const patientID = req.params.patientID;
- const category = req.params.category;
- const filename = req.params.filename;
- emr.getData(patientID, category, filename, (result) =>
- res.send(result)
- )
- });
- router.post('/register_patient', async (req, res) => {
- const patientID = req.body['patient_id'];
- const patientName = req.body['patient_name'];
- const orgName = req.body['org_name'];
- const result = await emr.postPatient(patientID, patientName, orgName);
- res.send(result);
- });
- router.post('/register_doctor', async (req, res) => {
- const doctorID = req.body['doctor_id'];
- const doctorName = req.body['doctor_name'];
- const orgName = req.body['org_name'];
- const result = await emr.postDoctor(doctorID, doctorName, orgName);
- res.send(result);
- });
- router.post('/share_patient_data', async (req, res) => {
- const patientID = req.body['patient_id'];
- const doctorID = req.body['doctor_id'];
- const right = req.body['right'];
- const category = req.body['category'];
- const date_from = req.body['date_from'];
- const date_to = req.body['date_to'];
- const result = await emr.uploadPatientData(
- patientID, doctorID, right,
- category, date_from, date_to
- );
- res.send(result)
- });
- router.get('/show_history/:patientID', async (req, res) => {
- const patientID = req.params.patientID;
- let result = await emr.getPatientHistory(patientID);
- res.json(result);
- });
- router.post('/check_permission', async (req, res) => {
- const patientID = req.body['patient_id'];
- const doctorID = req.body['doctor_id'];
- const category = req.body['category'];
- const result = await emr.checkPermission(doctorID, patientID, category);
- res.send(result);
- });
- router.post('/add_patient_data', async (req, res) => {
- const patientID = req.body['patient_id'];
- const doctorID = req.body['doctor_id'];
- const dataLink = req.body['data_link'];
- const category = req.body['category'];
- const data = req.body['data'];
- const result = await emr.addPatientData(patientID, doctorID, dataLink, category, data);
- res.send(result);
- });
- router.post('/get_patient_file_names', async (req, res) => {
- const patientID = req.body['patient_id'];
- const category = req.body['category'];
- const result = await emr.getPatientFileNames(patientID, category);
- res.json(result);
- });
- router.post('/add_patient_comment', async (req, res) => {
- const patientID = req.body['patient_id'];
- const doctorID = req.body['doctor_id'];
- const category = req.body['category'];
- const date_from = req.body['date_from'];
- const date_to = req.body['date_to'];
- const comment = req.body['data'];
- const result = await emr.addPatientComment(
- patientID, doctorID, category, date_from, date_to, comment);
- res.send(result)
- });
- router.get('/patient_comment/:doctorID/:patientID/:category', async (req, res) => {
- const doctorID = req.params.doctorID;
- const patientID = req.params.patientID;
- const category = req.params.category;
- try {
- let result = await emr.getPatientComment(doctorID, patientID, category);
- res.end(result);
- } catch (exc) {
- res.status(500).send(exc);
- }
- });
- module.exports = router;
- ----------------------------------------------------------------------------------------------------------
- web/patient/index.js
- const {registerUser, addDataElement, readUser, getAllUsers, getPublicKey} = require('../fabric');
- const {extractPatientsFromMysql, extractPatientDataFromMysql, checkValidation} = require('./mysql');
- const {addPatientDataAccessPermission} = require('../doctor');
- const CryptoJS = require("crypto-js");
- const crypto = require('crypto');
- const fs = require('fs');
- const config = require('config');
- const request = require('async-request');
- const extractPatientData = async (mode) => {
- let patientData;
- switch (mode) {
- case "mysql":
- patientData = await extractPatientDataFromMysql();
- break
- }
- try {
- await Promise.all(
- patientData.map(
- (row) => addPatientDataElement(
- row.patientID,
- row.doctorID,
- row.filename,
- row.category,
- row.data
- )
- )
- );
- } catch (err) {
- console.error(err);
- }
- return patientData;
- };
- const extractPatients = async (mode, orgName) => {
- let patients;
- switch (mode) {
- case "mysql":
- patients = await extractPatientsFromMysql();
- break
- }
- try {
- await Promise.all(
- patients.map((patient) => registerPatient(patient, orgName))
- );
- } catch (err) {
- console.error(err);
- }
- return patients;
- };
- const isValidPatient = async (id, password) => {
- return await checkValidation(id, password);
- };
- const readPatient = async (patientID) => {
- try {
- return readUser("readPatient", patientID);
- } catch (e) {
- console.log('Error in readPatient', e);
- throw e;
- }
- };
- const registerPatient = async (patient, orgName) => {
- return await registerUser('createPatient', patient, orgName)
- };
- const addPatientDataElement = async (patientID, doctorID, dataLink, category, data) => {
- // TODO: data has to retrieved from dataLink instead of passing it.
- return await addDataElement(
- 'addDataItem',
- patientID,
- doctorID,
- dataLink,
- category,
- crypto.createHash('md5').update(data).digest("hex"),
- )
- };
- const getAllPatients = async () => {
- const users = await getAllUsers();
- let patients = users.filter((user) =>
- user.label.startsWith('p')
- );
- patients = await Promise.all(patients.map((patient) =>
- readPatient(patient.label)
- ));
- return patients;
- };
- //TODO: add extract data from db part
- const sharePatientData = async (patientID, doctorID, right, category, dateFrom, dateTo) => {
- let filenames = [];
- try {
- const isPermAdded = await addPatientDataAccessPermission(
- patientID, doctorID, right,
- category, dateFrom, dateTo
- );
- if (right === 'remove')
- return filenames;
- if (!isPermAdded)
- return filenames;
- const publicKey = await getPublicKey(doctorID);
- const cloudIP = config.get("CloudServer.ip");
- const cloudPort = config.get("CloudServer.port");
- const endPoint = 'add_patient_data';
- const patientFiles = fs.readdirSync(`data/patient_files/${patientID}/${category}`);
- for (let filename of patientFiles) {
- let fileData = fs.readFileSync(`data/patient_files/${patientID}/${category}/${filename}`);
- let base64data = Buffer.from(fileData).toString('base64');
- let dataToAws = CryptoJS.AES.encrypt(base64data, publicKey).toString();
- const url = `${cloudIP}:${cloudPort}/${endPoint}`;
- const options = {
- method: 'POST',
- data: {
- "doctor_id": doctorID,
- "patient_id": patientID,
- "category": category,
- "content": dataToAws,
- "filename": filename
- }
- };
- const response = await request(url, options);
- if (response.body.includes('No permission')) {
- return "No permission to access file";
- } else {
- filenames.push(filename);
- await addPatientDataElement(
- patientID,
- doctorID,
- filename,
- category,
- new Buffer(fileData, 'binary')
- )
- }
- }
- } catch (exc) {
- console.log('caught an error in catch: ', exc);
- }
- return filenames;
- };
- //TODO: add extract data from db part
- const sharePatientCommentData = async (patientID, doctorID, right, category, dateFrom, dateTo, data) => {
- let filenames = [];
- const comments_filename = config.get("CommentsFile");
- try {
- const isPermAdded = await addPatientDataAccessPermission(
- patientID, doctorID, right,
- category, dateFrom, dateTo
- );
- if (right === 'remove')
- return filenames;
- if (!isPermAdded)
- return filenames;
- const publicKey = await getPublicKey(doctorID);
- const cloudIP = config.get("CloudServer.ip");
- const cloudPort = config.get("CloudServer.port");
- const endPoint = 'add_patient_data';
- let base64data = Buffer.from(data).toString('base64');
- let dataToAws = CryptoJS.AES.encrypt(base64data, publicKey).toString();
- const url = `${cloudIP}:${cloudPort}/${endPoint}/${doctorID}/${patientID}/${category}/${comments_filename}`;
- const options = {
- method: 'POST',
- data: {
- "doctor_id": doctorID,
- "patient_id": patientID,
- "category": category,
- "content": dataToAws,
- "filename": comments_filename
- }
- };
- const response = await request(url, options);
- if (response.body.includes('No permission')) {
- return "No permission to access file";
- } else {
- filenames.push(comments_filename);
- await addPatientDataElement(
- patientID,
- doctorID,
- comments_filename,
- category,
- new Buffer(data, 'binary')
- )
- }
- } catch
- (exc) {
- console.log('caught an error in catch: ', exc);
- }
- return filenames;
- }
- ;
- module.exports = {
- 'extractPatients': extractPatients,
- 'extractPatientData': extractPatientData,
- 'readPatient': readPatient,
- 'registerPatient': registerPatient,
- 'addPatientDataElement': addPatientDataElement,
- 'getAllPatients': getAllPatients,
- 'sharePatientData': sharePatientData,
- 'isValidPatient': isValidPatient,
- 'sharePatientCommentData': sharePatientCommentData
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement