diff --git a/src/Controllers/admin.controller.ts b/src/Controllers/admin.controller.ts index bdd64d0..083d3c9 100644 --- a/src/Controllers/admin.controller.ts +++ b/src/Controllers/admin.controller.ts @@ -1,31 +1,37 @@ import prisma from "../lib/prisma"; import { Request, Response } from "express"; +import { AdminLevel } from "@prisma/client"; + +const AUTH_ERROR = { + type: "failure", + payload: { + message: "The server was not able to validate your credentials; Please try again later", + }, +}; + +const createInsufficientPermissionsError = (required: AdminLevel = "ELEVATED") => ({ + type: "error", + payload: { + message: "You do not have sufficient permissions to use this feature", + required_level: required, + }, + _links: [ + { + rel: "authentication", + href: "/api/authentication", + type: "POST", + }, + ], +}); // requires: auth(elevated) export const getAllAdmins = async (req: Request, res: Response) => { if (!req.auth?.isAuthenticated) { - return res.status(500).json({ - type: "failure", - payload: { - message: "The server was not able to validate your credentials; Please try again later", - }, - }); + return res.status(500).json(AUTH_ERROR); } if (req.auth.permission_level !== "ELEVATED") { - return res.status(403).json({ - type: "error", - payload: { - message: "You do not have sufficient permissions to use this feature", - }, - _links: [ - { - rel: "authentication", - href: "/api/authentication", - type: "POST", - }, - ], - }); + return res.status(403).json(createInsufficientPermissionsError()); } // TODO: Add exception handling