diff --git a/src/server.js b/src/server.js index 81ed7a45..1668a244 100644 --- a/src/server.js +++ b/src/server.js @@ -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))) { @@ -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.' }); } @@ -211,8 +213,6 @@ server.get('/stats', (req, res) => { // eslint-disable-line complexity } } else { - const latestStatDate = moment(); // today - requestedStats = filterStats(firstStatDate, latestStatDate); }