Change order url to invitee ID, add invitee model

This commit is contained in:
Ben Ramey
2014-05-13 13:11:43 -05:00
parent e4552ebfe4
commit d02bd6c659
10 changed files with 136 additions and 72 deletions

View 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
View 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();
},
};

View File

@@ -20,17 +20,10 @@ module.exports = {
type: 'email', type: 'email',
required: true required: true
}, },
invitees: {
type: 'array'
},
menuUrl: { menuUrl: {
type: 'url' type: 'url'
}, },
restaurant: 'string' restaurant: 'string'
}, },
afterCreate: function(values, next) {
EmailService.sendRunEmails(values);
next();
},
}; };

View File

@@ -1,33 +1,36 @@
// EmailService.js // EmailService.js
var sendgrid = require("sendgrid"); var sendgrid = require("sendgrid");
exports.sendRunEmails = function(runData) { exports.sendOrderEmail = function(inviteeData) {
var emailText = "You've been invited to " + runData.creatorName + "'s lunch run: '" Run.findOne({ id: inviteeData.run_id }, function(err, runData) {
+ runData.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/run/" if (err) {
+ runData.id + "/order/"; console.log(err);
var options = { return;
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);
for (var idx in runData.invitees) { var emailText = "You've been invited to " + runData.creatorName + "'s lunch run: '"
var invitee = runData.invitees[idx]; + runData.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/order/"
var specificOptions = options; + inviteeData.id;
specificOptions.to = invitee.email;
specificOptions.toname = invitee.name;
specificOptions.text = emailText + invitee.name;
console.log(specificOptions); 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);
sg.send(specificOptions, function(err, response) { sg.send(options, function(err, response) {
if (err) { if (err) {
console.log(err); console.log(err);
} else { } else {
console.log("message sent from: " + runData.creatorEmail); console.log("message sent from: " + runData.creatorEmail + ", to: " + inviteeData.email);
} }
}); });
}
console.log(options);
});
}; };

View File

@@ -16,7 +16,7 @@ lunchApp.config ['$routeProvider',
.when '/run/:id', .when '/run/:id',
templateUrl: 'templates/run/show.html', templateUrl: 'templates/run/show.html',
controller: 'RunCtrl' controller: 'RunCtrl'
.when '/run/:id/order/:inviteeName', .when '/order/:inviteeId',
templateUrl: 'templates/run/create-order.html', templateUrl: 'templates/run/create-order.html',
controller: 'CreateOrderCtrl' controller: 'CreateOrderCtrl'
.otherwise .otherwise

View File

@@ -5,24 +5,18 @@ lunchControllers.controller 'CreateOrderCtrl', ['$scope','socket','$routeParams'
$scope.show = false $scope.show = false
$scope.showMenuUrl = false $scope.showMenuUrl = false
socket.get '/run/' + $routeParams['id'], socket.get '/invitee/findOne', id: $routeParams['inviteeId'], (inviteeResponse) ->
(response) -> $scope.invitee = inviteeResponse
$scope.show = true
$scope.showMenuUrl = response.menuUrl
$scope.run = response socket.get '/run/' + inviteeResponse.run_id,
$scope.invitee = _.find response.invitees, (i) -> i.name.toLowerCase() == $routeParams['inviteeName'].toLowerCase() (runResponse) ->
$scope.invitees = response.invitees $scope.show = true
$scope.showMenuUrl = runResponse.menuUrl
$scope.$apply() $scope.run = runResponse
$scope.$apply()
$scope.submit = (invitee) -> $scope.submit = (invitee) ->
invitees = _.map $scope.invitees, (i) -> socket.put '/invitee/' + $routeParams['inviteeId'], { order: invitee.order }, (response) ->
if i.name is invitee.name $location.path '/run/' + $scope.run.id
return invitee
return i
socket.put '/run/' + $routeParams['id'], { invitees: invitees }, (response) ->
$location.path '/run/' + response.id
$scope.$apply() $scope.$apply()
] ]

View File

@@ -2,23 +2,31 @@ lunchControllers = angular.module 'lunchControllers'
lunchControllers.controller 'CreateRunCtrl', ['$scope', '$http', '$location', 'socket', lunchControllers.controller 'CreateRunCtrl', ['$scope', '$http', '$location', 'socket',
CreateRunController = ($scope, $http, $location, socket) -> CreateRunController = ($scope, $http, $location, socket) ->
$scope.run = $scope.run = {}
invitees: [] $scope.invitees = []
$scope.addInvitee = () -> $scope.addInvitee = () ->
$scope.run.invitees.push {} $scope.invitees.push {}
$scope.removeInvitee = (index) -> $scope.removeInvitee = (index) ->
$scope.run.invitees.splice index, 1 $scope.invitees.splice index, 1
$scope.create = (run) -> $scope.create = (run,invitees) ->
data = angular.copy run runData = angular.copy run
data.invitees.push inviteeData = angular.copy invitees
name: data.creatorName inviteeData.push
email: data.creatorEmail name: runData.creatorName
socket.post '/run', data, (response) -> email: runData.creatorEmail
$location.path '/run/' + response.id
$scope.$apply() socket.post '/run', runData, (runResponse) ->
inviteesCreated = 0
for i in inviteeData
i.run_id = runResponse.id
socket.post '/invitee', i, (inviteeResponse) ->
inviteesCreated++
if inviteesCreated is inviteeData.length
$location.path '/run/' + runResponse.id
$scope.$apply()
$scope.addInvitee() $scope.addInvitee()
] ]

View File

@@ -10,10 +10,13 @@ lunchControllers.controller 'RunCtrl', ['$scope', 'socket', '$routeParams', ($sc
(response) -> (response) ->
$scope.show = true $scope.show = true
$scope.run = response $scope.run = response
$scope.numOrders = $scope.run.invitees.length
$scope.setStatusClasses() socket.get '/invitee', run_id: response.id, (inviteeResponse) ->
$scope.setProgress() $scope.invitees = inviteeResponse
$scope.$apply() $scope.numOrders = $scope.invitees.length
$scope.setStatusClasses()
$scope.setProgress()
$scope.$apply()
socket.on 'message', (m) -> socket.on 'message', (m) ->
if m.verb is 'update' and m.id is $routeParams.id if m.verb is 'update' and m.id is $routeParams.id
@@ -23,14 +26,14 @@ lunchControllers.controller 'RunCtrl', ['$scope', 'socket', '$routeParams', ($sc
$scope.$apply() $scope.$apply()
$scope.setStatusClasses = () -> $scope.setStatusClasses = () ->
for i in $scope.run.invitees for i in $scope.invitees
if i.order if i.order
i.statusClass = 'done' i.statusClass = 'done'
else else
i.statusClass = 'incomplete' i.statusClass = 'incomplete'
$scope.setProgress = () -> $scope.setProgress = () ->
numFilledOrders = _.filter($scope.run.invitees, (i) -> i.order).length numFilledOrders = _.filter($scope.invitees, (i) -> i.order).length
$scope.progress = (numFilledOrders / $scope.numOrders) * 100 $scope.progress = (numFilledOrders / $scope.numOrders) * 100
if $scope.progress >= 100 if $scope.progress >= 100
$scope.progressClass = 'progress-bar-success' $scope.progressClass = 'progress-bar-success'

View File

@@ -55,7 +55,7 @@ block interiorcontent
.col-xs-12.col-sm-2.step-num .col-xs-12.col-sm-2.step-num
span.glyphicon.glyphicon-list span.glyphicon.glyphicon-list
.col-xs-12.col-sm-10 .col-xs-12.col-sm-10
.invitee.clearfix(ng-repeat='i in run.invitees') .invitee.clearfix(ng-repeat='i in invitees')
ng-form(name='inviteeForm') ng-form(name='inviteeForm')
.row .row
.col-xs-12.col-sm-5 .col-xs-12.col-sm-5
@@ -86,5 +86,5 @@ block interiorcontent
| Invite another! | Invite another!
section.submit section.submit
button.btn.btn-lg.btn-primary.btn-block(ng-click="create(run)",ng-disabled="form.$invalid") button.btn.btn-lg.btn-primary.btn-block(ng-click="create(run,invitees)",ng-disabled="form.$invalid")
| Create | Create

View File

@@ -13,11 +13,11 @@ block interiorcontent
div(class='progress-bar {{ progressClass }}',role='progressbar',aria-valuenow="{{ progress }}",aria-minvalue='0',aria-maxvalue='100',style='width:{{ progress }}%',ng-show='progress') div(class='progress-bar {{ progressClass }}',role='progressbar',aria-valuenow="{{ progress }}",aria-minvalue='0',aria-maxvalue='100',style='width:{{ progress }}%',ng-show='progress')
| {{ progress }}% | {{ progress }}%
div.row.run-order-status(ng-repeat="i in run.invitees") div.row.run-order-status(ng-repeat="i in invitees")
div(class='name col-sm-3 {{ i.statusClass }}') div(class='name col-sm-3 {{ i.statusClass }}')
span.glyphicon.glyphicon-user span.glyphicon.glyphicon-user
| {{ i.name }} | {{ i.name }}
.order.col-sm-7 {{ i.order }} .order.col-sm-7 {{ i.order }}
.fill.col-sm-2 .fill.col-sm-2
a.btn.btn-default.btn-sm.btn-block(href='#/run/{{ run.id }}/order/{{ i.name }}') a.btn.btn-default.btn-sm.btn-block(href='#/order/{{ i.id }}')
span.glyphicon.glyphicon-pencil span.glyphicon.glyphicon-pencil