From 5489e6377c3e4ae0f8bbd0a45d61c5f56747d890 Mon Sep 17 00:00:00 2001 From: minie4 Date: Thu, 2 Jun 2022 21:32:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20date=20parameter=20not=20a?= =?UTF-8?q?ccepting=20unix=20timestamp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/index.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/server/api/index.js b/server/api/index.js index 6c3df30..db12d93 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -32,6 +32,17 @@ export async function getTimetable(req, res) { res.send(timetable.data); } +// Helper function for converting a date string +// (eg. "2022-06-02" or "1654128000000") to a +// unix timestamp +function convertToDate(dateQuery) { + var date; + if (dateQuery.match(/^[0-9]+$/) != null) date = parseInt(dateQuery); + else date = dateQuery; + date = new Date(date).setUTCHours(0, 0, 0, 0); + return date; +} + // Get substitutions API endpoint (/api/substitutions) // Returns all known substitutions for requested date / class // If no class is supplied, all substitutions are returned @@ -40,10 +51,10 @@ export async function getSubstitutions(req, res) { var from, to, date; // Check if from or to date is set in request if (req.query.from && req.query.to) { - from = new Date(req.query.from).setUTCHours(0, 0, 0, 0); - to = new Date(req.query.to).setUTCHours(0, 0, 0, 0); + from = convertToDate(req.query.from); + to = convertToDate(req.query.to); } else if (req.query.date) { - date = new Date(req.query.date).setUTCHours(0, 0, 0, 0); + date = convertToDate(req.query.date); } const prismaOptions = { @@ -102,10 +113,10 @@ export async function getHistory(req, res) { var from, to, date; // Check if from or to date is set in request if (req.query.from && req.query.to) { - from = new Date(req.query.from).setUTCHours(0, 0, 0, 0); - to = new Date(req.query.to).setUTCHours(0, 0, 0, 0); + from = convertToDate(req.query.from); + to = convertToDate(req.query.to); } else if (req.query.date) { - date = new Date(req.query.date).setUTCHours(0, 0, 0, 0); + date = convertToDate(req.query.date); } const prismaOptions = {