diff --git a/routes/invite.js b/routes/invite.js index af6a6a4..f5a0250 100644 --- a/routes/invite.js +++ b/routes/invite.js @@ -13,13 +13,17 @@ module.exports = function (app) { app.post('/invites', function(req, res, next) { if(!validator.isEmail(req.body.email)) { - res.status(400).send('Invalid email'); + return res.status(400).send('invalid_email'); } function success() { res.status(200).end(); } + function alreadyInvited(email) { + res.status(400).send('already_invited'); + } + slack .createInvite(req.body.email) .then(function() { @@ -37,6 +41,11 @@ module.exports = function (app) { }) .then(success) .catch(function(err) { + + if(err === 'already_invited') { + return alreadyInvited(req.body.email); + } + var message = 'Creating automatic invitation failed for: ' + req.body.email + ' reason: ' + err; slack.createMessage(message); diff --git a/services/github.js b/services/github.js index 1c7b0b9..d987abc 100644 --- a/services/github.js +++ b/services/github.js @@ -49,10 +49,22 @@ module.exports = { .query({q: email}) .set('Authorization', 'token ' + config.github.token) .end(function(error, response){ - if (error) { - reject(error); + if(error) { + return reject(error); } - resolve(response.body); + + if(!response.body.items) { + return reject(new Error(response.body.message)); + } + + if(response.body.items.length === 0) { + return reject(new Error('Not Found')); + } + + resolve(response.body.items[0]); + }); + }); + }, /** * Invite user to organization */