Skip to content

Commit

Permalink
feat(AC check): Add endpoint to get most recent events of a body (#1074)
Browse files Browse the repository at this point in the history
* feat(AC check): Add endpoint to most recent events of a body

* feat(AC check): Add tests new endpoint

* fix(AC check): Use event end instead of event start for endpoint
  • Loading branch information
LeonVreling committed Aug 19, 2024
1 parent 130897f commit 3cc469a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const moment = require('moment');

const errors = require('./errors');
const merge = require('./merge');
const constants = require('./constants');
Expand Down Expand Up @@ -33,6 +35,29 @@ exports.listEvents = async (req, res) => {
});
};

exports.listMostRecentEvents = async (req, res) => {
const queryObj = {
where: {
deleted: false,
status: 'published'
},
group: 'organizing_bodies',
attributes: [
'organizing_bodies',
[Sequelize.fn('MAX', Sequelize.col('ends')), 'latest_event']
]
};

if (req.query.ends) queryObj.where[Sequelize.Op.and] = { ends: { [Sequelize.Op.lte]: moment(req.query.ends, 'YYYY-MM-DD').endOf('day').toDate() } };

const events = await Event.findAll(queryObj);

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

// All applications for bodies, including events.
exports.listBodyApplications = async (req, res) => {
const bodyId = Number(req.params.body_id);
Expand Down
2 changes: 2 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ GeneralRouter.get('/mine/participating', middlewares.ensureAuthorized, events.li
GeneralRouter.get('/mine/approvable', middlewares.ensureAuthorized, events.listApprovableEvents);
GeneralRouter.get('/boardview/:body_id', middlewares.ensureAuthorized, events.listBodyApplications);

GeneralRouter.get('/recents', middlewares.ensureAuthorized, events.listMostRecentEvents);

// All requests from here on use the getEvent middleware to fetch a single event from db
EventsRouter.use(middlewares.fetchSingleEvent);

Expand Down
64 changes: 64 additions & 0 deletions test/api/events-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,68 @@ describe('Events listing', () => {
expect(res.body.data[1].id).toEqual(second.id);
expect(res.body.data[2].id).toEqual(third.id);
});

it('should list most recent events per body', async () => {
await generator.createEvent({
status: 'published',
organizing_bodies: [{ body_id: 1, body_name: 'blabla' }],
application_starts: moment().subtract(10, 'days').toDate(),
application_ends: moment().subtract(9, 'days').toDate(),
starts: moment().subtract(8, 'days').toDate(),
ends: moment().subtract(7, 'days').toDate(),
});
const mostRecentEvent = await generator.createEvent({
status: 'published',
organizing_bodies: [{ body_id: 1, body_name: 'blabla' }],
application_starts: moment().subtract(5, 'days').toDate(),
application_ends: moment().subtract(4, 'days').toDate(),
starts: moment().subtract(3, 'days').toDate(),
ends: moment().subtract(2, 'days').toDate(),
});

const res = await request({
uri: '/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_event).toEqual(mostRecentEvent.ends.toISOString());
});

it('should not list most recent events in the future', async () => {
const previousEvent = await generator.createEvent({
status: 'published',
organizing_bodies: [{ body_id: 1, body_name: 'blabla' }],
application_starts: moment().subtract(20, 'days').toDate(),
application_ends: moment().subtract(19, 'days').toDate(),
starts: moment().subtract(18, 'days').toDate(),
ends: moment().subtract(17, 'days').toDate(),
});
await generator.createEvent({
status: 'published',
organizing_bodies: [{ body_id: 1, body_name: 'blabla' }],
application_starts: moment().subtract(5, 'days').toDate(),
application_ends: moment().subtract(4, 'days').toDate(),
starts: moment().subtract(3, 'days').toDate(),
ends: moment().subtract(2, 'days').toDate(),
});

const ends = moment().subtract(10, 'days').toISOString();

const res = await request({
uri: '/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_event).toEqual(previousEvent.ends.toISOString());
});
});

0 comments on commit 3cc469a

Please sign in to comment.