Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AC check): Add endpoint to get most recent event of a body #1074

Merged
merged 12 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
});
});