Skip to content

Commit

Permalink
fix(events): filter events by date. Fixes MEMB-689
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Nov 7, 2019
1 parent 16ef1bc commit 5925052
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
11 changes: 7 additions & 4 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ exports.getDefaultQuery = (req) => {
queryObj.where.type = Array.isArray(req.query.type) ? { [Sequelize.Op.in]: req.query.type } : req.query.type;
}

// If displayPast === true, display also past events.
if (req.query.displayPast !== true) {
queryObj.where.starts = { [Sequelize.Op.gte]: new Date() };
}
// Filtering by event start and end dates.
// The events are not inclusive, so when the event starts on 2018-01-02 and ends on 2018-01-17, querying
// from 2018-01-05 to 2018-01-10 won't return it.
const dateQuery = [];
if (req.query.starts) dateQuery.push({ starts: { [Sequelize.Op.gte]: moment(req.query.starts, 'YYYY-MM-DD').startOf('day').toDate() } });
if (req.query.ends) dateQuery.push({ ends: { [Sequelize.Op.lte]: moment(req.query.ends, 'YYYY-MM-DD').endOf('day').toDate() } });
queryObj.where[Sequelize.Op.and] = dateQuery;

// If offset is set and is valid, use it.
if (req.query.offset) {
Expand Down
33 changes: 25 additions & 8 deletions test/api/events-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('Events listing', () => {
expect(res.body.data[0].id).toEqual(event.id);
});

it('should not display past events if displayPast=false', async () => {
it('should filter by start date', async () => {
await generator.createEvent({
status: 'published',
application_starts: moment().subtract(5, 'week').toDate(),
Expand All @@ -231,8 +231,16 @@ describe('Events listing', () => {
ends: moment().subtract(2, 'week').toDate(),
});

const event = await generator.createEvent({
status: 'published',
application_starts: moment().add(1, 'week').toDate(),
application_ends: moment().add(2, 'week').toDate(),
starts: moment().add(3, 'week').toDate(),
ends: moment().add(4, 'week').toDate(),
});

const res = await request({
uri: '/?displayPast=false',
uri: '/?starts=' + moment().format('YYYY-MM-DD'),
method: 'GET',
headers: { 'X-Auth-Token': 'blablabla' }
});
Expand All @@ -241,30 +249,39 @@ describe('Events listing', () => {
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');

expect(res.body.data.length).toEqual(0);
expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(event.id);
});

it('should display past events if displayPast=true', async () => {
it('should filter by end date', async () => {
await generator.createEvent({
status: 'published',
application_starts: moment().add(1, 'week').toDate(),
application_ends: moment().add(2, 'week').toDate(),
starts: moment().add(3, 'week').toDate(),
ends: moment().add(4, 'week').toDate(),
});

const event = await generator.createEvent({
status: 'published',
application_starts: moment().subtract(5, 'week').toDate(),
application_ends: moment().subtract(4, 'week').toDate(),
starts: moment().subtract(3, 'week').toDate(),
ends: moment().subtract(2, 'week').toDate(),
status: 'published'
});

const res = await request({
uri: '/?displayPast=true',
uri: '/?ends=' + moment().format('YYYY-MM-DD'),
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).not.toEqual(0);
expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(event.id);
});

it('should filter by name case-insensitive', async () => {
Expand Down

0 comments on commit 5925052

Please sign in to comment.