T-954157790482088: bug fixes based on code review changes

This commit is contained in:
Okechi Onyeje 2018-12-29 20:34:20 -05:00
parent bdcd675c9c
commit 97f7d7bc92
4 changed files with 14 additions and 32 deletions

View File

@ -115,8 +115,8 @@ function createNewSessionId(tagferId) {
function getSession(id) {
const sessions = loki.getCollection("sessions");
var session = sessions.by('id', id)
if (!_.isEmpty(session) ) {
return session[0];
if (session) {
return session;
} else {
throw errors.AUTH_INVALID_SESSION_ID;
}

View File

@ -2,7 +2,8 @@ const authDao = require('./dao');
const profileDao = require('./../profiles/dao');
const utils = require('../utils/utils');
const errors = require('../../config/errors');
const appConfig = require('../../config/app.json')
const appConfig = require('../../config/app.json');
const http = require('../../config/http');
// Handlers
/**
@ -113,19 +114,16 @@ function signup(req, res) {
const sessionId = authDao.createNewSessionId(user.uid);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
profileDao.createInitialProfiles(profile,tagferId).then ((result) => { //for some reason this callback is not returning the data populated by promise
if (result.result) {
profileDao.createInitialProfiles(profile,tagferId).then (() => {
res.json({sessionId});
} else {
res.json( {error: result.error});
}
}).catch((error) => {
res.status(http.BAD_REQUEST).json({error});
})
} catch (error) {
res.status(http.BAD_REQUEST).json({error});
}
}).catch(error => {
error = error.code
res.json( {error: error.code} );
res.json( {error} );
});
}

View File

@ -5,13 +5,9 @@ const database = require('firebase-admin').database();
* @param {object} profileObj
* @param {string} tagferId
*/
async function createInitialProfiles(profileObj, tagferId) {
function createInitialProfiles(profileObj, tagferId) {
//persist profile data to firebase
await database.ref(`/profiles/${tagferId}/profile1`).set(profileObj).then( () => {
return {result:true};
}).catch( (error) => {
return {result:false, error};
});
return database.ref(`/profiles/${tagferId}/profile1`).set(profileObj);
}
/**
@ -21,13 +17,9 @@ async function createInitialProfiles(profileObj, tagferId) {
* @param {string} tagferId tagferId obtained by extracting from authorization header
* @returns {Boolean} Boolean result of whether the
*/
async function updateProfile(profileObj, profileNumber, tagferId) {
function updateProfile(profileObj, profileNumber, tagferId) {
//persist profile data to firebase
await database.ref(`/profiles/${tagferId}/profile${profileNumber}`).update(profileObj).then( () => {
return {result:true};
}).catch( (error) => {
return {result:false, error};
});
return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).update(profileObj);
}
module.exports = {

View File

@ -17,16 +17,8 @@ async function updateUserProfile(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
var createRes = profileDao.updateProfile(profileObj, req.params.profileNumber, tagferId).then( (createRes) => {
if (createRes.result) {
res.status(http.CREATED).json({})
} else {
if (createRes.error) {
res.status(http.BAD_REQUEST).json({ error: createRes.error})
} else {
res.status(http.INTERNAL_SERVER_ERROR).json({ error: errors.MAX_NUMBER_OF_PROFILES_REACHED});
}
}
profileDao.updateProfile(profileObj, req.params.profileNumber, tagferId).then( () => {
res.status(http.CREATED).json({})
}).catch( (error) => {
res.status(http.INTERNAL_SERVER_ERROR).json({error: "Promise error unknown"})
});