mirror of
https://bitbucket.org/tagfer_team/tagfer-server.git
synced 2025-12-25 03:37:38 +00:00
T-954157790482088: added .nvmrc and refactored based on local testing
This commit is contained in:
parent
cd0100f1a4
commit
dd0779fe4e
@ -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 = {
|
||||
|
||||
@ -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 = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user