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 events #1242

Merged
merged 3 commits into from
Sep 15, 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
20 changes: 20 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ exports.listEvents = async (req, res) => {
});
};

exports.listMostRecentEvents = async (req, res) => {
const queryObj = {
where: { status: 'published' },
group: 'body_id',
attributes: [
'body_id',
[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
});
};

exports.displayEvent = async (req, res) => {
req.event.permissions = req.permissions;
const event = req.event.toJSON();
Expand Down
2 changes: 2 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ GeneralRouter.post('/', events.addEvent);
GeneralRouter.get('/tasks', middlewares.getTasksList);
GeneralRouter.get('/mine', events.listUserAppliedEvents);

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

PaxLimitsRouter.use(middlewares.authenticateUser, middlewares.ensureAuthorized, paxLimits.checkEventType);
PaxLimitsRouter.get('/:body_id', paxLimits.getSingleLimit);
PaxLimitsRouter.delete('/:body_id', paxLimits.deleteSingleLimit);
Expand Down
96 changes: 96 additions & 0 deletions test/api/events-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,100 @@ describe('Events listing', () => {
expect(ids).toContain(first.id);
expect(ids).toContain(second.id);
});

test('should list most recent events per body', async () => {
await generator.createEvent({
status: 'published',
body_id: 1,
application_period_starts: moment().subtract(20, 'days').toDate(),
application_period_ends: moment().subtract(19, 'days').toDate(),
board_approve_deadline: moment().subtract(18, 'days').toDate(),
participants_list_publish_deadline: moment().subtract(17, 'days').toDate(),
memberslist_submission_deadline: moment().subtract(16, 'days').toDate(),
draft_proposal_deadline: moment().subtract(15, 'days').toDate(),
final_proposal_deadline: moment().subtract(14, 'days').toDate(),
candidature_deadline: moment().subtract(13, 'days').toDate(),
booklet_publication_deadline: moment().subtract(12, 'days').toDate(),
updated_booklet_publication_deadline: moment().subtract(11, 'days').toDate(),
starts: moment().subtract(8, 'days').toDate(),
ends: moment().subtract(7, 'days').toDate(),
});
const mostRecentEvent = await generator.createEvent({
status: 'published',
body_id: 1,
application_period_starts: moment().subtract(20, 'days').toDate(),
application_period_ends: moment().subtract(19, 'days').toDate(),
board_approve_deadline: moment().subtract(18, 'days').toDate(),
participants_list_publish_deadline: moment().subtract(17, 'days').toDate(),
memberslist_submission_deadline: moment().subtract(16, 'days').toDate(),
draft_proposal_deadline: moment().subtract(15, 'days').toDate(),
final_proposal_deadline: moment().subtract(14, 'days').toDate(),
candidature_deadline: moment().subtract(13, 'days').toDate(),
booklet_publication_deadline: moment().subtract(12, 'days').toDate(),
updated_booklet_publication_deadline: moment().subtract(11, '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',
body_id: 1,
application_period_starts: moment().subtract(40, 'days').toDate(),
application_period_ends: moment().subtract(39, 'days').toDate(),
board_approve_deadline: moment().subtract(38, 'days').toDate(),
participants_list_publish_deadline: moment().subtract(37, 'days').toDate(),
memberslist_submission_deadline: moment().subtract(36, 'days').toDate(),
draft_proposal_deadline: moment().subtract(35, 'days').toDate(),
final_proposal_deadline: moment().subtract(34, 'days').toDate(),
candidature_deadline: moment().subtract(33, 'days').toDate(),
booklet_publication_deadline: moment().subtract(32, 'days').toDate(),
updated_booklet_publication_deadline: moment().subtract(31, 'days').toDate(),
starts: moment().subtract(18, 'days').toDate(),
ends: moment().subtract(17, 'days').toDate(),
});
await generator.createEvent({
status: 'published',
body_id: 1,
application_period_starts: moment().subtract(20, 'days').toDate(),
application_period_ends: moment().subtract(19, 'days').toDate(),
board_approve_deadline: moment().subtract(18, 'days').toDate(),
participants_list_publish_deadline: moment().subtract(17, 'days').toDate(),
memberslist_submission_deadline: moment().subtract(16, 'days').toDate(),
draft_proposal_deadline: moment().subtract(15, 'days').toDate(),
final_proposal_deadline: moment().subtract(14, 'days').toDate(),
candidature_deadline: moment().subtract(13, 'days').toDate(),
booklet_publication_deadline: moment().subtract(12, 'days').toDate(),
updated_booklet_publication_deadline: moment().subtract(11, '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());
});
});