Skip to content

Commit

Permalink
fix(boards): add endpoint to get most recently elected boards (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonVreling authored Sep 15, 2024
1 parent ae26340 commit e399a72
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
27 changes: 26 additions & 1 deletion lib/boards.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Op } = require('sequelize');
const { Board } = require('../models');
const errors = require('./errors');
const helpers = require('./helpers');
const { sequelize } = require('./sequelize');
const { Sequelize, sequelize } = require('./sequelize');
const core = require('./core');
const mailer = require('./mailer');
const config = require('../config');
Expand Down Expand Up @@ -63,6 +63,31 @@ exports.listAllBoards = async (req, res) => {
});
};

exports.listMostRecentBoardsElected = async (req, res) => {
if (!req.permissions.view_board) {
return errors.makeForbiddenError(res, 'You are not allowed to list recently elected boards.');
}

let endDate = moment().endOf('day').toDate();
if (req.query.ends) endDate = moment(req.query.ends, 'YYYY-MM-DD').endOf('day').toDate();

const boards = await Board.findAll({
where: {
elected_date: { [Op.lte]: endDate },
},
group: 'body_id',
attributes: [
'body_id',
[Sequelize.fn('MAX', Sequelize.col('elected_date')), 'latest_election']
]
});

return res.json({
success: true,
data: boards
});
};

exports.listAllBoardsBody = async (req, res) => {
if (!req.permissions.view_board) {
return errors.makeForbiddenError(res, 'You are not allowed to list all boards of this body.');
Expand Down
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ GeneralRouter.get('/metrics/requests', endpointsMetrics.getEndpointMetrics);
GeneralRouter.use(middlewares.authenticateUser);

GeneralRouter.get('/boards', boards.listAllBoards);
GeneralRouter.get('/bodies/:body_id', boards.listAllBoardsBody);
GeneralRouter.get('/boards/recents', boards.listMostRecentBoardsElected);
GeneralRouter.get('/bodies/:body_id/boards', boards.listAllBoardsBody);
GeneralRouter.post('/bodies/:body_id/boards', boards.createBoard);
GeneralRouter.get('/bodies/:body_id/boards/current', boards.listCurrentBoardBody);
GeneralRouter.get('/bodies/:body_id/boards/:board_id', boards.findBoard, boards.getBoard);
Expand Down
80 changes: 77 additions & 3 deletions test/api/board-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ describe('Board listing', () => {
expect(res.body.success).toEqual(false);
});

test('should fail for most recent elected board of all bodies if no permission', async () => {
mock.mockAll({ mainPermissions: { noPermissions: true } });

const res = await request({
uri: '/boards/recents',
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});

expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
});

test('should fail for all boards of body if no permission', async () => {
mock.mockAll({ mainPermissions: { noPermissions: true } });

const res = await request({
uri: '/bodies/1/boards',
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});

expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
});

test('should fail for the current board of body if no permission', async () => {
mock.mockAll({ mainPermissions: { noPermissions: true } });

Expand Down Expand Up @@ -85,7 +111,7 @@ describe('Board listing', () => {
await generator.createBoard({ body_id: 2 });

const res = await request({
uri: '/bodies/1',
uri: '/bodies/1/boards',
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});
Expand All @@ -98,7 +124,7 @@ describe('Board listing', () => {

test('should fail if body_id is not a number', async () => {
const res = await request({
uri: 'bodies/NaN',
uri: 'bodies/NaN/boards',
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});
Expand Down Expand Up @@ -225,7 +251,7 @@ describe('Board listing', () => {
await generator.createBoard({ body_id: 2 });

const res = await request({
uri: '/bodies/1?sort=start_date&direction=desc',
uri: '/bodies/1/boards?sort=start_date&direction=desc',
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});
Expand All @@ -239,4 +265,52 @@ describe('Board listing', () => {
expect(res.body.data[1].id).toEqual(second.id);
expect(res.body.data[2].id).toEqual(third.id);
});

test('should list only most recently elected board', async () => {
await generator.createBoard({
body_id: 1,
elected_date: moment().subtract(2, 'weeks').toDate()
});
const mostRecentBoard = await generator.createBoard({
body_id: 1,
elected_date: moment().subtract(1, 'weeks').toDate()
});

const res = await request({
uri: '/boards/recents',
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].latest_election).toEqual(mostRecentBoard.elected_date);
});

test('should not list boards elected in the future', async () => {
const formerBoard = await generator.createBoard({
body_id: 1,
elected_date: moment().subtract(2, 'weeks').toDate()
});
await generator.createBoard({
body_id: 1,
elected_date: moment().toDate()
});

const ends = moment().subtract(1, 'weeks').toISOString();

const res = await request({
uri: '/boards/recents?ends=' + ends,
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].latest_election).toEqual(formerBoard.elected_date);
});
});

0 comments on commit e399a72

Please sign in to comment.