From 1271b2dfe60d20e9d7b8b6a1fb272215b33b261b Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Sat, 29 Dec 2018 21:14:29 -0500 Subject: [PATCH] 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