Skip to content

Commit

Permalink
Send prescription to front in consumePrescription, deleted some conso…
Browse files Browse the repository at this point in the history
…le.log and downloadPrescription Complete
  • Loading branch information
Alex99y committed Aug 14, 2019
1 parent e28cf43 commit 6f31f5b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
47 changes: 41 additions & 6 deletions api/controllers/services/secure-rec/prescription/download.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const ipfs = require('../../../../../ipfs-api/ipfs_api');
const fileType = require('file-type');
const fs = require('fs');
const sleep = require('sleep');


module.exports = {

friendlyName: 'Secure Rec download prescription',
Expand All @@ -14,6 +20,14 @@ module.exports = {
invalid: {
responseType: 'bad-combo',
description: 'Los parámetros proporcionados son inválidos.'
},
write: {
responseType: 'internal-error',
description: 'Error escribiendo el archivo'
},
ipfs: {
responseType: 'ipfs-error2',
description: 'Error uploading the image'
}
},

Expand All @@ -22,12 +36,33 @@ module.exports = {
if ( inputs.ipfsHash === undefined )
return exits.invalid();

return exits.success({
success: true,
fileName: 'Name',
file: 'url'
});

// Download file from IPFS
let file;
try {
file = await ipfs.asyncDownload(inputs.ipfsHash);
let type = fileType(file);
if (!type) {
type = {
ext: 'unknownType',
mime: 'file/unknownType'
}
}
const path = 'assets/images/prescriptions/' + inputs.ipfsHash + '.' + type.ext;
const image = 'images/prescriptions/' + inputs.ipfsHash + '.' + type.ext;
fs.writeFile(path, file, 'binary', (err)=>{
if (err)
return exits.write();
sleep.sleep(2);
const datetime = new Date();
return exits.success({
success: true,
fileName: datetime + '.' + type.ext,
file: '../../../' + image
});
});
}catch(err){
return exits.ipfs();
}
}

};
44 changes: 41 additions & 3 deletions api/controllers/services/secure-rec/prescription/edit-provider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fabric = require('../../../../../fabric-api/chaincodeTransactions');
const ursa = require('../../../../../crypto/keys');
const ipfs = require('../../../../../ipfs-api/ipfs_api');

module.exports = {

Expand All @@ -18,13 +19,21 @@ module.exports = {
responseType: 'bad-combo',
description: 'Los parámetros proporcionados son inválidos.'
},
ursa: {
responseType: 'ursa-error',
description: 'Error en la clave enviada'
},
fabric: {
responseType: 'fabric-error',
description: 'Error Hyperledger Fabric'
},
internalError: {
responseType: 'internal-error',
description: 'Error changing password'
},
ipfs: {
responseType: 'ipfs-error2',
description: 'Error uploading the image'
}
},

Expand All @@ -33,17 +42,46 @@ module.exports = {
if ( inputs.hash === undefined )
return exits.invalid();

let owner = this.req.session.auth, pubKey = await ursa.getPublicKey(owner.privateKey), result, Args;
let owner = this.req.session.auth, pubKey = await ursa.getPublicKey(owner.privateKey), result, Args, prescription;
try{
Args = [pubKey, inputs.hash];
result = await fabric.invokeTransaction('mychannel','Org1MSP','secureRec', 'consumePrescription', Args);
// result = await fabric.invokeTransaction('mychannel','Org1MSP','secureRec', 'consumePrescription', Args);
// Get prescription
prescription = await fabric.queryChaincode('mychannel','Org1MSP','secureRec', 'queryPrescription', [inputs.hash]);
prescription = JSON.parse(prescription[0].toString());
try {
let patient = await User.findOne({publicKey: prescription.patient});
prescription.patient = patient.emailAddress;
prescription.doctor = (await User.findOne({publicKey: prescription.doctor})).emailAddress;
if (prescription.insurance.length > 0 )
prescription.insurance = (await User.findOne({publicKey: prescription.insurance})).emailAddress;
prescription.provider = owner.emailAddress;
try {
let ipfsHash = await ursa.decryptIpfsHash(patient.privateKey, prescription.hash);
// prescription.ipfsHash
try {
let data = await ipfs.asyncDownload(ipfsHash);
data = JSON.parse(data.toString());
prescription.ipfsHash = data.hashFile;
prescription.description = data.description;
}catch(err){
return exits.ipfs();
}
}catch(err){
return exits.ursa();
}
}catch(err){
return exits.internalError();
}
}catch(err){
console.log(err);
return exits.fabric();
}
return exits.success({
success: true,
message: 'Edit prescription',
hash: result.hash
//hash: result.hash,
prescription: prescription
});

}
Expand Down
3 changes: 1 addition & 2 deletions api/controllers/services/secure-rec/prescription/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ module.exports = {
}catch(err){ return exits.internalError(); }
// Get description and ipfs hash
try{
console.log(result.prescriptions[i].hash)
hash = await ursa.decryptIpfsHash(patient.privateKey, result.prescriptions[i].hash);
data = await ipfs.asyncDownload(hash);
data = JSON.parse(data.toString());
}catch(err) { console.log(err); }
}catch(err) { return exits.ipfs(); }
// Prepare the prescription
let provider = "", insurance = "", doctor = await User.findOne({ publicKey: result.prescriptions[i].doctor });
if ( result.prescriptions[i].provider.length > 0 ){
Expand Down

0 comments on commit 6f31f5b

Please sign in to comment.