From 4a74028aeeb5992fd44affde49088fb20b618ee7 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Mon, 24 Dec 2018 11:54:00 -0500 Subject: [PATCH 1/6] T-954157790482088: User can create up to 4 profiles - using backend validation to ensure no more than four profiles created - --- src/config/errors.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/config/errors.js b/src/config/errors.js index 1c4b805..60d8e56 100644 --- a/src/config/errors.js +++ b/src/config/errors.js @@ -21,5 +21,4 @@ module.exports = { MISSING_BODY_ATTRIBUTES: 'request/missing-body-attributes', //Profile Errors MAX_NUMBER_OF_PROFILES_REACHED: 'profile/max-number-of-profiles-are-being-used', - NO_PROFILE_FOUND_FOR_NUMBER: 'profile/no-profile-found-for-number' }; \ No newline at end of file From aece58471effac3f682863e75e4dd5861030ad91 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Mon, 24 Dec 2018 15:30:11 -0500 Subject: [PATCH 2/6] T-954157790456099: add get endpoint to retrieve user profile by number --- src/config/errors.js | 1 + src/config/router.js | 1 + src/ups/profiles/dao.js | 21 +++++++++++++++++++++ src/ups/profiles/handlers.js | 15 +++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/src/config/errors.js b/src/config/errors.js index 60d8e56..33ae981 100644 --- a/src/config/errors.js +++ b/src/config/errors.js @@ -21,4 +21,5 @@ module.exports = { MISSING_BODY_ATTRIBUTES: 'request/missing-body-attributes', //Profile Errors MAX_NUMBER_OF_PROFILES_REACHED: 'profile/max-number-of-profiles-are-being-used', + NO_PROFILE_FOUND_FOR_NUMBER: 'profile/no-profile-was-found-for-the-profile-number-passed', }; \ No newline at end of file diff --git a/src/config/router.js b/src/config/router.js index 67c545e..d88c22a 100644 --- a/src/config/router.js +++ b/src/config/router.js @@ -21,6 +21,7 @@ function router(app) { // Profile Endpoints app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile); + app.get('profiles/:profileNumber', ProfileHandlers.getUserProfile); } module.exports = router; \ No newline at end of file diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 15f822b..ae29bad 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -22,7 +22,28 @@ function updateProfile(profileObj, profileNumber, tagferId) { return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).set(profileObj); } +/** + * Gets a user profile + * @param {Number} profileNumber profile number that identifies which profile information to get for a user + * @returns {Object} Profile object containg information for a specific user's profile + */ +async function getProfile(profileNumber) { + //get tagfer-id from loki + const sessions = loki.getCollection("sessions"); + const primarySession = sessions.get(1); + const tagferId = primarySession.tagferId; + + var profile = null; + await database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) { + if (snapshot.val()) { + profile = snapshot.val(); + } + }); + return profile; +} + module.exports = { updateProfile, createInitialProfiles + getProfile }; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 043e331..ee62a82 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -33,6 +33,21 @@ async function updateUserProfile(req, res) { } } +async function getUserProfile(req, res) { + const { profileNumber } = req.param; + + var profile = dao.getProfile(profileNumber).then( (profile) => { + if (profile) { + res.json({ profile, status: http.OK}) + } else { + res.json({error: errors.NO_PROFILE_FOUND_FOR_NUMBER , status: http.BAD_REQUEST}) + } + }).catch( error => { + res.json({status: http.INTERNAL_SERVER_ERROR, error}) + }) +} + module.exports = { updateUserProfile + getUserProfile } \ No newline at end of file From 1271b2dfe60d20e9d7b8b6a1fb272215b33b261b Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Sat, 29 Dec 2018 21:14:29 -0500 Subject: [PATCH 3/6] T-954157790456099: Add endpoint for getting user profiles --- src/config/router.js | 2 +- src/ups/profiles/dao.js | 18 +++++------------- src/ups/profiles/handlers.js | 28 ++++++++++++++++------------ 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/config/router.js b/src/config/router.js index d88c22a..92e4f6b 100644 --- a/src/config/router.js +++ b/src/config/router.js @@ -21,7 +21,7 @@ function router(app) { // Profile Endpoints app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile); - app.get('profiles/:profileNumber', ProfileHandlers.getUserProfile); + app.get('/profiles/:profileNumber', ProfileHandlers.getUserProfile); } module.exports = router; \ No newline at end of file diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index ae29bad..41c4cd2 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -25,25 +25,17 @@ function updateProfile(profileObj, profileNumber, tagferId) { /** * Gets a user profile * @param {Number} profileNumber profile number that identifies which profile information to get for a user + * @param {string} tagferId SessionId obtained by extracting from authorization header * @returns {Object} Profile object containg information for a specific user's profile */ -async function getProfile(profileNumber) { - //get tagfer-id from loki - const sessions = loki.getCollection("sessions"); - const primarySession = sessions.get(1); - const tagferId = primarySession.tagferId; - - var profile = null; - await database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) { - if (snapshot.val()) { - profile = snapshot.val(); - } +function getProfile(profileNumber, tagferId) { + return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) { + return snapshot.val(); }); - return profile; } module.exports = { updateProfile, - createInitialProfiles + createInitialProfiles, getProfile }; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index ee62a82..783e7be 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -34,20 +34,24 @@ async function updateUserProfile(req, res) { } async function getUserProfile(req, res) { - const { profileNumber } = req.param; - - var profile = dao.getProfile(profileNumber).then( (profile) => { - if (profile) { - res.json({ profile, status: http.OK}) - } else { - res.json({error: errors.NO_PROFILE_FOUND_FOR_NUMBER , status: http.BAD_REQUEST}) - } - }).catch( error => { - res.json({status: http.INTERNAL_SERVER_ERROR, error}) - }) + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + try { + const tagferId = authDao.getSession(sessionId).tagferId; + profileDao.getProfile(req.params.profileNumber,tagferId).then((profile) => { + if (profile) { + res.status(http.OK).json({profile}) + } else { + res.status(http.BAD_REQUEST).json({ error: errors.NO_PROFILE_FOUND_FOR_NUMBER}) + } + }).catch(error => { + res.status(http.INTERNAL_SERVER_ERROR).json({error}) + }) + } catch (error) { + res.status(http.BAD_REQUEST).json({ error }) + } } module.exports = { - updateUserProfile + updateUserProfile, getUserProfile } \ No newline at end of file From 2449b207da3108650babf37de53058a0eb2a4c37 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Mon, 31 Dec 2018 09:18:53 -0500 Subject: [PATCH 4/6] T-954157790456099: code review comments --- src/ups/profiles/dao.js | 2 +- src/ups/profiles/handlers.js | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 41c4cd2..4d3d1f3 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -30,7 +30,7 @@ function updateProfile(profileObj, profileNumber, tagferId) { */ function getProfile(profileNumber, tagferId) { return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) { - return snapshot.val(); + return (snapshot.exists() ? snapshot.val() : {}); }); } diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 783e7be..d3759db 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -34,20 +34,21 @@ async function updateUserProfile(req, res) { } async function getUserProfile(req, res) { - const sessionId = utils.getSessionIdFromAuthHeader(req, res); - try { - const tagferId = authDao.getSession(sessionId).tagferId; - profileDao.getProfile(req.params.profileNumber,tagferId).then((profile) => { - if (profile) { + const profileNumber = req.params.profileNumber; + if (profileNumber > 4 || profileNumber < 1) { + res.status(http.BAD_REQUEST).json({error: errors.NO_PROFILE_FOUND_FOR_NUMBER}); + } else { + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + try { + const tagferId = authDao.getSession(sessionId).tagferId; + profileDao.getProfile(profileNumber,tagferId).then((profile) => { res.status(http.OK).json({profile}) - } else { - res.status(http.BAD_REQUEST).json({ error: errors.NO_PROFILE_FOUND_FOR_NUMBER}) - } - }).catch(error => { - res.status(http.INTERNAL_SERVER_ERROR).json({error}) - }) - } catch (error) { - res.status(http.BAD_REQUEST).json({ error }) + }).catch(error => { + res.status(http.INTERNAL_SERVER_ERROR).json({error: error.code}) + }) + } catch (error) { + res.status(http.UNAUTHORIZED).json({ error }) + } } } From b49d62c151b37fe65bb355f271033ee8a4121308 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Tue, 1 Jan 2019 16:58:08 -0500 Subject: [PATCH 5/6] T-954157790456099: add isProfileNumberValidation check to getUserProfile endpoint --- src/ups/profiles/handlers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index d3759db..b0717ae 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -35,8 +35,8 @@ async function updateUserProfile(req, res) { async function getUserProfile(req, res) { const profileNumber = req.params.profileNumber; - if (profileNumber > 4 || profileNumber < 1) { - res.status(http.BAD_REQUEST).json({error: errors.NO_PROFILE_FOUND_FOR_NUMBER}); + if (!utils.isProfileNumberValid(profileNumber)) { + return; } else { const sessionId = utils.getSessionIdFromAuthHeader(req, res); try { From f7d84ceea5ab0d172a3108cc7089e93e9c56c737 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Tue, 1 Jan 2019 20:38:42 -0500 Subject: [PATCH 6/6] T-954157790456099: Remove unnesseceary else statements --- src/ups/profiles/handlers.js | 46 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index b0717ae..6085056 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -18,18 +18,17 @@ async function updateUserProfile(req, res) { if (!utils.isProfileNumberValid(profileNumber, res)) { return; - } else { - const sessionId = utils.getSessionIdFromAuthHeader(req, res); - try { - const tagferId = authDao.getSession(sessionId).tagferId; - profileDao.updateProfile(profileObj, profileNumber, tagferId).then( () => { - res.status(http.CREATED).json({}) - }).catch( (error) => { - res.status(http.INTERNAL_SERVER_ERROR).json({error: errors.APP_FIREBASE_DATABASE_ERROR}) - }); - } catch (error) { - res.status(http.UNAUTHORIZED).json({error}) - } + } + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + try { + const tagferId = authDao.getSession(sessionId).tagferId; + profileDao.updateProfile(profileObj, profileNumber, tagferId).then( () => { + res.status(http.CREATED).json({}) + }).catch( (error) => { + res.status(http.INTERNAL_SERVER_ERROR).json({error: errors.APP_FIREBASE_DATABASE_ERROR}) + }); + } catch (error) { + res.status(http.UNAUTHORIZED).json({error}) } } @@ -37,18 +36,17 @@ async function getUserProfile(req, res) { const profileNumber = req.params.profileNumber; if (!utils.isProfileNumberValid(profileNumber)) { return; - } else { - const sessionId = utils.getSessionIdFromAuthHeader(req, res); - try { - const tagferId = authDao.getSession(sessionId).tagferId; - profileDao.getProfile(profileNumber,tagferId).then((profile) => { - res.status(http.OK).json({profile}) - }).catch(error => { - res.status(http.INTERNAL_SERVER_ERROR).json({error: error.code}) - }) - } catch (error) { - res.status(http.UNAUTHORIZED).json({ error }) - } + } + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + try { + const tagferId = authDao.getSession(sessionId).tagferId; + profileDao.getProfile(profileNumber,tagferId).then((profile) => { + res.status(http.OK).json({profile}) + }).catch(error => { + res.status(http.INTERNAL_SERVER_ERROR).json({error: error.code}) + }) + } catch (error) { + res.status(http.UNAUTHORIZED).json({ error }) } }