Skip to content

Commit

Permalink
Add DELETE /api/v1/users/session
Browse files Browse the repository at this point in the history
  • Loading branch information
Cow-Van committed Jan 16, 2024
1 parent 968c411 commit 8039218
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/api/routes/users.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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!",
});
}

Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/utils/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 8039218

Please sign in to comment.