Merge branch 'T-954157790456099-User-Can-View-And-Edit-Each-Profile'

This commit is contained in:
Okechi Onyeje 2019-01-02 22:13:13 -05:00
commit 506166b146
4 changed files with 47 additions and 15 deletions

View File

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

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

View File

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