small fixes

This commit is contained in:
Laurin
2022-06-03 22:25:21 +02:00
parent 0f0da062c6
commit b3a1ec5fa5
4 changed files with 22 additions and 29 deletions
+13 -12
View File
@@ -18,13 +18,14 @@ import { requireConfiguredAuthentication, requireResponsibleForGroup } from "../
// REVIEW: All this code should be able to be executed by the teamleader of the team the participant is in AND // REVIEW: All this code should be able to be executed by the teamleader of the team the participant is in AND
// an admin the group of whom overlaps with the team AND an elevated admin // an admin the group of whom overlaps with the team AND an elevated admin
const ParticipantBody = z.object({ const InitialParticipant = z.object({
firstName: z.string(), firstName: z.string(),
lastName: z.string(), lastName: z.string(),
groupId: z.string().uuid(), groupPid: z.string().uuid(),
//job: z.enum(["TEAMLEADER", "MEMBER"]),
}); });
const ParticipantBody = InitialParticipant.extend({ teamPid: z.string().uuid() });
const returnedParticipant = { const returnedParticipant = {
pid: true, pid: true,
firstName: true, firstName: true,
@@ -45,7 +46,7 @@ const returnedParticipant = {
} as const; } as const;
// at: POST api/teams/:teamPid/participant/ // at: POST api/teams/:teamPid/participant/
export const createParticipant = async (req: Request<{ teamPid: string }>, res: Response) => { export const createParticipant = async (req: Request, res: Response) => {
const result = ParticipantBody.safeParse(req.body); const result = ParticipantBody.safeParse(req.body);
if (result.success === false) { if (result.success === false) {
@@ -54,14 +55,14 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
{ {
firstname: DataType.STRING, firstname: DataType.STRING,
lastName: DataType.STRING, lastName: DataType.STRING,
groupId: DataType.UUID, groupPid: DataType.UUID,
teamPid: DataType.UUID,
}, },
result.error result.error
) )
); );
} }
const body = result.data; const body = result.data;
const { teamPid } = req.params;
try { try {
const participant = await prisma.participant.create({ const participant = await prisma.participant.create({
@@ -69,8 +70,8 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
firstName: body.firstName, firstName: body.firstName,
lastName: body.lastName, lastName: body.lastName,
relevance: "MEMBER", relevance: "MEMBER",
group: { connect: { pid: body.groupId } }, group: { connect: { pid: body.groupPid } },
team: { connect: { pid: teamPid } }, team: { connect: { pid: body.teamPid } },
}, },
select: returnedParticipant, select: returnedParticipant,
}); });
@@ -83,7 +84,7 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") { if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return res return res
.status(404) .status(404)
.json(generateError(`Could not link to team with ID '${teamPid}, or group with ID ${body.groupId}'`)); .json(generateError(`Could not link to team with ID '${body.teamPid}, or group with ID ${body.groupPid}'`));
} }
throw e; throw e;
} }
@@ -91,7 +92,7 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
// at: PATCH api/participants/:pid/ // at: PATCH api/participants/:pid/
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => { export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
const result = ParticipantBody.partial().safeParse(req.body); const result = InitialParticipant.partial().safeParse(req.body);
if (result.success === false) { if (result.success === false) {
return res.status(400).json( return res.status(400).json(
@@ -99,7 +100,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
{ {
firstname: DataType.STRING, firstname: DataType.STRING,
lastName: DataType.STRING, lastName: DataType.STRING,
groupId: DataType.UUID, groupPid: DataType.UUID,
}, },
result.error result.error
) )
@@ -115,7 +116,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
data: { data: {
firstName: body.firstName, firstName: body.firstName,
lastName: body.lastName, lastName: body.lastName,
group: { connect: { pid: body.groupId } }, group: { connect: { pid: body.groupPid } },
}, },
select: returnedParticipant, select: returnedParticipant,
}); });
+5 -9
View File
@@ -1,28 +1,24 @@
import express from "express"; import express from "express";
import teamRouter from "./team.routes";
import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller"; import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller";
import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth"; import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth";
const router = express.Router(); const router = express.Router();
teamRouter.post<"/:teamPid/participants/", { teamPid: string }>( router.post(
"/:teamPid/participants/", "/",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
createParticipant createParticipant
); );
router.patch<"/:pid/", { pid: string }>( router.patch<"/:pid/", { pid: string }>(
"/:pid/", "/:pid/",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
updateParticipant updateParticipant
); );
router.delete<"/:pid/", { pid: string }>( router.delete<"/:pid/", { pid: string }>(
"/:pid/", "/:pid/",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
deleteParticipant deleteParticipant
); );
+2 -4
View File
@@ -7,15 +7,13 @@ const router = Express.Router();
//TO DO: maybe transfer getRolesForTeam to team router -> Seconded //TO DO: maybe transfer getRolesForTeam to team router -> Seconded
router.get<"team/:teamPid/", { teamPid: string }>( router.get<"team/:teamPid/", { teamPid: string }>(
"team/:teamPid/", "team/:teamPid/",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
getRolesForTeam getRolesForTeam
); );
router.put<"/:pid/participant", { pid: string }>( router.put<"/:pid/participant", { pid: string }>(
"/:pid/participant", "/:pid/participant",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
assignParticipantToRole assignParticipantToRole
); );
+2 -4
View File
@@ -8,16 +8,14 @@ const router = express.Router();
router.get("/", requireConfiguredAuthentication({ type: "admin", optional: false }), getTeams); router.get("/", requireConfiguredAuthentication({ type: "admin", optional: false }), getTeams);
router.get( router.get(
"/:id", "/:id",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
getTeam getTeam
); );
router.put("/", requireTeamleaderAuthentication, updateTeam); router.put("/", requireTeamleaderAuthentication, updateTeam);
router.delete<"/:pid/", { pid: string }>( router.delete<"/:pid/", { pid: string }>(
"/:pid/", "/:pid/",
requireAuthentication, requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
deleteTeam deleteTeam
); );