Fix routes, add getRole controller

This commit is contained in:
Stephan
2022-06-09 16:23:54 +02:00
committed by La_Felx
parent 87f5fa2e91
commit 718c040517
5 changed files with 68 additions and 15 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ export const getAllParticipants = async (req: Request<{}, {}, {}, { teamPid?: st
return _getAllParticipants(res, auth, req.query.teamPid); 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; const auth = req.auth || req.teamleader;
if (!auth) { if (!auth) {
+25 -6
View File
@@ -11,6 +11,13 @@ import { getGroupsByTeamPid } from "./team.controller";
require("express-async-errors"); require("express-async-errors");
const basicRole = {
pid: true,
score: true,
schema: { select: { pid: true } },
participant: { select: { pid: true, firstName: true, lastName: true } },
};
const detailedRole = { const detailedRole = {
pid: true, pid: true,
score: true, score: true,
@@ -67,12 +74,7 @@ export async function getRolesForTeam(req: Request<{ pid: string }>, res: Respon
const roles = await prisma.role.findMany({ const roles = await prisma.role.findMany({
where: { team: { pid } }, where: { team: { pid } },
select: { select: basicRole,
pid: true,
score: true,
schema: { select: { pid: true } },
participant: { select: { pid: true, firstName: true, lastName: true } },
},
}); });
return res.status(200).json({ 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({ const AssignParticipantToRoleBody = z.object({
participantPid: z.string().uuid(), participantPid: z.string().uuid(),
}); });
+8 -1
View File
@@ -1,6 +1,6 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import prisma from "../lib/prisma"; import prisma from "../lib/prisma";
import { DataType, generateInvalidBodyError } from "./common"; import { createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
import { requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth"; import { requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth";
import { TeamBody } from "./user_auth.controller"; import { TeamBody } from "./user_auth.controller";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
@@ -88,6 +88,13 @@ export const updateTeam = async (req: Request, res: Response) => {
const body = result.data; const body = result.data;
try {
requireLeaderOfTeam(req.teamleader, pid);
} catch {
// TODO: DO NOT CATCH THESE ERRORS
return res.status(401).json(createInsufficientPermissionsError("STANDARD"));
}
try { try {
const team = await prisma.team.update({ const team = await prisma.team.update({
where: { where: {
+25 -5
View File
@@ -1,13 +1,33 @@
import express from "express"; import express from "express";
import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller";
import { requireConfiguredAuthentication } from "../Middleware/auth/auth";
import teamRouter from "./team.routes"; 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(); const router = express.Router();
teamRouter.post<"/:teamPid/participants", { teamPid: string }>( router.get(
"/:teamPid/participants", "/",
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }), 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 createParticipant
); );
+9 -2
View File
@@ -1,10 +1,17 @@
import Express from "express"; import Express from "express";
import { assignParticipantToRole, getRolesForTeam } from "../Controllers/role.controller"; import { assignParticipantToRole, getRole, getRolesForTeam } from "../Controllers/role.controller";
import { requireConfiguredAuthentication } from "../Middleware/auth/auth"; import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth";
import { requireTeamleaderAuthentication } from "../Middleware/auth/teamleaderAuth";
import teamRouter from "./team.routes"; import teamRouter from "./team.routes";
const router = Express.Router(); const router = Express.Router();
router.get(
"/:rolePid",
requireConfiguredAuthentication({ type: { admin: true, teamleader: true }, optional: false }),
getRole
);
router.put<"/:pid/participant", { pid: string }>( router.put<"/:pid/participant", { pid: string }>(
"/:pid/participant", "/:pid/participant",
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }), requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),