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
// 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(),
lastName: z.string(),
groupId: z.string().uuid(),
//job: z.enum(["TEAMLEADER", "MEMBER"]),
groupPid: z.string().uuid(),
});
const ParticipantBody = InitialParticipant.extend({ teamPid: z.string().uuid() });
const returnedParticipant = {
pid: true,
firstName: true,
@@ -45,7 +46,7 @@ const returnedParticipant = {
} as const;
// 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);
if (result.success === false) {
@@ -54,14 +55,14 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
{
firstname: DataType.STRING,
lastName: DataType.STRING,
groupId: DataType.UUID,
groupPid: DataType.UUID,
teamPid: DataType.UUID,
},
result.error
)
);
}
const body = result.data;
const { teamPid } = req.params;
try {
const participant = await prisma.participant.create({
@@ -69,8 +70,8 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
firstName: body.firstName,
lastName: body.lastName,
relevance: "MEMBER",
group: { connect: { pid: body.groupId } },
team: { connect: { pid: teamPid } },
group: { connect: { pid: body.groupPid } },
team: { connect: { pid: body.teamPid } },
},
select: returnedParticipant,
});
@@ -83,7 +84,7 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return res
.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;
}
@@ -91,7 +92,7 @@ export const createParticipant = async (req: Request<{ teamPid: string }>, res:
// at: PATCH api/participants/:pid/
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) {
return res.status(400).json(
@@ -99,7 +100,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
{
firstname: DataType.STRING,
lastName: DataType.STRING,
groupId: DataType.UUID,
groupPid: DataType.UUID,
},
result.error
)
@@ -115,7 +116,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
data: {
firstName: body.firstName,
lastName: body.lastName,
group: { connect: { pid: body.groupId } },
group: { connect: { pid: body.groupPid } },
},
select: returnedParticipant,
});
+5 -9
View File
@@ -1,28 +1,24 @@
import express from "express";
import teamRouter from "./team.routes";
import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller";
import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth";
const router = express.Router();
teamRouter.post<"/:teamPid/participants/", { teamPid: string }>(
"/:teamPid/participants/",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
router.post(
"/",
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
createParticipant
);
router.patch<"/:pid/", { pid: string }>(
"/:pid/",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
updateParticipant
);
router.delete<"/:pid/", { pid: string }>(
"/:pid/",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
deleteParticipant
);
+2 -4
View File
@@ -7,15 +7,13 @@ const router = Express.Router();
//TO DO: maybe transfer getRolesForTeam to team router -> Seconded
router.get<"team/:teamPid/", { teamPid: string }>(
"team/:teamPid/",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
getRolesForTeam
);
router.put<"/:pid/participant", { pid: string }>(
"/:pid/participant",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
assignParticipantToRole
);
+2 -4
View File
@@ -8,16 +8,14 @@ const router = express.Router();
router.get("/", requireConfiguredAuthentication({ type: "admin", optional: false }), getTeams);
router.get(
"/:id",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
getTeam
);
router.put("/", requireTeamleaderAuthentication, updateTeam);
router.delete<"/:pid/", { pid: string }>(
"/:pid/",
requireAuthentication,
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }),
deleteTeam
);