Change order url to invitee ID, add invitee model
This commit is contained in:
30
api/controllers/InviteeController.js
Normal file
30
api/controllers/InviteeController.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* InviteeController
|
||||
*
|
||||
* @module :: Controller
|
||||
* @description :: A set of functions called `actions`.
|
||||
*
|
||||
* Actions contain code telling Sails how to respond to a certain type of request.
|
||||
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
|
||||
*
|
||||
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
|
||||
* and/or override them with custom routes (`config/routes.js`)
|
||||
*
|
||||
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
|
||||
*
|
||||
* @docs :: http://sailsjs.org/#!documentation/controllers
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Overrides for the settings in `config/controllers.js`
|
||||
* (specific to InviteeController)
|
||||
*/
|
||||
_config: {}
|
||||
|
||||
|
||||
};
|
||||
33
api/models/Invitee.js
Normal file
33
api/models/Invitee.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Invitee
|
||||
*
|
||||
* @module :: Model
|
||||
* @description :: A short summary of how this model works and what it represents.
|
||||
* @docs :: http://sailsjs.org/#!documentation/models
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
|
||||
attributes: {
|
||||
name: {
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
email: {
|
||||
type: 'email',
|
||||
required: true
|
||||
},
|
||||
order: {
|
||||
type: 'string'
|
||||
},
|
||||
run_id: {
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
|
||||
afterCreate: function(values, next) {
|
||||
EmailService.sendOrderEmail(values);
|
||||
next();
|
||||
},
|
||||
|
||||
};
|
||||
@@ -20,17 +20,10 @@ module.exports = {
|
||||
type: 'email',
|
||||
required: true
|
||||
},
|
||||
invitees: {
|
||||
type: 'array'
|
||||
},
|
||||
menuUrl: {
|
||||
type: 'url'
|
||||
},
|
||||
restaurant: 'string'
|
||||
},
|
||||
|
||||
afterCreate: function(values, next) {
|
||||
EmailService.sendRunEmails(values);
|
||||
next();
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,33 +1,36 @@
|
||||
// EmailService.js
|
||||
var sendgrid = require("sendgrid");
|
||||
|
||||
exports.sendRunEmails = function(runData) {
|
||||
var emailText = "You've been invited to " + runData.creatorName + "'s lunch run: '"
|
||||
+ runData.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/run/"
|
||||
+ runData.id + "/order/";
|
||||
var options = {
|
||||
fromname: runData.creatorName + " via LunchRun.me",
|
||||
from: sails.config.email.fromAddress,
|
||||
subject: "You've been invited to a lunch run!",
|
||||
replyto: runData.creatorEmail
|
||||
};
|
||||
var sg = sendgrid(sails.config.email.apiUser, sails.config.email.apiKey);
|
||||
exports.sendOrderEmail = function(inviteeData) {
|
||||
Run.findOne({ id: inviteeData.run_id }, function(err, runData) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
for (var idx in runData.invitees) {
|
||||
var invitee = runData.invitees[idx];
|
||||
var specificOptions = options;
|
||||
specificOptions.to = invitee.email;
|
||||
specificOptions.toname = invitee.name;
|
||||
specificOptions.text = emailText + invitee.name;
|
||||
var emailText = "You've been invited to " + runData.creatorName + "'s lunch run: '"
|
||||
+ runData.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/order/"
|
||||
+ inviteeData.id;
|
||||
|
||||
var options = {
|
||||
fromname: runData.creatorName + " via LunchRun.me",
|
||||
from: sails.config.email.fromAddress,
|
||||
to: inviteeData.email,
|
||||
toname: inviteeData.name,
|
||||
subject: "You've been invited to a lunch run!",
|
||||
replyto: runData.creatorEmail,
|
||||
text: emailText
|
||||
};
|
||||
var sg = sendgrid(sails.config.email.apiUser, sails.config.email.apiKey);
|
||||
|
||||
console.log(specificOptions);
|
||||
sg.send(options, function(err, response) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log("message sent from: " + runData.creatorEmail + ", to: " + inviteeData.email);
|
||||
}
|
||||
});
|
||||
|
||||
sg.send(specificOptions, function(err, response) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log("message sent from: " + runData.creatorEmail);
|
||||
}
|
||||
});
|
||||
}
|
||||
console.log(options);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user