Skip to content

Commit

Permalink
feat: improve events route
Browse files Browse the repository at this point in the history
add the possibility to filter events by minimum creation date
  • Loading branch information
rafaelcamargo committed Feb 16, 2024
1 parent eeeb15f commit 26622d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/controllers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@ _public.get = (req, res) => handleTransaction(
res
);

function buildFilter({ slug, minDate }){
function buildFilter({ slug, minDate, minCreationDate }){
return {
where: {
slug,
date: {
gte: minDate
},
created_at: {
gte: buildIsoDateString(minCreationDate)
}
}
};
}

function buildIsoDateString(dashedDateString){
if(dashedDateString) {
const [year, month, day] = dashedDateString.split('-').map(value => parseInt(value));
return new Date(year, month - 1, day).toISOString();
}
}

module.exports = _public;
36 changes: 36 additions & 0 deletions src/routes/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,40 @@ describe('Events Routes', () => {
}
]);
});

it('should filter events by mininum creation date', async () => {
const event1 = buildEvent({
title: 'First Event',
slug: 'first-event-joinville-sc-20240215',
date: '2024-02-15',
created_at: new Date(2024, 1, 10).toISOString(),
updated_at: new Date(2024, 1, 10).toISOString()
});
const event2 = buildEvent({
title: 'Second Event',
slug: 'second-event-joinville-sc-20240217',
date: '2024-02-17',
created_at: new Date(2024, 1, 10).toISOString(),
updated_at: new Date(2024, 1, 10).toISOString()
});
const event3 = buildEvent({
title: 'Third Event',
slug: 'third-event-joinville-sc-20240225',
date: '2024-02-25',
time: '20:00',
created_at: new Date(2024, 1, 12).toISOString(),
updated_at: new Date(2024, 1, 12).toISOString()
});
await serve().post('/events').send(event1);
await serve().post('/events').send(event2);
await serve().post('/events').send(event3);
const response = await serve().get('/events?minCreationDate=2024-02-12');
expect(response.status).toEqual(200);
expect(response.body).toEqual([
{
id: expect.any(String),
...event3
}
]);
});
});

0 comments on commit 26622d8

Please sign in to comment.