From aece58471effac3f682863e75e4dd5861030ad91 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Mon, 24 Dec 2018 15:30:11 -0500 Subject: [PATCH] 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