mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Fix routes, add getRole controller
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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(),
|
||||
});
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
|
||||
@@ -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 } }),
|
||||
|
||||
Reference in New Issue
Block a user