diff --git a/server/api/collections.js b/server/api/collections.js index 6e8f0e0f17..5095094dbf 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -1,6 +1,7 @@ // @flow import fs from 'fs'; import Router from 'koa-router'; +import { Op } from '../sequelize'; import auth from '../middlewares/authentication'; import pagination from './middlewares/pagination'; import { @@ -145,16 +146,28 @@ router.post('collections.users', auth(), async ctx => { }); router.post('collections.memberships', auth(), pagination(), async ctx => { - const { id } = ctx.body; + const { id, query } = ctx.body; ctx.assertUuid(id, 'id is required'); const collection = await Collection.findByPk(id); authorize(ctx.state.user, 'read', collection); + const where = { + collectionId: id, + }; + + let userWhere; + + if (query) { + userWhere = { + name: { + [Op.iLike]: `%${query}%`, + }, + }; + } + const memberships = await CollectionUser.findAll({ - where: { - collectionId: id, - }, + where, order: [['createdAt', 'DESC']], offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit, @@ -162,6 +175,8 @@ router.post('collections.memberships', auth(), pagination(), async ctx => { { model: User, as: 'user', + where: userWhere, + required: true, }, ], }); diff --git a/server/api/collections.test.js b/server/api/collections.test.js index 94621cec16..974f126d90 100644 --- a/server/api/collections.test.js +++ b/server/api/collections.test.js @@ -330,6 +330,36 @@ describe('#collections.memberships', async () => { expect(body.data.memberships[0].permission).toEqual('read_write'); }); + it('should allow filtering members in collection', async () => { + const { collection, user } = await seed(); + const user2 = await buildUser({ name: "Won't find" }); + await CollectionUser.create({ + createdById: user.id, + collectionId: collection.id, + userId: user.id, + permission: 'read_write', + }); + await CollectionUser.create({ + createdById: user2.id, + collectionId: collection.id, + userId: user2.id, + permission: 'read_write', + }); + + const res = await server.post('/api/collections.memberships', { + body: { + token: user.getJwtToken(), + id: collection.id, + query: user.name.slice(0, 3), + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.users.length).toEqual(1); + expect(body.data.users[0].id).toEqual(user.id); + }); + it('should require authentication', async () => { const res = await server.post('/api/collections.memberships'); const body = await res.json(); diff --git a/server/pages/developers/Api.js b/server/pages/developers/Api.js index 29ea5e70b2..007b179b17 100644 --- a/server/pages/developers/Api.js +++ b/server/pages/developers/Api.js @@ -239,6 +239,7 @@ export default function Api() { +