From 718c040517aef813908cb391f935386d04f0a96d Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Sat, 4 Jun 2022 22:28:59 +0200 Subject: [PATCH] Fix routes, add getRole controller --- src/Controllers/participant.controller.ts | 2 +- src/Controllers/role.controller.ts | 31 ++++++++++++++++++----- src/Controllers/team.controller.ts | 9 ++++++- src/Routes/participant.routes.ts | 30 ++++++++++++++++++---- src/Routes/role.routes.ts | 11 ++++++-- 5 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/Controllers/participant.controller.ts b/src/Controllers/participant.controller.ts index 84ecef0..94021ff 100644 --- a/src/Controllers/participant.controller.ts +++ b/src/Controllers/participant.controller.ts @@ -81,7 +81,7 @@ export const getAllParticipants = async (req: Request<{}, {}, {}, { teamPid?: st return _getAllParticipants(res, auth, req.query.teamPid); }; -export const getAllDisciplinesParams = async (req: Request<{ teamPid: string }>, res: Response) => { +export const getAllParticipantsParams = async (req: Request<{ teamPid: string }>, res: Response) => { const auth = req.auth || req.teamleader; if (!auth) { diff --git a/src/Controllers/role.controller.ts b/src/Controllers/role.controller.ts index 8860f52..7b4f671 100644 --- a/src/Controllers/role.controller.ts +++ b/src/Controllers/role.controller.ts @@ -11,6 +11,13 @@ import { getGroupsByTeamPid } from "./team.controller"; require("express-async-errors"); +const basicRole = { + pid: true, + score: true, + schema: { select: { pid: true } }, + participant: { select: { pid: true, firstName: true, lastName: true } }, +}; + const detailedRole = { pid: true, score: true, @@ -67,12 +74,7 @@ export async function getRolesForTeam(req: Request<{ pid: string }>, res: Respon const roles = await prisma.role.findMany({ where: { team: { pid } }, - select: { - pid: true, - score: true, - schema: { select: { pid: true } }, - participant: { select: { pid: true, firstName: true, lastName: true } }, - }, + select: basicRole, }); return res.status(200).json({ @@ -83,6 +85,23 @@ export async function getRolesForTeam(req: Request<{ pid: string }>, res: Respon }); } +export async function getRole(req: Request<{ rolePid: string }>, res: Response) { + const rolePid = req.params.rolePid; + + const role = await prisma.role.findUnique({ where: { pid: rolePid }, select: basicRole }); + + if (!role) { + throw new NotFoundError("role", rolePid); + } + + return res.status(200).json({ + type: "success", + payload: { + role, + }, + }); +} + const AssignParticipantToRoleBody = z.object({ participantPid: z.string().uuid(), }); diff --git a/src/Controllers/team.controller.ts b/src/Controllers/team.controller.ts index 7b685a0..9d1f0f2 100644 --- a/src/Controllers/team.controller.ts +++ b/src/Controllers/team.controller.ts @@ -1,6 +1,6 @@ import { Request, Response } from "express"; import prisma from "../lib/prisma"; -import { DataType, generateInvalidBodyError } from "./common"; +import { createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common"; import { requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth"; import { TeamBody } from "./user_auth.controller"; import { Prisma } from "@prisma/client"; @@ -88,6 +88,13 @@ export const updateTeam = async (req: Request, res: Response) => { const body = result.data; + try { + requireLeaderOfTeam(req.teamleader, pid); + } catch { + // TODO: DO NOT CATCH THESE ERRORS + return res.status(401).json(createInsufficientPermissionsError("STANDARD")); + } + try { const team = await prisma.team.update({ where: { diff --git a/src/Routes/participant.routes.ts b/src/Routes/participant.routes.ts index 2e76a2e..aa8f69d 100644 --- a/src/Routes/participant.routes.ts +++ b/src/Routes/participant.routes.ts @@ -1,13 +1,33 @@ import express from "express"; -import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller"; -import { requireConfiguredAuthentication } from "../Middleware/auth/auth"; import teamRouter from "./team.routes"; +import { + createParticipant, + deleteParticipant, + getAllParticipants, + getAllParticipantsParams, + updateParticipant, +} from "../Controllers/participant.controller"; +import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth"; + +require("express-async-errors"); const router = express.Router(); -teamRouter.post<"/:teamPid/participants", { teamPid: string }>( - "/:teamPid/participants", - requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }), +router.get( + "/", + requireConfiguredAuthentication({ type: { admin: true, teamleader: true }, optional: false }), + getAllParticipants +); + +teamRouter.get( + "/:pid/particpants", + requireConfiguredAuthentication({ type: { admin: true, teamleader: true }, optional: false }), + getAllParticipantsParams +); + +teamRouter.post<"/:teamPid/participants/", { teamPid: string }>( + "/:teamPid/participants/", + requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }), createParticipant ); diff --git a/src/Routes/role.routes.ts b/src/Routes/role.routes.ts index f5df428..f070e32 100644 --- a/src/Routes/role.routes.ts +++ b/src/Routes/role.routes.ts @@ -1,10 +1,17 @@ import Express from "express"; -import { assignParticipantToRole, getRolesForTeam } from "../Controllers/role.controller"; -import { requireConfiguredAuthentication } from "../Middleware/auth/auth"; +import { assignParticipantToRole, getRole, getRolesForTeam } from "../Controllers/role.controller"; +import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth"; +import { requireTeamleaderAuthentication } from "../Middleware/auth/teamleaderAuth"; import teamRouter from "./team.routes"; const router = Express.Router(); +router.get( + "/:rolePid", + requireConfiguredAuthentication({ type: { admin: true, teamleader: true }, optional: false }), + getRole +); + router.put<"/:pid/participant", { pid: string }>( "/:pid/participant", requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),