Skip to content

Commit

Permalink
feat(general): join requests filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Mar 21, 2020
1 parent 9563f90 commit 218a2a1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ module.exports = {
BODY: ['code', 'name'],
CAMPAIGN: ['name', 'url', 'description_short', 'description_long'],
CIRCLE: ['name', 'description'],
JOIN_REQUEST: [
'$user.first_name$',
'$user.last_name$',
'$user.email$',
]
},
TOKEN_LENGTH: {
MAIL_CONFIRMATION: 128,
Expand Down
11 changes: 8 additions & 3 deletions middlewares/join-requests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { JoinRequest, BodyMembership } = require('../models');
const { JoinRequest, BodyMembership, User } = require('../models');
const errors = require('../lib/errors');
const helpers = require('../lib/helpers');
const constants = require('../lib/constants');
const { sequelize } = require('../lib/sequelize');

exports.listAllJoinRequests = async (req, res) => {
Expand All @@ -9,9 +10,13 @@ exports.listAllJoinRequests = async (req, res) => {
}

const result = await JoinRequest.findAndCountAll({
where: { body_id: req.currentBody.id },
where: {
...helpers.filterBy(req.query.query, constants.FIELDS_TO_QUERY.JOIN_REQUEST),
body_id: req.currentBody.id,
},
...helpers.getPagination(req.query),
order: helpers.getSorting(req.query)
order: helpers.getSorting(req.query),
include: [User]
});

return res.json({
Expand Down
90 changes: 90 additions & 0 deletions test/api/join-requests-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,94 @@ describe('Join requests list', () => {
expect(res.body).toHaveProperty('message');
expect(res.body).not.toHaveProperty('data');
});

test('should find by user first name', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody();


const user1 = await generator.createUser({ first_name: 'aaa', last_name: 'zzz', email: 'zzz@test.io' });
const joinRequest = await generator.createJoinRequest(body, user1);

const user2 = await generator.createUser({ first_name: 'bbb', last_name: 'zzz', email: 'zzz2@test.io' });
await generator.createJoinRequest(body, user2);

await generator.createPermission({ scope: 'global', action: 'view', object: 'join_request' });

const res = await request({
uri: '/bodies/' + body.id + '/join-requests?query=aaa',
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');

expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(joinRequest.id);
});

test('should find by user last name', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody();


const user1 = await generator.createUser({ first_name: 'zzz', last_name: 'aaa', email: 'zzz@test.io' });
const joinRequest = await generator.createJoinRequest(body, user1);

const user2 = await generator.createUser({ first_name: 'zzz', last_name: 'bbb', email: 'zzz2@test.io' });
await generator.createJoinRequest(body, user2);

await generator.createPermission({ scope: 'global', action: 'view', object: 'join_request' });

const res = await request({
uri: '/bodies/' + body.id + '/join-requests?query=aaa',
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');

expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(joinRequest.id);
});

test('should find by user email', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody();


const user1 = await generator.createUser({ first_name: 'zzz', last_name: 'zzz', email: 'aaa@test.io' });
const joinRequest = await generator.createJoinRequest(body, user1);

const user2 = await generator.createUser({ first_name: 'zzz', last_name: 'zzz', email: 'bbb@test.io' });
await generator.createJoinRequest(body, user2);

await generator.createPermission({ scope: 'global', action: 'view', object: 'join_request' });

const res = await request({
uri: '/bodies/' + body.id + '/join-requests?query=aaa',
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');

expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(joinRequest.id);
});
});

0 comments on commit 218a2a1

Please sign in to comment.