This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from HackIllinois/staging
Version 0.0.4
- Loading branch information
Showing
21 changed files
with
527 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
var bodyParser = require('body-parser'); | ||
var _Promise = require('bluebird'); | ||
|
||
var services = require('../services'); | ||
var middleware = require('../middleware'); | ||
var requests = require('../requests'); | ||
var roles = require('../utils/roles'); | ||
var mail = require('../utils/mail'); | ||
|
||
var router = require('express').Router(); | ||
|
||
function _isAuthenticated (req) { | ||
return req.auth && (req.user !== undefined); | ||
} | ||
|
||
function _removeFromList(rsvpCurrent, rsvpNew) { | ||
return rsvpCurrent.get('isAttending') && !rsvpNew.isAttending; | ||
} | ||
|
||
function _addToList(rsvpCurrent, rsvpNew) { | ||
return !rsvpCurrent.get('isAttending') && rsvpNew.isAttending; | ||
} | ||
|
||
function createRSVP(req, res, next) { | ||
if(!req.body.isAttending) | ||
delete req.body.type; | ||
|
||
services.RegistrationService | ||
.findAttendeeByUser(req.user) | ||
.then(function(attendee) { | ||
return services.RSVPService | ||
.createRSVP(attendee, req.user, req.body); | ||
}) | ||
.then(function(rsvp) { | ||
if(rsvp.get('isAttending')) | ||
services.MailService.addToList(req.user, mail.lists.attendees); | ||
res.body = rsvp.toJSON(); | ||
|
||
return next(); | ||
}) | ||
.catch(function(error) { | ||
return next(error); | ||
}); | ||
} | ||
|
||
function fetchRSVPByUser(req, res, next) { | ||
services.RegistrationService | ||
.findAttendeeByUser(req.user) | ||
.then(function(attendee) { | ||
return services.RSVPService | ||
.findRSVPByAttendee(attendee); | ||
}) | ||
.then(function (rsvp) { | ||
res.body = rsvp.toJSON(); | ||
if(!res.body.type) { | ||
delete res.body.type; | ||
} | ||
|
||
return next(); | ||
}) | ||
.catch(function(error) { | ||
return next(error); | ||
}) | ||
} | ||
|
||
function fetchRSVPById(req, res, next) { | ||
services.RSVPService | ||
.getRSVPById(req.params.id) | ||
.then(function(rsvp){ | ||
res.body = rsvp.toJSON(); | ||
if(!res.body.type) { | ||
delete res.body.type; | ||
} | ||
|
||
return next(); | ||
}) | ||
.catch(function(error) { | ||
return next(error); | ||
}) | ||
} | ||
|
||
function updateRSVPByUser(req, res, next) { | ||
if(!req.body.isAttending) | ||
delete req.body.type; | ||
|
||
services.RegistrationService | ||
.findAttendeeByUser(req.user) | ||
.then(function(attendee) { | ||
return _updateRSVPByAttendee(req.user, attendee, req.body); | ||
}) | ||
.then(function(rsvp){ | ||
res.body = rsvp.toJSON(); | ||
|
||
return next(); | ||
}) | ||
.catch(function (error) { | ||
return next(error); | ||
}); | ||
} | ||
|
||
function _updateRSVPByAttendee(user, attendee, newRSVP) { | ||
return services.RSVPService | ||
.findRSVPByAttendee(attendee) | ||
.then(function (rsvp) { | ||
return services.RSVPService.updateRSVP(user, rsvp, newRSVP) | ||
.then(function (updatedRSVP) { | ||
if(_addToList(rsvp, newRSVP)) | ||
services.MailService.addToList(user, mail.lists.attendees); | ||
if(_removeFromList(rsvp, newRSVP)) | ||
services.MailService.removeFromList(user, mail.lists.attendees); | ||
|
||
return updatedRSVP | ||
}); | ||
}); | ||
} | ||
|
||
router.use(bodyParser.json()); | ||
router.use(middleware.auth); | ||
|
||
router.post('/attendee', middleware.request(requests.RSVPRequest), | ||
middleware.permission(roles.ATTENDEE, _isAuthenticated), createRSVP); | ||
router.get('/attendee/', middleware.permission(roles.ATTENDEE), fetchRSVPByUser); | ||
router.get('/attendee/:id', middleware.permission(roles.ORGANIZERS), fetchRSVPById); | ||
router.put('/attendee/', middleware.request(requests.RSVPRequest), | ||
middleware.permission(roles.ATTENDEE), updateRSVPByUser); | ||
|
||
router.use(middleware.response); | ||
router.use(middleware.errors); | ||
|
||
module.exports.router = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var _ = require('lodash'); | ||
var CheckIt = require('checkit'); | ||
|
||
var rsvp = require('../utils/rsvp'); | ||
var Model = require('./Model'); | ||
var AttendeeRSVP = Model.extend({ | ||
tableName: 'attendee_rsvps', | ||
idAttribute: 'id', | ||
validations: { | ||
attendeeId: ['required', 'integer'], | ||
isAttending: ['required', 'boolean'] | ||
} | ||
}); | ||
|
||
AttendeeRSVP.findByAttendeeId = function (attendeeId) { | ||
return AttendeeRSVP.where({ attendee_id: attendeeId }).fetch(); | ||
}; | ||
|
||
AttendeeRSVP.prototype.validate = function () { | ||
var checkit = CheckIt(this.validations); | ||
checkit.maybe({type: ['required', 'string', rsvp.verifyAttendanceReply]}, function(input) { | ||
return input.isAttending; | ||
}); | ||
|
||
return checkit.run(this.attributes); | ||
}; | ||
|
||
module.exports = AttendeeRSVP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var Request = require('./Request'); | ||
var rsvp = require('../utils/rsvp'); | ||
|
||
var bodyRequired = ['isAttending']; | ||
var bodyAllowed = ['type']; | ||
var bodyValidations = { | ||
'isAttending': ['required', 'boolean'], | ||
'type': ['string', rsvp.verifyAttendanceReply] | ||
}; | ||
|
||
function RSVPRequest(headers, body) { | ||
Request.call(this, headers, body); | ||
|
||
this.bodyRequired = bodyRequired; | ||
this.bodyAllowed = bodyAllowed; | ||
this.bodyValidations = bodyValidations; | ||
} | ||
|
||
RSVPRequest.prototype = Object.create(Request.prototype); | ||
RSVPRequest.prototype.constructor = RSVPRequest; | ||
|
||
module.exports = RSVPRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
var CheckitError = require('checkit').Error; | ||
var _Promise = require('bluebird'); | ||
var _ = require('lodash'); | ||
|
||
var RSVP = require('../models/AttendeeRSVP'); | ||
var UserRole = require('../models/UserRole'); | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
|
||
/** | ||
* Gets an rsvp by its id | ||
* @param {integer} id the id of the RSVP to find | ||
* @returns {Promise} the resolved rsvp | ||
*/ | ||
module.exports.getRSVPById = function (id) { | ||
return RSVP.findById(id); | ||
}; | ||
|
||
/** | ||
* Creates an RSVP and sets the users attendee role to active | ||
* @param {Attendee} attendee the associated attendee for the rsvp | ||
* @param {User} user the associated user for the rsvp | ||
* @param {Object} attributes the rsvp data | ||
* @returns {Promise} the resolved rsvp | ||
* @throws {InvalidParameterError} thrown when an attendee already has an rsvp | ||
*/ | ||
module.exports.createRSVP = function (attendee, user, attributes) { | ||
attributes.attendeeId = attendee.get('id'); | ||
var rsvp = RSVP.forge(attributes); | ||
|
||
return rsvp | ||
.validate() | ||
.catch(CheckitError, utils.errors.handleValidationError) | ||
.then(function (validated) { | ||
return RSVP.findByAttendeeId(attributes.attendeeId); | ||
}) | ||
.then(function (result) { | ||
if (!_.isNull(result)) { | ||
var message = "An RSVP already exists for the given attendee"; | ||
var source = "attendeeId"; | ||
throw new errors.InvalidParameterError(message, source); | ||
} | ||
|
||
var userRole = user.getRole(utils.roles.ATTENDEE); | ||
UserRole.setActive(userRole, true); | ||
|
||
return rsvp.save(); | ||
}) | ||
}; | ||
|
||
/** | ||
* Finds an RSVP by its associated attendee | ||
* @param {Attendee} attendee the associated attendee for the rsvp | ||
* @returns {Promise} the resolved rsvp for the attendee | ||
* @throws {NotFoundError} when the attendee has no RSVP | ||
*/ | ||
module.exports.findRSVPByAttendee = function (attendee) { | ||
return RSVP | ||
.findByAttendeeId(attendee.get('id')) | ||
.then(function (result) { | ||
if (_.isNull(result)) { | ||
var message = "An RSVP cannot be found for the given attendee"; | ||
var source = "attendeeId"; | ||
throw new errors.NotFoundError(message, source); | ||
} | ||
|
||
return _Promise.resolve(result); | ||
}); | ||
}; | ||
|
||
/** | ||
* Updates a given RSVP | ||
* @param {RSVP} rsvp the RSVP to update | ||
* @param {Object} attributes the new RSVP data to set | ||
* @returns {Promise} the resolved RSVP | ||
*/ | ||
module.exports.updateRSVP = function (user, rsvp, attributes) { | ||
rsvp.set({'type': null}); | ||
rsvp.set(attributes); | ||
|
||
return rsvp | ||
.validate() | ||
.catch(CheckitError, utils.errors.handleValidationError) | ||
.then(function (validated) { | ||
var userRole = user.getRole(utils.roles.ATTENDEE); | ||
rsvp.get('isAttending') ? UserRole.setActive(userRole, true) : UserRole.setActive(userRole, false); | ||
|
||
return rsvp.save(); | ||
}); | ||
}; |
Oops, something went wrong.