diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..0b76996 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +9.11.1 \ No newline at end of file diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index f9391e2..ae3e8ab 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -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 = { diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index e7cd336..6086cd0 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -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 = {