diff --git a/src/config/app.json b/src/config/app.json index 10074dc..8f4b340 100644 --- a/src/config/app.json +++ b/src/config/app.json @@ -31,7 +31,8 @@ } }, "dbPath": { - "users": "users" + "users": "users", + "profiles": "profiles" }, "loki" : { "verificationTTL": 300000, diff --git a/src/config/errors.js b/src/config/errors.js index 8b661f3..2899c2a 100644 --- a/src/config/errors.js +++ b/src/config/errors.js @@ -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', }; \ No newline at end of file diff --git a/src/config/router.js b/src/config/router.js index 5121338..d963df1 100644 --- a/src/config/router.js +++ b/src/config/router.js @@ -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; \ No newline at end of file diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js new file mode 100644 index 0000000..f9391e2 --- /dev/null +++ b/src/ups/profiles/dao.js @@ -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 +}; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js new file mode 100644 index 0000000..e7cd336 --- /dev/null +++ b/src/ups/profiles/handlers.js @@ -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 +} \ No newline at end of file