From 634df68529f8cf9b139e29e292c6435582a0abf5 Mon Sep 17 00:00:00 2001 From: Omar Date: Sun, 24 Feb 2019 20:46:29 -0800 Subject: [PATCH] Connections + Requests --- src/config/router.js | 8 +++ src/ups/connections/dao.js | 120 ++++++++++++++++++++++++++++++++ src/ups/connections/handlers.js | 92 ++++++++++++++++++++++++ src/ups/profiles/dao.js | 7 +- 4 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 src/ups/connections/dao.js create mode 100644 src/ups/connections/handlers.js diff --git a/src/config/router.js b/src/config/router.js index ac1d5e8..f8ead4e 100644 --- a/src/config/router.js +++ b/src/config/router.js @@ -1,5 +1,6 @@ const AuthHandlers = require('../ups/auth/handlers'); const ProfileHandlers = require('../ups/profiles/handlers'); +const ConnectionsHandlers = require('../ups/connections/handlers'); const NoteHandlers = require('../ups/notes/handlers'); /** @@ -31,6 +32,13 @@ function router(app) { app.put('/notes/me/:tagferId', NoteHandlers.createNote); app.post('/notes/me/:tagferId', NoteHandlers.updateNote); app.delete('/notes/me/:tagferId', NoteHandlers.deleteNote); + + // Connection Endpoints + app.get('/connections/me', ConnectionsHandlers.getAllConnections); + app.get('/connections/me/requests', ConnectionsHandlers.getConnectionRequests); + app.put('/connections/me/:profileN', ConnectionsHandlers.sendConnectionRequest); + app.post('/connections/me/:profileN', ConnectionsHandlers.acceptConnectionRequest); + app.delete('/connections/me', ConnectionsHandlers.removeConnectionRequest); } module.exports = router; \ No newline at end of file diff --git a/src/ups/connections/dao.js b/src/ups/connections/dao.js new file mode 100644 index 0000000..2659b41 --- /dev/null +++ b/src/ups/connections/dao.js @@ -0,0 +1,120 @@ +const admin = require('firebase-admin'); + +const profileDao = require('../profiles/dao'); +const utils = require('../utils/utils'); + +function createConnectionRequest(fromTagferId, fromProfileN, toTagferId) { + const ref = admin.database().ref('requests'); + const updates = {}; + updates[`${fromTagferId}/sent/${toTagferId}`] = fromProfileN; + updates[`${toTagferId}/received/${fromTagferId}`] = fromProfileN; + + return ref.update(updates).catch(utils.dbErrorHandler); +} + +function removeConnectionRequest(fromTagferId, toTagferId) { + const ref = admin.database().ref('requests'); + const updates = {}; + updates[`${fromTagferId}/sent/${toTagferId}`] = null; + updates[`${toTagferId}/received/${fromTagferId}`] = null; + + return ref.update(updates).catch(utils.dbErrorHandler); +} + +function createConnection(fromTagferId, fromProfileN, toTagferId, toProfileN) { + const rootRef = admin.database().ref(); + const updates = {}; + updates[`requests/${fromTagferId}/sent/${toTagferId}`] = null; + updates[`connections/${fromTagferId}/to/${toTagferId}`] = toProfileN; + updates[`requests/${toTagferId}/received/${fromTagferId}`] = null; + updates[`connections/${toTagferId}/to/${fromTagferId}`] = fromProfileN; + + return Promise.all([ + rootRef.update(updates).catch(utils.dbErrorHandler), + updateConnectionCount(fromTagferId), + updateConnectionCount(toTagferId) + ]); +} + +function updateConnectionCount(tagferId) { + const ref = admin.database().ref(`connections/${tagferId}`); + return ref.child('count').once('value').then(count => ref.update({ count: count.val() + 1 })).catch(utils.dbErrorHandler); +} + +function getConnectionCount(tagferId) { + return admin.database().ref(`connections/${tagferId}/count`).once('value') + .then(count => (count.exists()? count.val(): 0)) + .catch(utils.dbErrorHandler); +} + +// Gets sent and received requests as lite profile objects +async function getConnectionRequests(tagferId) { + const requestsData = await admin.database().ref(`requests/${tagferId}`).once('value').catch(utils.dbErrorHandler); + const promises = []; + let rcvdCount = 0; + + requestsData.forEach(requests => { + if (requests.key === 'received') { rcvdCount = requests.numChildren(); } + requests.forEach(request => { promises.push(_connectionToLiteProfile(request)); }); + }); + + const profiles = await Promise.all(promises); + return { + received: profiles.slice(0, rcvdCount), + sent: profiles.slice(rcvdCount) + }; +} + +async function getAllConnections(tagferId) { + const connections = await admin.database().ref(`connections/${tagferId}/to`).once('value').catch(utils.dbErrorHandler); + const profiles = [[], [], [], []]; + connections.forEach(connection => { + profiles[connection.val() - 1].push(_connectionToLiteProfile(connection)); + }); + + return await Promise.all(profiles.map(Promise.all, Promise)); +} + +function getAutoAcceptProfileN(tagferId) { + return admin.database().ref(`connections/${tagferId}/autoAccept`).once('value') + .then(profileN => (profileN.exists() ? profileN.val() : 0 )) + .catch(utils.dbErrorHandler); +} + +function getConnectionProfileN(fromTagferId, toTagferId) { + return admin.database().ref(`connections/${fromTagferId}/to/${toTagferId}`).once('value') + .then(profileN => (profileN.exists() ? profileN.val() : 0 )) + .catch(utils.dbErrorHandler); +} + +function deleteConnection(tagferId1, tagfeId2) { + const ref = admin.database().ref('connections'); + const updates = {}; + updates[`${tagferId1}/to/${tagfeId2}`] = null; + updates[`${tagfeId2}/to/${tagferId1}`] = null; + + return ref.update(updates).catch(utils.dbErrorHandler); +} + +function _connectionToLiteProfile(connection) { + const tagferId = connection.key; + const profileN = connection.val(); + + return profileDao.getProfile(1, tagferId).then(profile => { + const litePorfile = profileDao.toLiteProfile(profile, tagferId); + litePorfile.profileN = profileN; + return litePorfile; + }); +} + +module.exports = { + createConnectionRequest, + removeConnectionRequest, + getConnectionRequests, + getAllConnections, + getAutoAcceptProfileN, + getConnectionProfileN, + getConnectionCount, + deleteConnection, + createConnection +}; \ No newline at end of file diff --git a/src/ups/connections/handlers.js b/src/ups/connections/handlers.js new file mode 100644 index 0000000..463b839 --- /dev/null +++ b/src/ups/connections/handlers.js @@ -0,0 +1,92 @@ +const authDao = require('../auth/dao'); +const connDao = require('./dao'); + +const utils = require('../utils/utils'); + +async function getConnectionRequests(req, res) { + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + + try { + const tagferId = authDao.getSession(sessionId).tagferId; + const requests = await connDao.getConnectionRequests(tagferId); + res.send(requests); + } catch (error) { + console.log(error); + res.send({ error }); + } +} + +async function getAllConnections(req, res) { + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + + try { + const tagferId = authDao.getSession(sessionId).tagferId; + const conns = await connDao.getAllConnections(tagferId); + res.send({ + profile1: conns[0], + profile2: conns[1], + profile3: conns[2], + profile4: conns[3] + }); + } catch (error) { + console.log(error); + res.send({ error }); + } +} + +async function sendConnectionRequest(req, res) { + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + const fromProfileN = parseInt(req.params.profileN); + const toTagferId = req.body.toTagferId; + + try { + const fromTagferId = authDao.getSession(sessionId).tagferId; + await connDao.createConnectionRequest(fromTagferId, fromProfileN, toTagferId); + res.send({}); + } catch (error) { + console.log(error); + res.send({ error }); + } +} + +async function removeConnectionRequest(req, res) { + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + + try { + const myTagferId = authDao.getSession(sessionId).tagferId; + const fromTagferId = req.body.fromTagferId || myTagferId; + const toTagferId = req.body.toTagferId || myTagferId; + + await connDao.removeConnectionRequest(fromTagferId, toTagferId); + res.send({}); + } catch (error) { + console.log(error); + res.send({ error }); + } +} + +async function acceptConnectionRequest(req, res) { + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + + try { + // Connection request is accepted by the user is the TO_SIDE, he received a request FROM_SIDE + const toTagferId = authDao.getSession(sessionId).tagferId; + const toProfileN = parseInt(req.params.profileN); + const fromTagferId = req.body.fromTagferId; + const fromProfileN = req.body.fromProfileN; + + await connDao.createConnection(fromTagferId, fromProfileN, toTagferId, toProfileN); + res.send({}); + } catch (error) { + console.log(error); + res.send({ error }); + } +} + +module.exports = { + getAllConnections, + getConnectionRequests, + sendConnectionRequest, + acceptConnectionRequest, + removeConnectionRequest +}; diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 3d07288..be3cb4c 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -26,7 +26,7 @@ async function updateProfile(profileObj, profileN, tagferId) { */ function getProfile(profileN, tagferId) { return database.ref(`/profiles/${tagferId}/profile${profileN}`).once('value') - .then(snapshot => (snapshot.exists() ? snapshot.val() : {})) + .then(snapshot => (snapshot.exists() ? { tagferId, ...snapshot.val() } : {})) .catch(utils.dbErrorHandler); } @@ -50,7 +50,7 @@ async function suggestNProfiles(N) { const list = new Array(data.numChildren()); let index = 0; - data.forEach(profile => { list[index++] = _toLiteProfile(profile.val().profile1, profile.key); }); + data.forEach(profile => { list[index++] = toLiteProfile(profile.val().profile1, profile.key); }); if (index > 0) { lastRetrievedProfile = list[index-1].tagferId; } @@ -90,7 +90,7 @@ async function createInitialProfileObject(profile, tagferId) { }; } -function _toLiteProfile(profile, tagferId) { +function toLiteProfile(profile, tagferId) { const { fullName, experience, photoURL } = profile; const { jobTitle, companyName } = experience; return { tagferId, fullName, jobTitle, companyName, photoURL }; @@ -104,5 +104,6 @@ module.exports = { createInitialProfileObject, updateProfile, getProfile, + toLiteProfile, suggestNProfiles };