From 3cc469aa5c4fd73794699af6d837e9f7522e7c28 Mon Sep 17 00:00:00 2001 From: Leon Vreling <49942406+LeonVreling@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:37:18 +0200 Subject: [PATCH] feat(AC check): Add endpoint to get most recent events of a body (#1074) * 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 --- lib/events.js | 25 +++++++++++++ lib/server.js | 2 ++ test/api/events-listing.test.js | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/lib/events.js b/lib/events.js index 1c88e973..7b675dec 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1,3 +1,5 @@ +const moment = require('moment'); + const errors = require('./errors'); const merge = require('./merge'); const constants = require('./constants'); @@ -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); diff --git a/lib/server.js b/lib/server.js index 07cfbcb7..e5bd027d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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); diff --git a/test/api/events-listing.test.js b/test/api/events-listing.test.js index d0ae0276..21f44aa7 100644 --- a/test/api/events-listing.test.js +++ b/test/api/events-listing.test.js @@ -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()); + }); });