From ed90f1e6ae2d5e89f76b85162642739a91933ae3 Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Tue, 21 Dec 2021 12:19:17 +0100 Subject: [PATCH] Allow for groups to be specified when POSTing /api/admins + Add the admin router to the global app --- src/Controllers/admin.controller.ts | 29 +++++++++++++++++++++++++---- src/app.ts | 3 +++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Controllers/admin.controller.ts b/src/Controllers/admin.controller.ts index 526d80f..d15d720 100644 --- a/src/Controllers/admin.controller.ts +++ b/src/Controllers/admin.controller.ts @@ -50,7 +50,7 @@ interface CreateAdminBody { name?: string; password: string; permission_level: string; - // TODO: Add groups or events + groups?: string[]; } const PERMISSION_LEVELS: readonly AdminLevel[] = ["ELEVATED", "STANDARD"]; // TODO: Enforce completeness @@ -67,9 +67,13 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re return res.status(403).json(createInsufficientPermissionsError()); } - const { name, password, permission_level } = req.body || {}; + const { name, password, permission_level, groups } = req.body || {}; - if (!(typeof name === "string" && typeof password == "string" && isPermissionLevel(permission_level))) { + const groupsIsValid = groups ? groups.filter((e) => typeof e !== "string").length === 0 : true; + + if ( + !(typeof name === "string" && typeof password == "string" && isPermissionLevel(permission_level) && groupsIsValid) + ) { return res.status(400).json({ type: "error", payload: { @@ -87,9 +91,26 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re const password_hash = await argon2.hash(password, { type: argon2.argon2id }); + // Check if all gropus exist + for (const groupId of groups || []) { + if (!(await prisma.group.findUnique({ where: { pid: groupId } }))) { + return res.status(404).json({ + type: "error", + payload: { + message: `The group with ID '${groupId}' could not be found!`, + }, + }); + } + } + // TODO: Check for uniqueness of the name const user = await prisma.admin.create({ - data: { name, password: password_hash, permission_level }, + data: { + name, + password: password_hash, + permission_level, + groups: { connect: groups?.map((group) => ({ pid: group })) }, + }, select: { pid: true, name: true, permission_level: true }, }); diff --git a/src/app.ts b/src/app.ts index ce9c33f..75f3c95 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,6 +3,7 @@ import prisma from "./lib/prisma"; import eventRouter from "./Routes/event.routes"; import adminAuthRouter from "./Routes/admin_auth.routes"; import argon2 from "argon2"; +import adminRouter from "./Routes/admin.routes"; require("dotenv").config(); // Load dotenv config @@ -28,6 +29,8 @@ async function main() { // All API endpoints are behind /api/... app.use("/api/events", eventRouter); + app.use("/api/admins", adminRouter); + app.listen(process.env.PORT, () => { console.log(`Listening on Port: ${process.env.PORT}`); });