mirror of
https://bitbucket.org/tagfer_team/tagfer-server.git
synced 2025-12-25 03:37:38 +00:00
T-954157790482088: refactored profile update function
- added initializing function for creating default and blank profiles for new users
This commit is contained in:
parent
dd0779fe4e
commit
df7f92155c
1408
package-lock.json
generated
1408
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -20,7 +20,7 @@ function router(app) {
|
||||
app.get('/users/by/phone', UserHandlers.findNetworkByPhone);
|
||||
|
||||
// Profile Endpoints
|
||||
app.post('/profiles/', ProfileHandlers.createUserProfile);
|
||||
app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile);
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
@ -106,6 +106,17 @@ function createNewSessionId(tagferId) {
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the loki session containg the user's tagferId on the server
|
||||
* @param {string} id
|
||||
* @returns {object} Loki Session object containg user's tagferId or null if id param passed is invalid
|
||||
*/
|
||||
function getSession(id) {
|
||||
const sessions = loki.getCollection("sessions");
|
||||
var session = sessions.find({'id': id})
|
||||
return (!_.isEmpty(session) ? session[0] : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new verification code for the phone number
|
||||
*/
|
||||
@ -166,6 +177,7 @@ module.exports = {
|
||||
signinWithEmail,
|
||||
createNewUser,
|
||||
createNewSessionId,
|
||||
getSession
|
||||
resetPassword
|
||||
};
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
const dao = require('./dao');
|
||||
const userDao = require('./dao');
|
||||
const profileDao = require('./../profiles/dao');
|
||||
const utils = require('../utils/utils');
|
||||
const errors = require('../../config/errors');
|
||||
|
||||
@ -14,7 +15,7 @@ function doesAttributeExist(req, res) {
|
||||
if (!utils.isAppSecretValid(req,res)) {
|
||||
return;
|
||||
}
|
||||
var promise = email? dao.doesEmailExist(email) : dao.doesTagferIdExist(tagferId.toLowerCase());
|
||||
var promise = email? userDao.doesEmailExist(email) : userDao.doesTagferIdExist(tagferId.toLowerCase());
|
||||
|
||||
promise.then((result) => res.json({ result }) ).catch(error => res.json(error));
|
||||
}
|
||||
@ -32,9 +33,9 @@ function sendPhoneCode(req, res) {
|
||||
}
|
||||
|
||||
const phoneNumber = req.body.phoneNumber;
|
||||
dao.doesPhoneExist(phoneNumber).then( result => {
|
||||
userDao.doesPhoneExist(phoneNumber).then( result => {
|
||||
if (result === false) {
|
||||
dao.sendVerificationCode(phoneNumber, status => res.json(status));
|
||||
userDao.sendVerificationCode(phoneNumber, status => res.json(status));
|
||||
} else{
|
||||
throw { error: errors.AUTH_PHONE_ALREADY_EXISTS };
|
||||
}
|
||||
@ -54,7 +55,7 @@ function verifyPhoneCode(req, res) {
|
||||
|
||||
const phoneNumber = req.body.phoneNumber;
|
||||
const code = req.body.code;
|
||||
res.json(dao.isVerificationCodeCorrect(phoneNumber, code));
|
||||
res.json(userDao.isVerificationCodeCorrect(phoneNumber, code));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,13 +74,13 @@ function signin(req, res) {
|
||||
var promise;
|
||||
|
||||
if (email) {
|
||||
promise = dao.signinWithEmail(email, password);
|
||||
promise = userDao.signinWithEmail(email, password);
|
||||
} else {
|
||||
promise = dao.signinWithTagferId(tagferId, password);
|
||||
promise = userDao.signinWithTagferId(tagferId, password);
|
||||
}
|
||||
|
||||
promise.then( ({ user })=> {
|
||||
const sessionId = dao.createNewSessionId(user.uid);
|
||||
const sessionId = userDao.createNewSessionId(user.uid);
|
||||
res.json({sessionId});
|
||||
}).catch(error => {
|
||||
res.json( {error: error.code} );
|
||||
@ -93,18 +94,39 @@ function signin(req, res) {
|
||||
* @param {Object} res { sessionId: String } | { error: String }
|
||||
*/
|
||||
function signup(req, res) {
|
||||
const user = req.body;
|
||||
const verifier = () => user.tagferId && user.email && user.password && user.phoneNumber && user.fullName;
|
||||
if (!utils.isAppSecretValid(req,res) || !utils.isBodyValid(verifier, res)) {
|
||||
const user = req.body.user;
|
||||
var profile = req.body.profile;
|
||||
const userVerifier = () => user.tagferId && user.email && user.password && user.phoneNumber && user.fullName;
|
||||
const profileVerifier = () => {
|
||||
return (
|
||||
profile.title && profile.companyTxt && profile.addressTxt
|
||||
);
|
||||
}
|
||||
|
||||
if (!utils.isAppSecretValid(req,res) || !utils.isBodyValid(userVerifier, res) || !utils.isBodyValid(profileVerifier, res)) {
|
||||
return;
|
||||
}
|
||||
|
||||
dao.createNewUser(user).then( user => {
|
||||
const sessionId = dao.createNewSessionId(user.uid);
|
||||
res.json({sessionId});
|
||||
if (!profile.contactNumbers || (profile.contactNumbers && !profile.contactNumbers.primary)) {
|
||||
profile.contactNumbers = {primary: user.phoneNumber};
|
||||
profile.name = "Business Profile"
|
||||
}
|
||||
profile.email = user.email
|
||||
|
||||
userDao.createNewUser(user).then( user => {
|
||||
sessionId = userDao.createNewSessionId(user.uid);
|
||||
profileDao.createInitialProfiles(profile,sessionId).then ((result) => { //for some reason this callback is not returning the data populated by promise
|
||||
if (result.result) {
|
||||
res.json({sessionId});
|
||||
} else {
|
||||
res.json( {error: result.error});
|
||||
}
|
||||
})
|
||||
}).catch(error => {
|
||||
error = error.code
|
||||
res.json( {error: error.code} );
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,60 +1,94 @@
|
||||
const database = require('firebase').database();
|
||||
const database = require('firebase-admin').database();
|
||||
const auth = require('../auth/dao');
|
||||
|
||||
const uuid = require('uuid/v4');
|
||||
const _ = require('lodash');
|
||||
|
||||
const errors = require('../../config/errors');
|
||||
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 {Boolean} Boolean result of whether the
|
||||
*
|
||||
* @param {object} profileObj
|
||||
* @param {string} sessionId
|
||||
*/
|
||||
async function createNewProfile(profileObj) {
|
||||
//get tagfer-id from loki
|
||||
const sessions = loki.getCollection("sessions");
|
||||
const primarySession = sessions.get(1);
|
||||
const tagferId = primarySession.tagferId;
|
||||
async function createInitialProfiles(profileObj, sessionId) {
|
||||
if (sessionId) {
|
||||
const tagferId = auth.getSession(sessionId).tagferId;
|
||||
|
||||
//persist profile data to firebase
|
||||
var updateObj = {}
|
||||
var updated = false;
|
||||
await database.ref(`/profiles/${tagferId}`).once('value').then(function(snapshot) {
|
||||
var profiles = snapshot.val();
|
||||
if (profiles) {
|
||||
//check the profile slots available
|
||||
if(!profiles.profile2) {
|
||||
profiles.profile2 = profileObj
|
||||
updated = true
|
||||
} else if (!profiles.profile3) {
|
||||
profiles.profile3 = profileObj
|
||||
updated = true
|
||||
} else if (!profiles.profile4) {
|
||||
profiles.profile4 = profileObj
|
||||
if (!tagferId) {
|
||||
return {result: false, error: 'Unauthorized access into api'}
|
||||
}
|
||||
|
||||
//persist profile data to firebase
|
||||
var updateObj = {}
|
||||
var updated = false;
|
||||
var profiles = null;
|
||||
await database.ref(`/profiles/${tagferId}`).once('value').then(function(snapshot) {
|
||||
profiles = snapshot.val();
|
||||
if (!profiles) {
|
||||
profiles = {};
|
||||
profiles.profile1 = profileObj;
|
||||
profiles.profile2 = {name: "Blank"};
|
||||
profiles.profile3 = {name: "Blank"};
|
||||
profiles.profile4 = {name: "Blank"}
|
||||
updated = true
|
||||
}
|
||||
updateObj[`/profiles/${tagferId}`] = profiles
|
||||
})
|
||||
|
||||
updateObj[`/profiles/${tagferId}`] = profiles
|
||||
|
||||
if (updated) {
|
||||
await database.ref().update(updateObj).then( () => {
|
||||
return {result:true};
|
||||
}).catch( (error) => {
|
||||
return {result:false, error};
|
||||
});
|
||||
} else {
|
||||
//create the default profile
|
||||
updateObj[`/profiles/${tagferId}`] = {profile1: profileObj}
|
||||
updated = true
|
||||
return {result:false};
|
||||
}
|
||||
})
|
||||
|
||||
if (updated) {
|
||||
await database.ref().update(updateObj).then( () => {
|
||||
return {result:true};
|
||||
}).catch( (error) => {
|
||||
return {result:false, error};
|
||||
});
|
||||
} else {
|
||||
return {result:false};
|
||||
return {result: false, error: 'Unauthorized access into api'}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a user profile
|
||||
* @param {object} profileObj JSON object containing all data for a profile captured from the frontend
|
||||
* @param {number} profileNumber Number used to identify which profile a user wants to update/add to if the profile slot is empty
|
||||
* @param {string} sessionId SessionId obtained by extracting from authorization header
|
||||
* @returns {Boolean} Boolean result of whether the
|
||||
*/
|
||||
async function updateProfile(profileObj, profileNumber, sessionId) {
|
||||
//get tagfer-id from loki
|
||||
if (sessionId) {
|
||||
const tagferId = auth.getSession(sessionId).tagferId;
|
||||
|
||||
//persist profile data to firebase
|
||||
var updateObj = {}
|
||||
var updated = false;
|
||||
var profiles = null
|
||||
await database.ref(`/profiles/${tagferId}`).once('value').then(function(snapshot) {
|
||||
profiles = snapshot.val();
|
||||
profiles[`profile${profileNumber}`] = profileObj
|
||||
updated = true
|
||||
})
|
||||
updateObj[`/profiles/${tagferId}`] = profiles
|
||||
|
||||
if (updated) {
|
||||
await database.ref().update(updateObj).then( () => {
|
||||
return {result:true};
|
||||
}).catch( (error) => {
|
||||
return {result:false, error};
|
||||
});
|
||||
} else {
|
||||
return {result:false};
|
||||
}
|
||||
} else {
|
||||
return {result: false, error: 'Unauthorized access into api'}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createNewProfile
|
||||
updateProfile,
|
||||
createInitialProfiles
|
||||
};
|
||||
|
||||
@ -2,31 +2,57 @@ const dao = require('./dao');
|
||||
const utils = require('../utils/utils');
|
||||
const errors = require('../../config/errors');
|
||||
const http = require('../../config/http');
|
||||
const _ = require('lodash');
|
||||
|
||||
// Handlers
|
||||
/**
|
||||
* Endpoints: POST profiles/
|
||||
* Creates a new profile for a user based on session stored tagferId
|
||||
* Updates a profile for a user based on session stored tagferId
|
||||
* @param {Object} req
|
||||
* @param {Object} res {result: Boolean} | {error: String}
|
||||
*/
|
||||
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})
|
||||
async function updateUserProfile(req, res) {
|
||||
if ( utils.isBodyValid(() => {
|
||||
return (
|
||||
req.body.title && req.body.title !== 'blank' &&
|
||||
req.body.name && req.body.name !== 'blank' &&
|
||||
req.body.aboutTxt &&
|
||||
req.body.helpTxt &&
|
||||
req.body.needTxt &&
|
||||
req.body.experienceTxt &&
|
||||
req.body.educationTxt &&
|
||||
req.body.companyTxt &&
|
||||
req.body.addressTxt &&
|
||||
req.body.contactNumbers &&
|
||||
req.body.email &&
|
||||
req.body.skills &&
|
||||
req.body.linkedin &&
|
||||
req.body.facebook &&
|
||||
req.body.google &&
|
||||
req.body.twitter
|
||||
);
|
||||
},res) ) {
|
||||
const profileObj = req.body;
|
||||
const sessionId = utils.getSessionId(req, res);
|
||||
var createRes = dao.updateProfile(profileObj, req.params.profileNumber, sessionId).then( (createRes) => {
|
||||
if (createRes.result) {
|
||||
res.status(http.CREATED).json({})
|
||||
} else {
|
||||
res.json({status: http.INTERNAL_SERVER_ERROR, error: errors.MAX_NUMBER_OF_PROFILES_REACHED});
|
||||
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});
|
||||
}
|
||||
}
|
||||
}
|
||||
}).catch( (error) => {
|
||||
res.json({status: http.INTERNAL_SERVER_ERROR, error: "Promise error unknown"})
|
||||
});
|
||||
}).catch( (error) => {
|
||||
res.status(http.INTERNAL_SERVER_ERROR).json({error: "Promise error unknown"})
|
||||
});
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createUserProfile
|
||||
updateUserProfile
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
const appConfig = require('../../config/app.json');
|
||||
const http = require('../../config/http');
|
||||
const errors = require('../../config/errors');
|
||||
const auth = require('../auth/dao');
|
||||
|
||||
/**
|
||||
* Verifies if the request is valid by checking if the request has the right app secret.
|
||||
@ -19,6 +20,17 @@ function isAppSecretValid(req, res) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getSessionId(req, res) {
|
||||
const usrToken = req.headers.authorization;
|
||||
|
||||
if (usrToken && auth.getSession(usrToken)) {
|
||||
return usrToken;
|
||||
} else {
|
||||
res.status(http.UNAUTHORIZED).json({ error: 'Unauthorized access into api' });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Checks if the body is valid
|
||||
* @param {Function} isValid verifier
|
||||
@ -34,5 +46,6 @@ function isBodyValid(isValid, response) {
|
||||
|
||||
module.exports = {
|
||||
isAppSecretValid,
|
||||
isBodyValid
|
||||
isBodyValid,
|
||||
getSessionId
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user