From 42d189309e3f09ea69d63be524736423029adf85 Mon Sep 17 00:00:00 2001 From: Harsh Patel Date: Sat, 14 Dec 2019 16:22:38 -0500 Subject: [PATCH 1/5] Returns error 409 with proper email. --- constants/error.constant.js | 2 ++ models/account.model.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/constants/error.constant.js b/constants/error.constant.js index 06ab069d..f2e6c535 100644 --- a/constants/error.constant.js +++ b/constants/error.constant.js @@ -9,6 +9,7 @@ const VOLUNTEER_404_MESSAGE = "Volunteer not found"; const SETTINGS_404_MESSAGE = "Settings not found"; const ACCOUNT_TYPE_409_MESSAGE = "Wrong account type"; +const ACCOUNT_EMAIL_409_MESSAGE = "Email already in use"; const SPONSOR_ID_409_MESSAGE = "Conflict with sponsor accountId link"; const VOLUNTEER_ID_409_MESSAGE = "Conflict with volunteer accountId link"; const HACKER_ID_409_MESSAGE = "Conflict with hacker accountId link"; @@ -50,6 +51,7 @@ module.exports = { TEAM_404_MESSAGE: TEAM_404_MESSAGE, RESUME_404_MESSAGE: RESUME_404_MESSAGE, ACCOUNT_TYPE_409_MESSAGE: ACCOUNT_TYPE_409_MESSAGE, + ACCOUNT_EMAIL_409_MESSAGE: ACCOUNT_EMAIL_409_MESSAGE, SPONSOR_ID_409_MESSAGE: SPONSOR_ID_409_MESSAGE, VOLUNTEER_ID_409_MESSAGE: VOLUNTEER_ID_409_MESSAGE, TEAM_MEMBER_409_MESSAGE: TEAM_MEMBER_409_MESSAGE, diff --git a/models/account.model.js b/models/account.model.js index c5228f42..8ac22152 100644 --- a/models/account.model.js +++ b/models/account.model.js @@ -2,6 +2,8 @@ const mongoose = require("mongoose"); const bcrypt = require("bcrypt"); const Constants = require("../constants/general.constant"); +const Error = require("../constants/error.constant") + //describes the data type const AccountSchema = new mongoose.Schema({ firstName: { @@ -96,5 +98,18 @@ AccountSchema.methods.getAge = function() { return Math.abs(ageDate.getUTCFullYear() - 1970); }; +var handleE11000 = function(error, res, next) { + if (error.name === 'MongoError' && error.code === 11000) { + next( + { + status: 409, + message: Error.ACCOUNT_EMAIL_409_MESSAGE, + }); + } else { + next(); + } +}; + +AccountSchema.post('findOneAndUpdate', handleE11000); //export the model module.exports = mongoose.model("Account", AccountSchema); From dfd25c4b6868fb8838897aba954d1ce3c80fcbdb Mon Sep 17 00:00:00 2001 From: Harsh Patel Date: Mon, 16 Dec 2019 20:48:57 -0500 Subject: [PATCH 2/5] Made it into middleware --- middlewares/account.middleware.js | 14 +++++++++++++- models/account.model.js | 13 ------------- routes/api/account.js | 3 ++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/middlewares/account.middleware.js b/middlewares/account.middleware.js index bea0e578..2956e757 100644 --- a/middlewares/account.middleware.js +++ b/middlewares/account.middleware.js @@ -156,6 +156,17 @@ async function addAccount(req, res, next) { return next(); } +async function validateUniqueEmail(req, res, next) { + const acc = await Services.Account.findByEmail(req.body.email); + if (acc && acc.id != req.params.id) { + return next({ + status: 409, + message: Constants.Error.ACCOUNT_EMAIL_409_MESSAGE, + }); + } + return next(); +} + /** * Updates an account that is specified by req.params.id * @param {{params:{id: string}, body: *}} req @@ -254,5 +265,6 @@ module.exports = { updatePassword: Middleware.Util.asyncMiddleware(updatePassword), addAccount: Middleware.Util.asyncMiddleware(addAccount), updateAccount: Middleware.Util.asyncMiddleware(updateAccount), - inviteAccount: Middleware.Util.asyncMiddleware(inviteAccount) + inviteAccount: Middleware.Util.asyncMiddleware(inviteAccount), + validateUniqueEmail: Middleware.Util.asyncMiddleware(validateUniqueEmail) }; diff --git a/models/account.model.js b/models/account.model.js index 8ac22152..9dbf0e42 100644 --- a/models/account.model.js +++ b/models/account.model.js @@ -98,18 +98,5 @@ AccountSchema.methods.getAge = function() { return Math.abs(ageDate.getUTCFullYear() - 1970); }; -var handleE11000 = function(error, res, next) { - if (error.name === 'MongoError' && error.code === 11000) { - next( - { - status: 409, - message: Error.ACCOUNT_EMAIL_409_MESSAGE, - }); - } else { - next(); - } -}; - -AccountSchema.post('findOneAndUpdate', handleE11000); //export the model module.exports = mongoose.model("Account", AccountSchema); diff --git a/routes/api/account.js b/routes/api/account.js index dfa7c802..6f7cc559 100644 --- a/routes/api/account.js +++ b/routes/api/account.js @@ -21,7 +21,7 @@ const Services = { }; module.exports = { - activate: function(apiRouter) { + activate: function (apiRouter) { const accountRouter = express.Router(); /** @@ -252,6 +252,7 @@ module.exports = { Middleware.parseBody.middleware, Middleware.Account.parsePatch, + Middleware.Account.validateUniqueEmail, Middleware.Account.updateAccount, Middleware.Auth.sendConfirmAccountEmail, // no parse account because will use req.body as information From fd5b855f19c66fda8959fe87b994e90c5b6c7cd4 Mon Sep 17 00:00:00 2001 From: Harsh Patel Date: Mon, 16 Dec 2019 20:49:50 -0500 Subject: [PATCH 3/5] Update account.model.js --- models/account.model.js | 1 - 1 file changed, 1 deletion(-) diff --git a/models/account.model.js b/models/account.model.js index 9dbf0e42..c22b60a5 100644 --- a/models/account.model.js +++ b/models/account.model.js @@ -2,7 +2,6 @@ const mongoose = require("mongoose"); const bcrypt = require("bcrypt"); const Constants = require("../constants/general.constant"); -const Error = require("../constants/error.constant") //describes the data type const AccountSchema = new mongoose.Schema({ From be1b3d921e8b4c20cbfcb8e79b111f2219420c80 Mon Sep 17 00:00:00 2001 From: Harsh Patel Date: Mon, 16 Dec 2019 20:50:02 -0500 Subject: [PATCH 4/5] Update account.model.js --- models/account.model.js | 1 - 1 file changed, 1 deletion(-) diff --git a/models/account.model.js b/models/account.model.js index c22b60a5..c5228f42 100644 --- a/models/account.model.js +++ b/models/account.model.js @@ -2,7 +2,6 @@ const mongoose = require("mongoose"); const bcrypt = require("bcrypt"); const Constants = require("../constants/general.constant"); - //describes the data type const AccountSchema = new mongoose.Schema({ firstName: { From 32bfc18810eb0c3ac5582694fb099c8a1b5e1c01 Mon Sep 17 00:00:00 2001 From: Harsh Patel Date: Mon, 16 Dec 2019 20:52:16 -0500 Subject: [PATCH 5/5] Added comment --- middlewares/account.middleware.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/middlewares/account.middleware.js b/middlewares/account.middleware.js index 2956e757..4d333afb 100644 --- a/middlewares/account.middleware.js +++ b/middlewares/account.middleware.js @@ -156,6 +156,13 @@ async function addAccount(req, res, next) { return next(); } +/** + * @async + * @function validateUniqueEmail + * @param {{params:{id: string}, body: {email: string}}} req + * @param {*} res + * @description Gets an account by email in req.body, and if found ensures id matches req.params.id + */ async function validateUniqueEmail(req, res, next) { const acc = await Services.Account.findByEmail(req.body.email); if (acc && acc.id != req.params.id) {