From 7a6dfbcb4a65a58ed48cd1d7e1d922dde24f7e9b Mon Sep 17 00:00:00 2001 From: David Jay Date: Fri, 18 Nov 2016 14:13:42 -0500 Subject: [PATCH] Returning public user info. --- models/user.js | 12 +++++++++++- tests/models/user.js | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/models/user.js b/models/user.js index 83677e12c..bde2363c2 100644 --- a/models/user.js +++ b/models/user.js @@ -402,7 +402,7 @@ UserService.findById = (id) => { }; /** - * Finds users in an array of idd. + * Finds users in an array of ids. * @param {Array} ids array of user identifiers (uuid) */ UserService.findByIdArray = (ids) => { @@ -411,6 +411,16 @@ UserService.findByIdArray = (ids) => { }); }; +/** + * Finds public user information by an array of ids. + * @param {Array} ids array of user identifiers (uuid) +*/ +UserService.findPublicByIdArray = (ids) => { + return UserModel.find({ + 'id': {$in: ids} + }, 'id displayName'); +}; + /** * Creates a JWT from a user email. Only works for local accounts. * @param {String} email of the local user diff --git a/tests/models/user.js b/tests/models/user.js index 056aafeed..16b78b708 100644 --- a/tests/models/user.js +++ b/tests/models/user.js @@ -43,6 +43,23 @@ describe('User: models', () => { }); }); + describe('#findPublicByIdArray()', () => { + it('should find an array of users from an array of ids', () => { + const ids = mockUsers.map((user) => user.id); + return User.findPublicByIdArray(ids).then((result) => { + expect(result).to.have.length(3); + const sorted = result.sort((a, b) => { + if(a.displayName < b.displayName) {return -1;} + if(a.displayName > b.displayName) {return 1;} + return 0; + }); + console.log(sorted); + expect(sorted[0]).to.have.property('displayName') + .and.to.equal('Marvel'); + }); + }); + }); + describe('#findLocalUser', () => { it('should find a user when we give the right credentials', () => {