T-954157790482088: added .nvmrc and refactored based on local testing

This commit is contained in:
Okechi Onyeje 2018-12-24 14:55:33 -05:00
parent cd0100f1a4
commit dd0779fe4e
3 changed files with 31 additions and 15 deletions

1
.nvmrc Normal file
View File

@ -0,0 +1 @@
9.11.1

View File

@ -9,7 +9,7 @@ const loki = require('../../config/loki');
/**
* Creates a new user profile
* @param {object} profileObj JSON object containing all data for a profile captured from the frontend
* @returns {Promise} A Promise that carries the result of the write operation for the New Profile or null if there are no more available slots to add profiles
* @returns {Boolean} Boolean result of whether the
*/
async function createNewProfile(profileObj) {
//get tagfer-id from loki
@ -18,10 +18,10 @@ async function createNewProfile(profileObj) {
const tagferId = primarySession.tagferId;
//persist profile data to firebase
var updateObj = {}
var updated = false;
await database.ref(`/profiles/${tagferId}`).once('value').then(function(snapshot) {
var updateObj = {}
var profiles = snapshot.val();
var updated = false;
if (profiles) {
//check the profile slots available
if(!profiles.profile2) {
@ -30,7 +30,7 @@ async function createNewProfile(profileObj) {
} else if (!profiles.profile3) {
profiles.profile3 = profileObj
updated = true
} else if (!profiles4) {
} else if (!profiles.profile4) {
profiles.profile4 = profileObj
updated = true
}
@ -43,8 +43,16 @@ async function createNewProfile(profileObj) {
}
})
return updated ? database.ref().update(updates) : null;
if (updated) {
await database.ref().update(updateObj).then( () => {
return {result:true};
}).catch( (error) => {
return {result:false, error};
});
} else {
return {result:false};
}
}
module.exports = {

View File

@ -1,6 +1,7 @@
const dao = require('./dao');
const utils = require('../utils/utils');
const errors = require('../../config/errors');
const http = require('../../config/http');
// Handlers
/**
@ -9,15 +10,21 @@ const errors = require('../../config/errors');
* @param {Object} req
* @param {Object} res {result: Boolean} | {error: String}
*/
function createUserProfile(req, res) {
const { profileObj } = req.body;
var promise = dao.createNewProfile(profileObj);
if (promise) {
promise.then((result) => res.json({ result }) ).catch(error => res.json(error));
} else {
res.json(errors.MAX_NUMBER_OF_PROFILES_REACHED);
}
async function createUserProfile(req, res) {
const profileObj = req.body;
var createRes = dao.createNewProfile(profileObj).then( (createRes) => {
if (createRes.result) {
res.json({ status: http.CREATED })
} else {
if (createRes.error) {
res.json({status: http.BAD_REQUEST, error: createRes.error})
} else {
res.json({status: http.INTERNAL_SERVER_ERROR, error: errors.MAX_NUMBER_OF_PROFILES_REACHED});
}
}
}).catch( (error) => {
res.json({status: http.INTERNAL_SERVER_ERROR, error: "Promise error unknown"})
});
}
module.exports = {