feat: Add ability to filter collection.memberships with query

This commit is contained in:
Tom Moor committed 2019-08-26 21:59:38 -07:00
1 parent 0ac596fdb0
commit 8dc7a96a6d
3 files changed
+50 -4

No files matched your search

+19 -4
View File
@@ -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,
},
],
});
+30
View File
@@ -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();
+1
View File
@@ -239,6 +239,7 @@ export default function Api() {
</Description>
<Arguments pagination>
<Argument id="id" description="Collection ID" required />
<Argument id="query" description="Filter by user name" />
</Arguments>
</Method>