diff --git a/src/api/routes/users.ts b/src/api/routes/users.ts index 1e70c41..1ce0df9 100644 --- a/src/api/routes/users.ts +++ b/src/api/routes/users.ts @@ -1,7 +1,7 @@ import express from "express"; import User, { getUserByPassword, updateUser } from "../../models/User.model"; -import { createSession, getSessionsByUserId, updateSession } from "../../models/Session.model"; +import { createSession, deleteSession, getSessionsByUserId, updateSession } from "../../models/Session.model"; import Time, { isOverlappingPreviousTimes } from "../../utils/time"; import { InvalidTimeError, RowNotFoundError } from "../../utils/errors"; @@ -124,7 +124,7 @@ router.patch("/session", async (req, res) => { if (!req.body.start_time || !req.body.end_time || !req.body.session_id) { return res.status(400).json({ - description: "Missing start and/or end times!", + description: "Missing session ID and/or start times and/or end times!", }); } @@ -171,4 +171,26 @@ router.patch("/session", async (req, res) => { }); }); +router.delete("/session", async (req, res) => { + const user: User = res.locals.user; + + if (!req.body.start_time || !req.body.end_time || !req.body.session_id) { + return res.status(400).json({ + description: "Missing session ID!", + }); + } + + const deleted = await deleteSession(req.body.session_id); + + if (!deleted) { + return res.status(400).json({ + description: "Could not delete session!", + }) + } + + return res.status(200).json({ + description: "Deleted session!", + }); +}); + export default router; diff --git a/src/utils/middleware.ts b/src/utils/middleware.ts index 7c3ca8d..9efcc7b 100644 --- a/src/utils/middleware.ts +++ b/src/utils/middleware.ts @@ -4,7 +4,7 @@ import logger from "./logger"; const errorHandler: ErrorRequestHandler = (err, req, res, next) => { if (err instanceof CustomError && !!err.statusCode) { - logger.warn(err.message) + logger.warn(err.message); return res.status(err.statusCode).json({ description: err.message, });