diff --git a/src/Controllers/group.controllers.ts b/src/Controllers/group.controllers.ts index 10f42c7..ee7fb5e 100644 --- a/src/Controllers/group.controllers.ts +++ b/src/Controllers/group.controllers.ts @@ -1,8 +1,8 @@ import { Admin } from "@prisma/client"; -import { PrismaClientUnknownRequestError } from "@prisma/client/runtime"; +import { PrismaClientKnownRequestError, PrismaClientUnknownRequestError } from "@prisma/client/runtime"; import { Request, Response } from "express"; import prisma from "../lib/prisma"; -import { generateError, genericError, handleCreateByName } from "./common"; +import { createInsufficientPermissionsError, generateError, genericError, handleCreateByName } from "./common"; const basicGroup = { pid: true, @@ -114,3 +114,27 @@ export const createGroup = async (req: Request<{ organisationPid: string }, {}, res ); }; + +interface DeleteGroupQueryParams { + pid: string; +} + +export const deleteGroup = async (req: Request, res: Response) => { + if (req.auth?.permission_level !== "ELEVATED") { + return res.status(403).json(createInsufficientPermissionsError()); + } + + const { pid } = req.params; + + try { + prisma.group.delete({ where: { pid } }); + + return res.status(204).end(); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") { + return res.status(404).json(generateError(`The group with the ID ${pid} could not be found`)); + } + + throw e; + } +}; diff --git a/src/Routes/group.routes.ts b/src/Routes/group.routes.ts index 12ae59d..4ab9387 100644 --- a/src/Routes/group.routes.ts +++ b/src/Routes/group.routes.ts @@ -1,5 +1,11 @@ import express from "express"; -import { createGroup, getAllGroups, getAllGroupsWithParam, getGroup } from "../Controllers/group.controllers"; +import { + createGroup, + deleteGroup, + getAllGroups, + getAllGroupsWithParam, + getGroup, +} from "../Controllers/group.controllers"; import { requireAuthentication } from "../Middleware/auth/auth"; import organisationRouter from "./organisation.routes"; @@ -7,6 +13,7 @@ const router = express.Router(); router.get("/", getAllGroups); router.get("/:pid", getGroup); +router.delete<"/:pid", { pid: string }>("/:pid", requireAuthentication, deleteGroup); organisationRouter.get("/:organisationPid/groups", getAllGroupsWithParam); organisationRouter.post("/:organisationPid/groups", requireAuthentication, createGroup); diff --git a/src/Routes/organisation.routes.ts b/src/Routes/organisation.routes.ts index 7cff08d..9cda611 100644 --- a/src/Routes/organisation.routes.ts +++ b/src/Routes/organisation.routes.ts @@ -2,6 +2,7 @@ import express from "express"; import eventRouter from "./event.routes"; import { createOrganisation, + deleteOrganisation, getAllOrganisations, getAllOrganisationsWithParam, getOrganisation, @@ -14,6 +15,7 @@ const router = express.Router(); router.get("/", getAllOrganisations); router.get("/:pid", getOrganisation); router.put<"/:pid", { pid: string }>("/:pid", requireAuthentication, updateOrganisation); +router.delete<"/:pid", { pid: string }>("/:pid", requireAuthentication, deleteOrganisation); eventRouter.get("/:eventPid/organisations", getAllOrganisationsWithParam); eventRouter.post<"/:eventPid/organisations", { eventPid: string }>(