src/server/api/recletter/recletter.controller.js
/**
 * create a letter in store in ipfs and store the hash in smart contract
 * @param req
 * @param res
 */
create a letter in store in ipfs and store the hash in smart contract
@param req
@param res
create a letter in store in ipfs and store the hash in smart contract @param req @param res
function createRecLetter(req, res) {
  console.log('Entering createRecLetter()..');
  let loggedInRecommenderId = req.user._id; 
  let loggedInRecommenderName = req.user.name;
  let studentId = req.body.studentId;
  let recommenderId = req.user._id;
  let schoolId = req.body.schoolId;
  console.log(`Student Id:${req.body.studentId}`);
  console.log(`loggedInRecommenderId:${loggedInRecommenderId}`);
  console.log(`loggedInRecommenderName:${loggedInRecommenderName}`);
  console.log(`schoolId:${req.body.schoolId}`);
  let newLetterId = null;
  letterOwnershipContract.deployed()
  .then(function(instance) {
    let web3 = req.app.get('web3');
    let accounts = web3.eth.accounts;
    let letterName = `Reco Letter for Student:${studentId}`;
    let letterPdfBuffer = req.body.recLetterContents;
    let letterjsonBuffer = req.body.candidateQuestions;
    let pdfFileHash = null;
    let jsonFileHash = null;
    let url1 = Buffer.from(JSON.stringify(letterjsonBuffer));
    ipfs.add(url1, function(err, result) {
      if(err) {
        console.error('Content submission error:', err);
      } else if(result && result[0] && result[0].hash) {
        console.log('JSON content successfully stored. IPFS address:', result[0].hash);
        jsonFileHash = ipfsHashToBytes32(result[0].hash);
        var url2 = Buffer.from(letterPdfBuffer, 'utf8');
        ipfs.add(url2, function(err, res1) {
          if(err) {
            console.error('Content submission error:', err);
          } else if(res1 && res1[0] && res1[0].hash) {
            console.log('PDF content successfully stored. IPFS address:', res1[0].hash);
            pdfFileHash = ipfsHashToBytes32(res1[0].hash);
            instance.createLetter(letterName, String(studentId), String(recommenderId), String(schoolId), pdfFileHash, jsonFileHash, {
              from: accounts[1],
              gas: 3000000
            }).then(function(createLetterResult) {
              newLetterId = createLetterResult.logs[0].args.letterId.c[0];
              let newRecLetter = new RecLetter({
                letterId: newLetterId,
                studentId: req.body.studentId,
                recommenderId: loggedInRecommenderId,
                schoolId: req.body.schoolId,
                studentName: req.body.studentName,
                recommenderName: loggedInRecommenderName,
                programName: req.body.programName,
                submissionDate: new Date()
              });
              newRecLetter.save(function(err, recLetter) {
                if(err) {
                  res.json(err);
                } else {
                  res.json(recLetter);
                  console.log('Rec Letter data saved in mongoose');
                  instance.changeRequestStatus(newLetterId, 1, {
                    from: accounts[0],
                    gas: 3000000
                  }).then(function(reqStatusResult) {
                    console.log('reqStatusResult', reqStatusResult);
                    RecLetterRequest.find({studentId: req.body.studentId, schoolId: req.body.schoolId, recommenderId: loggedInRecommenderId, requestId:req.body.letterId},
                    function(err, recLetterRequests) {
                      if(err) {
                        console.log('Error', err);
                      } else {
                        recLetterRequests.forEach(element => {
                          element.letterStatus = 'Created';
                          element.save(function(err, changedLetterRequest) {
                            if(err) {
                              console.log(err);
                            } else {
                              console.log('Changed Request saved in mongoDB', changedLetterRequest);
                            }
                          });
                        });
                      }
                    });
                  })
                  .catch(function(error) {
                    console.log('Error in call:', error);
                  });
                }
              });
            });
          }
        });
      } else {
        console.log(result);
        console.log(result[0]);
        console.log(result[0].Hash);
        console.error('Unresolved content submission error');
      }
    });
  });
}
function createRecLetter ( req , res ) { console . log ( 'Entering createRecLetter()..' ) ; let loggedInRecommenderId = req . user . _id ; let loggedInRecommenderName = req . user . name ; let studentId = req . body . studentId ; let recommenderId = req . user . _id ; let schoolId = req . body . schoolId ; console . log ( ` ${ req . body . studentId } ` ) ; console . log ( ` ${ loggedInRecommenderId } ` ) ; console . log ( ` ${ loggedInRecommenderName } ` ) ; console . log ( ` ${ req . body . schoolId } ` ) ; let newLetterId = null ; letterOwnershipContract . deployed ( ) . then ( function ( instance ) { let web3 = req . app . get ( 'web3' ) ; let accounts = web3 . eth . accounts ; let letterName = ` ${ studentId } ` ; let letterPdfBuffer = req . body . recLetterContents ; let letterjsonBuffer = req . body . candidateQuestions ; let pdfFileHash = null ; let jsonFileHash = null ; let url1 = Buffer . from ( JSON . stringify ( letterjsonBuffer ) ) ; ipfs . add ( url1 , function ( err , result ) { if ( err ) { console . error ( 'Content submission error:' , err ) ; } else if ( result && result [ 0 ] && result [ 0 ] . hash ) { console . log ( 'JSON content successfully stored. IPFS address:' , result [ 0 ] . hash ) ; jsonFileHash = ipfsHashToBytes32 ( result [ 0 ] . hash ) ; var url2 = Buffer . from ( letterPdfBuffer , 'utf8' ) ; ipfs . add ( url2 , function ( err , res1 ) { if ( err ) { console . error ( 'Content submission error:' , err ) ; } else if ( res1 && res1 [ 0 ] && res1 [ 0 ] . hash ) { console . log ( 'PDF content successfully stored. IPFS address:' , res1 [ 0 ] . hash ) ; pdfFileHash = ipfsHashToBytes32 ( res1 [ 0 ] . hash ) ; instance . createLetter ( letterName , String ( studentId ) , String ( recommenderId ) , String ( schoolId ) , pdfFileHash , jsonFileHash , { from : accounts [ 1 ] , gas : 3000000 } ) . then ( function ( createLetterResult ) { newLetterId = createLetterResult . logs [ 0 ] . args . letterId . c [ 0 ] ; let newRecLetter = new RecLetter ( { letterId : newLetterId , studentId : req . body . studentId , recommenderId : loggedInRecommenderId , schoolId : req . body . schoolId , studentName : req . body . studentName , recommenderName : loggedInRecommenderName , programName : req . body . programName , submissionDate : new Date ( ) } ) ; newRecLetter . save ( function ( err , recLetter ) { if ( err ) { res . json ( err ) ; } else { res . json ( recLetter ) ; console . log ( 'Rec Letter data saved in mongoose' ) ; instance . changeRequestStatus ( newLetterId , 1 , { from : accounts [ 0 ] , gas : 3000000 } ) . then ( function ( reqStatusResult ) { console . log ( 'reqStatusResult' , reqStatusResult ) ; RecLetterRequest . find ( { studentId : req . body . studentId , schoolId : req . body . schoolId , recommenderId : loggedInRecommenderId , requestId : req . body . letterId } , function ( err , recLetterRequests ) { if ( err ) { console . log ( 'Error' , err ) ; } else { recLetterRequests . forEach ( element => { element . letterStatus = 'Created' ; element . save ( function ( err , changedLetterRequest ) { if ( err ) { console . log ( err ) ; } else { console . log ( 'Changed Request saved in mongoDB' , changedLetterRequest ) ; } } ) ; } ) ; } } ) ; } ) . catch ( function ( error ) { console . log ( 'Error in call:' , error ) ; } ) ; } } ) ; } ) ; } } ) ; } else { console . log ( result ) ; console . log ( result [ 0 ] ) ; console . log ( result [ 0 ] . Hash ) ; console . error ( 'Unresolved content submission error' ) ; } } ) ; } ) ; }
create a letter in store in ipfs and store the hash in smart contract
 @param req
 @param res
create a letter in store in ipfs and store the hash in smart contract @param req @param res
//get logged in user id //These Ids need to be passed to contract, currently they are undefined //This is the new letter Id that's created in the blockchain. MongoDB needs to hold this ID to make the subsequent operation //on this letter for viewing, deleting etc. //Hash of IPFS files in bytes32 format to pass to contract method //Add JSON file in IPFS //Add pdf file in IPFS //Get the letterId //Change the corresponding letter request as completed: // console.log(instance); //res.json(err); //else block of pdf ipfs save