diff --git a/src/config/app.json b/src/config/app.json index ba3e76a..15341e0 100644 --- a/src/config/app.json +++ b/src/config/app.json @@ -58,6 +58,6 @@ "verificationTTL": 300000, "verificationTTLClear": 1800000 }, - "defaultProfileName": "Business Profile" - + "defaultProfileName": "Business Profile", + "suggestedUsersCount": 20 } \ No newline at end of file diff --git a/src/config/errors.js b/src/config/errors.js index f55d172..94bc884 100644 --- a/src/config/errors.js +++ b/src/config/errors.js @@ -10,6 +10,7 @@ module.exports = { AUTH_PHONE_NOT_CACHED: 'auth/phone-number-not-cached', AUTH_VERIFICATION_CODE_MISMATCH: 'auth/phone-verification-code-mismatch', AUTH_INVALID_SESSION_ID: 'auth/invalid-session-id', + AUTH_INVALID_PAGE_TOKEN: 'auth/invalid-page-token', AUTH_UNAUTHORIZED_ACCESS: 'auth/unauthorized-access-into-api', AUTH_UNABLE_TO_EXTRACT_SESSION_ID_FROM_AUTH_HEADER: 'auth/unable-to-extract-session-id-from-auth-header', AUTH_TWITTER_REQUEST_TOKEN_FAILURE: 'auth/twitter-request-token-failure', diff --git a/src/config/router.js b/src/config/router.js index 6f53907..fee4e3a 100644 --- a/src/config/router.js +++ b/src/config/router.js @@ -19,7 +19,8 @@ function router(app) { app.post('/auth/passwordReset', AuthHandlers.sendPasswordResetEmail); // Users Endpoints - app.post('/users/by/phone', UserHandlers.findNetworkByPhone); + app.post('/users/by/phone', UserHandlers.findUsersByPhone); + app.get('/users/suggest', UserHandlers.suggestUsers); // Profile Endpoints app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile); diff --git a/src/ups/users/dao.js b/src/ups/users/dao.js index 8246d5d..a0a3811 100644 --- a/src/ups/users/dao.js +++ b/src/ups/users/dao.js @@ -34,6 +34,18 @@ async function filterContactsIntoBatches(contacts) { return {inNetworkBatch, outNetworkBatch, failedBatch}; } +var pageToken = undefined; +/** + * Returns a list of N users. Used to suggest contacts. + * @param {Number} N is the number of contacts you want to get + */ +function getNextNUsers(N) { + return admin.auth().listUsers(N, pageToken).then( result => { + pageToken = !result.pageToken? undefined : result.pageToken; + return result.users.length === 0? getNextNUsers(N): result.users; + }).catch( error => { throw { error: error.code }; }); +} + function _getContactStatus(phoneNumber) { return admin.auth().getUserByPhoneNumber(phoneNumber).then( user => { return {inNetwork: true, tagferId: user.uid}; @@ -43,5 +55,6 @@ function _getContactStatus(phoneNumber) { } module.exports = { - filterContactsIntoBatches + filterContactsIntoBatches, + getNextNUsers }; \ No newline at end of file diff --git a/src/ups/users/handlers.js b/src/ups/users/handlers.js index 52ce538..1980a4c 100644 --- a/src/ups/users/handlers.js +++ b/src/ups/users/handlers.js @@ -2,6 +2,7 @@ const phone = require('phone'); const dao = require('./dao'); const utils = require('../utils/utils'); +const appConfig = require('../../config/app.json'); /** * Endpoint: users/by/phone @@ -10,7 +11,7 @@ const utils = require('../utils/utils'); * @param {Object} req {contacts: Array} * @param {Object} res {inNetworkBatch : Array, outNetworkBatch: Array, failedBatch: Array } */ -async function findNetworkByPhone(req, res) { +async function findUsersByPhone(req, res) { const contacts = req.body.contacts; const verifier = () => contacts && Array.isArray(contacts); if (!utils.isAppSecretValid(req,res) || !utils.isBodyValid(verifier, res)) { @@ -21,6 +22,25 @@ async function findNetworkByPhone(req, res) { res.json(batches); } +/** + * Endpoint: users/suggest + * @param {Object} req {} + * @param {Object} res { users: UserObject } + * + * UserObject = { tagferId: String, displayName: String, photoURL: String } + */ +function suggestUsers(req, res) { + if (!utils.isAppSecretValid(req,res)) { + return; + } + + dao.getNextNUsers(appConfig.suggestedUsersCount).then( listUsers => { + const users = listUsers.map( user => ({ tagferId: user.uid, displayName: user.displayName, photoURL: user.photoURL }) ); + res.json({ users }); + }).catch(error => res.json({ error }) ); +} + module.exports = { - findNetworkByPhone + findUsersByPhone, + suggestUsers };