Guest User

Untitled

a guest
Nov 23rd, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _ = require("underscore");
  2.  
  3. var kue = require('kue-scheduler');
  4. var Queue = kue.createQueue();
  5. // var header    = new smtpapi();
  6. //create a job instance
  7. var job = Queue
  8.             .createJob('createUser')
  9.             .attempts(3)
  10.             .priority('normal');
  11.  
  12. //schedule it to run every 2 seconds
  13. Queue.every('10 seconds', job);
  14.  
  15.  
  16. //whatever you define inside this function will run every 10 seconds
  17. Queue.process('createUser', function(job, done) {
  18.     //console.log("job running");
  19.    
  20.    
  21.     //console.log(needToNameIt());
  22.    
  23.     var json={
  24.       "user_id":"vipul@socialcops.com",
  25.       "password":"demo123"
  26.     }
  27.     var user=new Parse.Object('EmailTemp',json);
  28.     // console.log(user);
  29.     // user.set("user_id","vipul@socialcops.com");
  30.     // user.set("password","demo123");
  31.     user.save({
  32.       success: function(results){
  33.         console.log("User Saved");    
  34.       },
  35.       error: function(error){
  36.         console.log(error)
  37.       }
  38.     });
  39.  
  40.     // console.log("sometrhing")
  41.  
  42.    
  43.      done(); //the process is complete
  44. });
  45.  
  46. exports.updateUser= function(request, response) {
  47.    
  48.   if (!request.user) {
  49.     response.error("Must be signed in to call this Cloud Function.")
  50.     return;
  51.   }
  52.   // The user making this request is available in request.user
  53.   // Make sure to first check if this user is authorized to perform this change.
  54.   // One way of doing so is to query an Admin role and check if the user belongs to that Role.
  55.   // Replace !authorized with whatever check you decide to implement.
  56.   // if (!authorized) {
  57.   //   response.error("Not an Admin.")
  58.   //   return;    
  59.   // }
  60.  
  61.   // The rest of the function operates on the assumption that request.user is *authorized*
  62.  
  63.   Parse.Cloud.useMasterKey();
  64.  
  65.   // Query for the user to be modified by username
  66.   // The username is passed to the Cloud Function in a
  67.   // key named "username". You can search by email or
  68.   // user id instead depending on your use case.
  69.  
  70.   var query = new Parse.Query(Parse.User);
  71.   query.equalTo("username", request.params.email);
  72.  
  73.   // Get the first user which matches the above constraints.
  74.   query.first({
  75.     success: function(anotherUser) {
  76.       // Successfully retrieved the user.
  77.       // Modify any parameters as you see fit.
  78.       // You can use request.params to pass specific
  79.       // keys and values you might want to change about
  80.       // this user.
  81.      
  82.       anotherUser.set("fullName", request.params.name);
  83.       anotherUser.set("email", request.params.email);
  84.       anotherUser.set("username", request.params.email);
  85.       anotherUser.set("phone", request.params.phone);
  86.       anotherUser.set("userGroup", request.params.userGroup);
  87.       anotherUser.set("permissions",request.params.permissions);
  88.       // Save the user.
  89.       anotherUser.save(null, {
  90.         success: function(anotherUser) {
  91.           // The user was saved successfully.
  92.           response.success("Successfully updated user.");
  93.         },
  94.         error: function(error) {
  95.           // The save failed.
  96.           // error is a Parse.Error with an error code and description.
  97.           response.error("Could not save changes to user.");
  98.         }
  99.       });
  100.     },
  101.     error: function(error) {
  102.       response.error("Could not find user.");
  103.     }
  104.   });
  105. }
  106.  
  107. exports.deleteUser= function(request, response) {
  108.    
  109.   if (!request.user) {
  110.     response.error("Must be signed in to call this Cloud Function.")
  111.     return;
  112.   }
  113.  
  114.   Parse.Cloud.useMasterKey();
  115.  
  116.  
  117.   var query = new Parse.Query(Parse.User);
  118.   query.equalTo("username", request.params.email);
  119.  
  120.  
  121.   query.first({
  122.     success: function(anotherUser) {
  123.            
  124.       // anotherUser.set("isActive", false);
  125.       // Save the user.
  126.       anotherUser.destroy({
  127.         success: function(anotherUser) {
  128.           // The user was saved successfully.
  129.           response.success("Successfully deleted user.");
  130.         },
  131.         error: function(error) {
  132.           // The save failed.
  133.           // error is a Parse.Error with an error code and description.
  134.           response.error("Could not save changes to user.");
  135.         }
  136.       });
  137.     },
  138.     error: function(error) {
  139.       response.error("Could not find user.");
  140.     }
  141.   });
  142. }
  143.  
  144. exports.afterSaveUser= function(request){
  145.   var smtpapi = require('smtpapi')
  146.   var nodemailer = require('nodemailer');
  147.   var header    = new smtpapi();
  148.   var user_id=request.object.get("user_id");
  149.   var password=request.object.get("password");
  150.  
  151.   header.setTos(user_id);
  152.   header.setSubstitutions({'%user_id%': [user_id], '%password%': [password]});
  153.  
  154.  
  155.  
  156.   header.setFilters({
  157.       'templates': {
  158.         'settings': {
  159.           'enable': 1,
  160.           'template_id': "912054f1-743c-4975-b9d9-b631d6b4c6f2"
  161.         }
  162.       }
  163.     });
  164.     console.log(header.jsonString());
  165.  
  166.     // Send usin Nodemailer
  167.     var headers    = { 'x-smtpapi': header.jsonString() };
  168.  
  169.     // Use nodemailer to send the email
  170.     var settings  = {
  171.       host: "smtp.sendgrid.net",
  172.       port: parseInt(587, 10),
  173.       requiresAuth: true,
  174.       auth: {
  175.         user: "mukund",
  176.         pass: "mu271190"
  177.       }
  178.     };
  179.     var smtpTransport = nodemailer.createTransport(settings);
  180.  
  181.     var mailOptions = {
  182.         from:     "NITI Admin <dashboard-niti@gov.in>",
  183.         to:       "vipul@socialcops.com",
  184.         headers:  headers
  185.         // html:     "<b>Hello world</b>",
  186.       }
  187.  
  188.    
  189.     smtpTransport.sendMail(mailOptions, function(error, response) {
  190.       smtpTransport.close();
  191.  
  192.       if (error) {
  193.         console.log(error);
  194.       } else {
  195.         console.log("Message sent");
  196.       }
  197.     });
  198.  
  199. }
Add Comment
Please, Sign In to add comment