T-954157790456099: add get endpoint to retrieve user profile by number

This commit is contained in:
Okechi Onyeje 2018-12-24 15:30:11 -05:00
parent 4a74028aee
commit aece58471e
4 changed files with 38 additions and 0 deletions

View File

@ -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',
};

View File

@ -21,6 +21,7 @@ function router(app) {
// Profile Endpoints
app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile);
app.get('profiles/:profileNumber', ProfileHandlers.getUserProfile);
}
module.exports = router;

View File

@ -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
};

View File

@ -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
}