diff --git a/src/config/errors.js b/src/config/errors.js index 1c4b805..33ae981 100644 --- a/src/config/errors.js +++ b/src/config/errors.js @@ -21,5 +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-found-for-number' + 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..92e4f6b 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..4d3d1f3 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -22,7 +22,20 @@ 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 + * @param {string} tagferId SessionId obtained by extracting from authorization header + * @returns {Object} Profile object containg information for a specific user's profile + */ +function getProfile(profileNumber, tagferId) { + return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) { + return (snapshot.exists() ? snapshot.val() : {}); + }); +} + module.exports = { updateProfile, - createInitialProfiles + createInitialProfiles, + getProfile }; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 043e331..6085056 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -18,21 +18,39 @@ 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}) + } +} + +async function getUserProfile(req, res) { + const profileNumber = req.params.profileNumber; + if (!utils.isProfileNumberValid(profileNumber)) { + return; + } + 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 }) } } module.exports = { - updateUserProfile + updateUserProfile, + getUserProfile } \ No newline at end of file