Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  Parse.Cloud.afterSave("Feedback", (request) => {  
  3.   const grade = request.object.get("grade").toString() ;
  4.   const message = request.object.get("message");
  5.  
  6.   // request.log.info("TEST_LOG_GRADE: "+ grade + "TEST_LOG_MESSAGE: " + message);
  7.  
  8.   const userObj = request.object.get("user");
  9.   const user = JSON.parse(JSON.stringify(userObj))
  10.   const userID = user["objectId"];
  11.  
  12.   request.log.info("TEST_LOG: "+userID);
  13.  
  14.   return getUser(userID).then(function(user){
  15.     if(user){
  16.       sendEmailOnFeedback(user.get("email"), grade, message)
  17.     } else {
  18.         console.log("User with objectId: " + userID + " was not found");
  19.     }
  20.   }, function(error){
  21.      console.error("Got an error " + error.code + " : " + error.message);
  22.   });
  23.  
  24. });
  25.  
  26. function getUser(userId) {
  27.   var promise = new Parse.Promise();
  28.  
  29.   var query = new Parse.Query(Parse.User);
  30.   query.equalTo("objectId",userId);
  31.   query.first().then(function(result){
  32.       if(result){
  33.           // If result was defined, the object with this objectID was found
  34.           promise.resolve(result);
  35.       } else {
  36.           console.log("User ID: " + userId + " was not found");
  37.           promise.resolve(null);
  38.       }
  39.   }, function(error){
  40.           console.error("Error searching for User with id: " + userId + " Error: " + error);
  41.           promise.error(error);
  42.   });
  43.   return promise;
  44. }
  45.  
  46. function sendEmailOnFeedback(useremail, grade, message) {
  47.  
  48.   //// https://nodemailer.com/about/
  49.   const nodemailer = require("nodemailer");
  50.   //// create reusable transporter object using the default SMTP transport
  51.   const mailTransport = nodemailer.createTransport({
  52.   service: 'gmail',
  53.   auth: {
  54.     user: "wadertest123@gmail.com",
  55.     pass: "anoda123456"
  56.   }
  57.   });
  58.  
  59.   //// setup email data with unicode symbols
  60.   const mailOptions = {
  61.     from: '"Wader App" <wader@example.com>', // sender address
  62.     to: "test@anoda.io, lamcevs0@gmail.com", // list of receivers
  63.     subject: "New user feedback", // Subject line
  64.     text: "Hello world?", // plain text body
  65.     //html: '<b> ${useremail} </b> <br> <b> ${message} </b> <br>' // html body
  66.   };
  67.  
  68.   // send mail with defined transport object
  69.   return mailTransport.sendMail(mailOptions);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement