Merge branch 'feature-endpoints' into feature-roles

This commit is contained in:
Laurin
2022-06-03 22:39:38 +02:00
21 changed files with 331 additions and 485 deletions
+46 -19
View File
@@ -4,9 +4,31 @@ import { createInsufficientPermissionsError, DataType, generateInvalidBodyError
import { requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth";
import { z } from "zod";
import { TeamBody } from "./user_auth.controller";
import { Prisma } from "@prisma/client";
import NotFoundError from "../Middleware/error/NotFoundError";
export const basicTeam = {
pid: true,
name: true,
discipline: { select: { pid: true } },
roles: {
select: {
pid: true,
schema: { select: { name: true } },
participant: { select: { pid: true } },
},
},
participants: {
select: {
pid: true,
firstName: true,
lastName: true,
},
},
};
export const getTeams = async (req: Request, res: Response) => {
const teams = prisma.team.findMany({ select: { pid: true, name: true, disciplineId: true } });
const teams = await prisma.team.findMany({ select: basicTeam });
res.status(200).json({ type: "success", payload: { teams } });
};
@@ -14,13 +36,9 @@ export const getTeams = async (req: Request, res: Response) => {
export const getTeam = async (req: Request, res: Response) => {
const { pid } = req.params;
const team = prisma.team.findUnique({
const team = await prisma.team.findUnique({
where: { pid },
select: {
disciplineId: true,
name: true,
pid: true,
},
select: basicTeam,
});
res.status(200).json({ type: "success", payload: { team } });
@@ -48,26 +66,35 @@ export const updateTeam = async (req: Request, res: Response) => {
requireLeaderOfTeam(req.teamleader, body.pid);
const team = prisma.team.update({
where: {
pid: body.pid,
},
data: {
name: body.teamName,
discipline: { connect: { pid: body.disciplineId } },
leaderEmail: body.leaderEmail,
},
});
try {
const team = await prisma.team.update({
where: {
pid: body.pid,
},
data: {
name: body.teamName,
discipline: { connect: { pid: body.disciplineId } },
leaderEmail: body.leaderEmail,
},
});
res.status(204).json({ type: "success", payload: { team } });
res.status(204).json({ type: "success", payload: { team } });
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("team", body.pid);
}
throw e;
}
};
export const deleteTeam = async (req: Request, res: Response) => {
const { pid } = req.params;
// TODO: accept admin auth
requireLeaderOfTeam(req.teamleader, pid);
prisma.team.delete({ where: { pid } });
await prisma.team.delete({ where: { pid } });
res.status(204).json({ type: "success", payload: { message: "Sucesfully deleted team" } });
};