Imp: check if input date is valid in submitted exercise log

This commit is contained in:
Manish 2023-03-29 13:16:23 +11:00
parent f223697b88
commit 58c2e2f94e

View File

@ -65,11 +65,21 @@ app.get("/api/users", (req, res) => {
});
});
const isValidDate = (dateString) => {
if (!Number.isNaN(Number(dateString))) return true;
const date = new Date(dateString);
return !Number.isNaN(date.getTime());
};
app.post("/api/users/:_id/exercises", (req, res) => {
const _id = req.params._id;
const description = req.body.description;
const duration = req.body.duration;
const date = req.body.date ? req.body.date : new Date().toDateString();
if (!isValidDate(date)) {
res.status(400);
return res.json({ error: `Input date: '${date}' is not valid` });
}
exerciseLogs
.exists({ _id: _id })
.then((userExists) => {