Skip to content

Commit

Permalink
Adjust "future date" entry checking
Browse files Browse the repository at this point in the history
  • Loading branch information
robflop committed Feb 17, 2018
1 parent 7830de3 commit 22cf5ab
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ server.get('/counter', (req, res) => {
server.get('/stats', (req, res) => { // eslint-disable-line complexity
let requestedStats = {};
const firstStatDate = moment(Object.keys(statistics)[0]);
const latestStatDate = moment(Object.keys(statistics)[Object.keys(statistics).length - 1]);
// grab latest statistics entry from the object itself instead of just today's date to make sure the entry exists

if (req.query.from || req.query.to) {
if ((req.query.from && !dateRegex.test(req.query.from)) || (req.query.to && !dateRegex.test(req.query.to))) {
Expand All @@ -191,7 +193,7 @@ server.get('/stats', (req, res) => { // eslint-disable-line complexity
const to = req.query.to ? moment(req.query.to) : null;
const from = req.query.from ? moment(req.query.from) : null;

if (to && to.isAfter(moment()) || from && from.isAfter(moment())) {
if (to && to.isAfter(latestStatDate) || from && from.isAfter(latestStatDate)) {
return res.status(400).json({ code: 400, name: 'Invalid timespan', message: 'Dates can not be in the future.' });
}

Expand All @@ -211,8 +213,6 @@ server.get('/stats', (req, res) => { // eslint-disable-line complexity
}
}
else {
const latestStatDate = moment(); // today

requestedStats = filterStats(firstStatDate, latestStatDate);
}

Expand Down

0 comments on commit 22cf5ab

Please sign in to comment.