mirror of
https://bitbucket.org/tagfer_team/tagfer-server.git
synced 2025-12-25 03:37:38 +00:00
T-954157790482088: User can create up to 4 profiles
- using backend validation to ensure no more than four profiles created -
This commit is contained in:
parent
b9a38a3d96
commit
cd0100f1a4
@ -31,7 +31,8 @@
|
||||
}
|
||||
},
|
||||
"dbPath": {
|
||||
"users": "users"
|
||||
"users": "users",
|
||||
"profiles": "profiles"
|
||||
},
|
||||
"loki" : {
|
||||
"verificationTTL": 300000,
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
module.exports = {
|
||||
//Auth erors
|
||||
AUTH_INVALID_EMAIL: 'auth/invalid-email',
|
||||
AUTH_INVALID_PHONE_NUMBER: 'auth/invalid-phone-number',
|
||||
AUTH_USER_NOT_FOUND: 'auth/user-not-found',
|
||||
@ -8,8 +9,12 @@ module.exports = {
|
||||
AUTH_PHONE_ALREADY_EXISTS: 'auth/phone-number-already-exists',
|
||||
AUTH_PHONE_NOT_CACHED: 'auth/phone-number-not-cached',
|
||||
AUTH_VERIFICATION_CODE_MISMATCH: 'auth/phone-verification-code-mismatch',
|
||||
//App Errors
|
||||
APP_NETWORK_ERROR: 'app/network-error',
|
||||
APP_NETWORK_TIMEOUT: 'app/network-timeout',
|
||||
APP_UNABLE_TO_PARSE_RESPONSE: 'app/unable-to-parse-response',
|
||||
MISSING_BODY_ATTRIBUTES: 'request/missing-body-attributes'
|
||||
//Request Errors
|
||||
MISSING_BODY_ATTRIBUTES: 'request/missing-body-attributes',
|
||||
//Profile Errors
|
||||
MAX_NUMBER_OF_PROFILES_REACHED: 'profile/max-number-of-profiles-are-being-used',
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
const AuthHandlers = require('../ups/auth/handlers');
|
||||
const UserHandlers = require('../ups/users/handlers');
|
||||
const ProfileHandlers = require('../ups/profiles/handlers');
|
||||
|
||||
/**
|
||||
* Main router for our application, include all routes here. No logic should be added in this file.
|
||||
@ -17,6 +18,9 @@ function router(app) {
|
||||
|
||||
// Users Endpoints
|
||||
app.get('/users/by/phone', UserHandlers.findNetworkByPhone);
|
||||
|
||||
// Profile Endpoints
|
||||
app.post('/profiles/', ProfileHandlers.createUserProfile);
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
52
src/ups/profiles/dao.js
Normal file
52
src/ups/profiles/dao.js
Normal file
@ -0,0 +1,52 @@
|
||||
const database = require('firebase').database();
|
||||
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 {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
|
||||
*/
|
||||
async function createNewProfile(profileObj) {
|
||||
//get tagfer-id from loki
|
||||
const sessions = loki.getCollection("sessions");
|
||||
const primarySession = sessions.get(1);
|
||||
const tagferId = primarySession.tagferId;
|
||||
|
||||
//persist profile data to firebase
|
||||
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) {
|
||||
profiles.profile2 = profileObj
|
||||
updated = true
|
||||
} else if (!profiles.profile3) {
|
||||
profiles.profile3 = profileObj
|
||||
updated = true
|
||||
} else if (!profiles4) {
|
||||
profiles.profile4 = profileObj
|
||||
updated = true
|
||||
}
|
||||
updateObj[`/profiles/${tagferId}`] = profiles
|
||||
|
||||
} else {
|
||||
//create the default profile
|
||||
updateObj[`/profiles/${tagferId}`] = {profile1: profileObj}
|
||||
updated = true
|
||||
}
|
||||
})
|
||||
|
||||
return updated ? database.ref().update(updates) : null;
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createNewProfile
|
||||
};
|
||||
25
src/ups/profiles/handlers.js
Normal file
25
src/ups/profiles/handlers.js
Normal file
@ -0,0 +1,25 @@
|
||||
const dao = require('./dao');
|
||||
const utils = require('../utils/utils');
|
||||
const errors = require('../../config/errors');
|
||||
|
||||
// Handlers
|
||||
/**
|
||||
* Endpoints: POST profiles/
|
||||
* Creates a new profile for a user based on session stored tagferId
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createUserProfile
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user