diff --git a/server/organizations.js b/server/organizations.js index a9a09f34..a47c82f1 100644 --- a/server/organizations.js +++ b/server/organizations.js @@ -8,10 +8,11 @@ router.get('', jwt.optionalJwtMiddleware, async function(req, res, next) { let params = {} if (req.query) { if (req.query['ids']) params.ids = req.query['ids'].split(',') - if (req.query['is-member'] && req.user) params['has-user'] = req.user.id + if (req.query['is-member'] && req.query['is-member'] === 'true' && req.user) params['has-user'] = req.user.id + if (req.query.q) params.q = req.query.q } const organizations = req.user ? await req.app.get('storage').findOrganizations(params) : {results: [], count: 0} - organizations.results = organizations.results.map(user => ({id: user.id, name: user.firstName + ' ' + user.lastName})) + organizations.results = organizations.results.map(organization => ({id: organization.id, name: organization.name})) res.json(organizations) }) diff --git a/server/storages/file.js b/server/storages/file.js index ee6b1c6e..6a6bf2a5 100644 --- a/server/storages/file.js +++ b/server/storages/file.js @@ -16,6 +16,10 @@ module.exports = async function(params) { if (params.ids) { filteredUsers = filteredUsers.filter(user => (params.ids).find(id => user.id === id)) } + if (params.q) { + const lq = params.q.toLowerCase() + filteredUsers = filteredUsers.filter(user => user.firstName.toLowerCase().indexOf(lq) >= 0 || user.lastName.toLowerCase().indexOf(lq) >= 0) + } return { results: filteredUsers, count: filteredUsers.length @@ -36,6 +40,10 @@ module.exports = async function(params) { if (params['has-user']) { filteredOrganizations = filteredOrganizations.filter(organization => organization.members.find(member => member.id === params['has-user'])) } + if (params.q) { + const lq = params.q.toLowerCase() + filteredOrganizations = filteredOrganizations.filter(organization => organization.name.toLowerCase().indexOf(lq) >= 0) + } return { results: filteredOrganizations, count: filteredOrganizations.length diff --git a/server/users.js b/server/users.js index 9ebd8f1d..20158005 100644 --- a/server/users.js +++ b/server/users.js @@ -6,8 +6,9 @@ let router = express.Router() // Get the list of users router.get('', jwt.optionalJwtMiddleware, async function(req, res, next) { let params = {} - if (req.query && req.query['ids']) { - params.ids = req.query['ids'].split(',') + if (req.query) { + if (req.query['ids']) params.ids = req.query['ids'].split(',') + if (req.query.q) params.q = req.query.q } const users = req.user ? await req.app.get('storage').findUsers(params) : {results: [], count: 0} users.results = users.results.map(user => ({id: user.id, name: user.firstName + ' ' + user.lastName})) diff --git a/test/organizations.js b/test/organizations.js index 4923168f..c8f7c052 100644 --- a/test/organizations.js +++ b/test/organizations.js @@ -16,6 +16,8 @@ test('Get organization list when authenticated', async t => { t.is(res.data.count, 6) res = await ax.get('/api/organizations?is-member=true') t.is(res.data.count, 2) + res = await ax.get('/api/organizations?q=li') + t.is(res.data.count, 2) }) test('Get organization roles', async t => { diff --git a/test/users.js b/test/users.js index ced473f8..e5bb1607 100644 --- a/test/users.js +++ b/test/users.js @@ -16,6 +16,13 @@ test('Get user list when authenticated', async t => { t.is(res.data.count, 10) }) +test('Get filter user list when authenticated', async t => { + const ax = await testUtils.axios(__filename, 'dmeadus0@answers.com') + const res = await ax.get('/api/users?q=Al') + t.is(res.status, 200) + t.is(res.data.count, 2) +}) + test('Get user info when not authenticated', async t => { const ax = await testUtils.axios(__filename) try {