Skip to content

Commit

Permalink
feat: text search in lists
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-bonnel committed Jan 4, 2018
1 parent 53eabf2 commit 75cfdb4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions server/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
8 changes: 8 additions & 0 deletions server/storages/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions server/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand Down
2 changes: 2 additions & 0 deletions test/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
7 changes: 7 additions & 0 deletions test/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '[email protected]')
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 {
Expand Down

0 comments on commit 75cfdb4

Please sign in to comment.