From 8aa4e5ef1609a346f4e671bf71c746661e3ffb2f Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 23 Apr 2022 20:42:01 +0200 Subject: [PATCH] Improve error handling and simplify code --- src/Controllers/common.ts | 16 +++++++ src/Controllers/organisation.controller.ts | 53 +++++++++++++++++++--- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/Controllers/common.ts b/src/Controllers/common.ts index 2e6bb5e..994ae79 100644 --- a/src/Controllers/common.ts +++ b/src/Controllers/common.ts @@ -44,3 +44,19 @@ export const createInsufficientPermissionsError = (required: AdminLevel = "ELEVA }, ], }); + +export const generateError = (message: string) => { + return { + type: "error", + payload: { + message, + }, + }; +}; + +export const genericError = { + type: "error", + payload: { + message: "There was an error processing your request, please try again later", + }, +}; diff --git a/src/Controllers/organisation.controller.ts b/src/Controllers/organisation.controller.ts index cab0c0d..d18cf56 100644 --- a/src/Controllers/organisation.controller.ts +++ b/src/Controllers/organisation.controller.ts @@ -1,7 +1,14 @@ import { PrismaClientUnknownRequestError } from "@prisma/client/runtime"; import { Request, Response } from "express"; import prisma from "../lib/prisma"; -import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common"; +import { + AUTH_ERROR, + createInsufficientPermissionsError, + DataType, + generateError, + generateInvalidBodyError, + genericError, +} from "./common"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; import { Prisma } from "@prisma/client"; @@ -230,13 +237,45 @@ export const updateOrganisation = async ( } catch (e) { if (e instanceof PrismaClientKnownRequestError) { if (e.code === "P2025") { - return res.status(404).json({ - type: "error", - payload: { - message: `The organiation with the ID '${pid}' could not be found!`, - }, - }); + return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`)); } + } else if (e instanceof PrismaClientUnknownRequestError) { + return res.status(400).send(generateError("Unkonwn error occured. This could be due to malformed IDs")); } } + + return res.status(500).json(genericError); +}; + +interface DeleteOrganisationQueryParams { + pid: string; +} + +// requires: auth(ELEVATED) +export const deleteOrganisation = async (req: Request, res: Response) => { + if (!req.auth?.isAuthenticated) { + return res.status(500).json(AUTH_ERROR); + } + + if (req.auth.permission_level !== "ELEVATED") { + return res.status(403).json(createInsufficientPermissionsError()); + } + + const { pid } = req.params; + + try { + prisma.organisation.delete({ where: { pid } }); + + res.status(204).end(); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if ((e.code = "P2025")) { + return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`)); + } + } else if (e instanceof PrismaClientUnknownRequestError) { + return res.status(400).send(generateError("Unkonwn error occured. This could be due to malformed IDs")); + } + } + + return res.status(500).json(genericError); };