patched requireLeaderOfTeam usecases

This commit is contained in:
Laurin
2022-06-05 03:35:37 +02:00
parent 619b46a6aa
commit 1d2794c75c
5 changed files with 61 additions and 27 deletions
+11 -8
View File
@@ -5,6 +5,7 @@ import prisma from "../lib/prisma";
import { requireLeaderOfTeam, requireResponsibleForParticipant } from "../Middleware/auth/teamleaderAuth";
import NotFoundError from "../Middleware/error/NotFoundError";
import { createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
import { getTeamPidByParticipantPid } from "./participant.controller";
require("express-async-errors");
@@ -53,15 +54,15 @@ export async function createRolesForTeam(teamPid: string) {
return roles.count;
}
export async function getRolesForTeam(req: Request<{ teamPid: string }>, res: Response) {
const teamPid = req.params.teamPid;
export async function getRolesForTeam(req: Request<{ pid: string }>, res: Response) {
const pid = req.params.pid;
if (req.teamleader?.isAuthenticated) {
requireLeaderOfTeam(req.teamleader, teamPid);
requireLeaderOfTeam(req.teamleader, pid);
}
const roles = await prisma.role.findMany({
where: { team: { pid: teamPid } },
where: { team: { pid } },
select: {
pid: true,
score: true,
@@ -80,16 +81,13 @@ export async function getRolesForTeam(req: Request<{ teamPid: string }>, res: Re
const AssignParticipantToRoleBody = z.object({
participantPid: z.string().uuid(),
teamPid: z.string().uuid(),
});
// requires: auth(leader of the team)
export async function assignParticipantToRole(req: Request<{ pid: string }>, res: Response) {
const { pid } = req.params;
if (req.teamleader?.isAuthenticated) {
requireLeaderOfTeam(req.teamleader, pid);
}
const zBody = AssignParticipantToRoleBody.safeParse(req.body);
if (zBody.success === false) {
@@ -97,6 +95,11 @@ export async function assignParticipantToRole(req: Request<{ pid: string }>, res
}
const { participantPid } = zBody.data;
const teamPid = await getTeamPidByParticipantPid(pid);
if (req.teamleader?.isAuthenticated) {
requireLeaderOfTeam(req.teamleader, teamPid);
}
const schema = await prisma.role.findFirst({
where: { pid, team: { participants: { some: { pid: participantPid } } } },