Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var _ = require("underscore");
- var kue = require('kue-scheduler');
- var Queue = kue.createQueue();
- // var header = new smtpapi();
- //create a job instance
- var job = Queue
- .createJob('createUser')
- .attempts(3)
- .priority('normal');
- //schedule it to run every 2 seconds
- Queue.every('10 seconds', job);
- //whatever you define inside this function will run every 10 seconds
- Queue.process('createUser', function(job, done) {
- //console.log("job running");
- //console.log(needToNameIt());
- var json={
- "user_id":"vipul@socialcops.com",
- "password":"demo123"
- }
- var user=new Parse.Object('EmailTemp',json);
- // console.log(user);
- // user.set("user_id","vipul@socialcops.com");
- // user.set("password","demo123");
- user.save({
- success: function(results){
- console.log("User Saved");
- },
- error: function(error){
- console.log(error)
- }
- });
- // console.log("sometrhing")
- done(); //the process is complete
- });
- exports.updateUser= function(request, response) {
- if (!request.user) {
- response.error("Must be signed in to call this Cloud Function.")
- return;
- }
- // The user making this request is available in request.user
- // Make sure to first check if this user is authorized to perform this change.
- // One way of doing so is to query an Admin role and check if the user belongs to that Role.
- // Replace !authorized with whatever check you decide to implement.
- // if (!authorized) {
- // response.error("Not an Admin.")
- // return;
- // }
- // The rest of the function operates on the assumption that request.user is *authorized*
- Parse.Cloud.useMasterKey();
- // Query for the user to be modified by username
- // The username is passed to the Cloud Function in a
- // key named "username". You can search by email or
- // user id instead depending on your use case.
- var query = new Parse.Query(Parse.User);
- query.equalTo("username", request.params.email);
- // Get the first user which matches the above constraints.
- query.first({
- success: function(anotherUser) {
- // Successfully retrieved the user.
- // Modify any parameters as you see fit.
- // You can use request.params to pass specific
- // keys and values you might want to change about
- // this user.
- anotherUser.set("fullName", request.params.name);
- anotherUser.set("email", request.params.email);
- anotherUser.set("username", request.params.email);
- anotherUser.set("phone", request.params.phone);
- anotherUser.set("userGroup", request.params.userGroup);
- anotherUser.set("permissions",request.params.permissions);
- // Save the user.
- anotherUser.save(null, {
- success: function(anotherUser) {
- // The user was saved successfully.
- response.success("Successfully updated user.");
- },
- error: function(error) {
- // The save failed.
- // error is a Parse.Error with an error code and description.
- response.error("Could not save changes to user.");
- }
- });
- },
- error: function(error) {
- response.error("Could not find user.");
- }
- });
- }
- exports.deleteUser= function(request, response) {
- if (!request.user) {
- response.error("Must be signed in to call this Cloud Function.")
- return;
- }
- Parse.Cloud.useMasterKey();
- var query = new Parse.Query(Parse.User);
- query.equalTo("username", request.params.email);
- query.first({
- success: function(anotherUser) {
- // anotherUser.set("isActive", false);
- // Save the user.
- anotherUser.destroy({
- success: function(anotherUser) {
- // The user was saved successfully.
- response.success("Successfully deleted user.");
- },
- error: function(error) {
- // The save failed.
- // error is a Parse.Error with an error code and description.
- response.error("Could not save changes to user.");
- }
- });
- },
- error: function(error) {
- response.error("Could not find user.");
- }
- });
- }
- exports.afterSaveUser= function(request){
- var smtpapi = require('smtpapi')
- var nodemailer = require('nodemailer');
- var header = new smtpapi();
- var user_id=request.object.get("user_id");
- var password=request.object.get("password");
- header.setTos(user_id);
- header.setSubstitutions({'%user_id%': [user_id], '%password%': [password]});
- header.setFilters({
- 'templates': {
- 'settings': {
- 'enable': 1,
- 'template_id': "912054f1-743c-4975-b9d9-b631d6b4c6f2"
- }
- }
- });
- console.log(header.jsonString());
- // Send usin Nodemailer
- var headers = { 'x-smtpapi': header.jsonString() };
- // Use nodemailer to send the email
- var settings = {
- host: "smtp.sendgrid.net",
- port: parseInt(587, 10),
- requiresAuth: true,
- auth: {
- user: "mukund",
- pass: "mu271190"
- }
- };
- var smtpTransport = nodemailer.createTransport(settings);
- var mailOptions = {
- from: "NITI Admin <dashboard-niti@gov.in>",
- to: "vipul@socialcops.com",
- headers: headers
- // html: "<b>Hello world</b>",
- }
- smtpTransport.sendMail(mailOptions, function(error, response) {
- smtpTransport.close();
- if (error) {
- console.log(error);
- } else {
- console.log("Message sent");
- }
- });
- }
Add Comment
Please, Sign In to add comment