mirror of
https://github.com/detleph/server.git
synced 2026-09-04 00:26:03 +02:00
adding team router/cont and participant router
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import prisma from "../lib/prisma";
|
||||
import { z } from "zod";
|
||||
import { Request, Response } from "express";
|
||||
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
|
||||
import {
|
||||
AUTH_ERROR,
|
||||
createInsufficientPermissionsError,
|
||||
DataType,
|
||||
generateError,
|
||||
generateInvalidBodyError,
|
||||
} from "./common";
|
||||
import { Job, Prisma } from "@prisma/client";
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||
import { requireResponsibleForGroup } from "../Middleware/auth/auth";
|
||||
|
||||
//TODO: add TeamleaderAuthentification
|
||||
|
||||
@@ -23,35 +30,49 @@ const returnedParticipant = {
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
relevance: true,
|
||||
team: { select: {
|
||||
team: {
|
||||
select: {
|
||||
pid: true,
|
||||
name: true,
|
||||
} },
|
||||
group: { select: {
|
||||
},
|
||||
},
|
||||
group: {
|
||||
select: {
|
||||
pid: true,
|
||||
name: true,
|
||||
} },
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
// REVIEW: Location of this endpoints (/groups, /teams, /participants, ...?)
|
||||
export const createParticipant = async (req: Request<{ pid: string}>, res: Response) => {
|
||||
//insert TeamleaderAuth
|
||||
|
||||
export const createParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||
const result = ParticipantBody.safeParse(req.body);
|
||||
|
||||
if(result.success === false){
|
||||
if (result.success === false) {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
generateInvalidBodyError(
|
||||
{
|
||||
firstname: DataType.STRING,
|
||||
lastName: DataType.STRING,
|
||||
groupId: DataType.UUID,
|
||||
}, result.error)
|
||||
},
|
||||
result.error
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const body = result.data;
|
||||
const { pid } = req.params;
|
||||
|
||||
/*
|
||||
if (!req.auth?.isAuthenticated || req.teamleader?.team != pid) {
|
||||
return res.status(500).json(AUTH_ERROR);
|
||||
}
|
||||
if (req.teamleader?.team != pid) {
|
||||
return res.status(500).json(AUTH_ERROR);
|
||||
}
|
||||
requireResponsibleForGroup(req.auth, req.body.groupId);
|
||||
*/
|
||||
|
||||
try {
|
||||
const participant = await prisma.participant.create({
|
||||
data: {
|
||||
@@ -70,24 +91,29 @@ export const createParticipant = async (req: Request<{ pid: string}>, res: Respo
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
return res.status(404).json(generateError(`Could not link to team with ID '${pid}, or group with ID ${body.groupId}'`));
|
||||
return res
|
||||
.status(404)
|
||||
.json(generateError(`Could not link to team with ID '${pid}, or group with ID ${body.groupId}'`));
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||
//insert TeamleaderAuth
|
||||
|
||||
const result = ParticipantBody.partial().safeParse(req.body); // Should be partial, right?
|
||||
|
||||
if(result.success === false){
|
||||
if (result.success === false) {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
generateInvalidBodyError(
|
||||
{
|
||||
firstname: DataType.STRING,
|
||||
lastName: DataType.STRING,
|
||||
groupId: DataType.UUID,
|
||||
}, result.error)
|
||||
},
|
||||
result.error
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,7 +126,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.groupId } },
|
||||
},
|
||||
select: returnedParticipant,
|
||||
});
|
||||
@@ -109,15 +135,14 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
|
||||
type: "success",
|
||||
payload: { participant },
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
throw new NotFoundError("participant", pid)
|
||||
throw new NotFoundError("participant", pid);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||
//insert TeamleaderAuth
|
||||
@@ -135,4 +160,4 @@ export const deleteParticipant = async (req: Request<{ pid: string }>, res: Resp
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@ const TeamBody = z.object({
|
||||
partFirstName: z.string().min(1),
|
||||
partLastName: z.string().min(1),
|
||||
partGroupId: z.string().uuid(),
|
||||
})
|
||||
});
|
||||
|
||||
interface CreateTeamBody {
|
||||
teamName: string;
|
||||
@@ -28,19 +28,21 @@ interface CreateTeamBody {
|
||||
|
||||
// TODO: Some kind of auth (Teamleader probably)
|
||||
export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Response) => {
|
||||
|
||||
const result = TeamBody.safeParse(req.body);
|
||||
|
||||
if (result.success === false) {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
generateInvalidBodyError(
|
||||
{
|
||||
teamName: DataType.STRING,
|
||||
leaderEmail: DataType.STRING,
|
||||
disciplineId: DataType.UUID,
|
||||
partFirstName: DataType.STRING,
|
||||
partLastName: DataType.STRING,
|
||||
partGroupId: DataType.UUID,
|
||||
}, result.error)
|
||||
},
|
||||
result.error
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -58,8 +60,8 @@ export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Respon
|
||||
lastName: body.partLastName,
|
||||
relevance: "TEAMLEADER",
|
||||
group: { connect: { pid: body.partGroupId } },
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
pid: true,
|
||||
@@ -68,7 +70,7 @@ export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Respon
|
||||
},
|
||||
});
|
||||
|
||||
//To do: maybe use returned amount of created use?
|
||||
//TODO: maybe use returned amount of created use?
|
||||
createRolesForTeam(team.pid);
|
||||
|
||||
const usid = nanoid();
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import express from "express";
|
||||
import teamRouter from "./team.routes";
|
||||
import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller";
|
||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||
import { requireTeamleaderAuthentication } from "../Middleware/auth/teamleaderAuth";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
teamRouter.post<"/:pid/participant/", { pid: string }>(
|
||||
"/:pid/participant/",
|
||||
requireAuthentication,
|
||||
requireTeamleaderAuthentication,
|
||||
createParticipant
|
||||
);
|
||||
|
||||
teamRouter.patch<"/:pid/participant/", { pid: string }>(
|
||||
"/:pid/participant/",
|
||||
requireAuthentication,
|
||||
requireTeamleaderAuthentication,
|
||||
updateParticipant
|
||||
);
|
||||
|
||||
teamRouter.delete<"/:pid/participant/", { pid: string }>(
|
||||
"/:pid/participant/",
|
||||
requireAuthentication,
|
||||
requireTeamleaderAuthentication,
|
||||
deleteParticipant
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,12 @@
|
||||
import express from "express";
|
||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||
import { requireTeamleaderAuthentication } from "../Middleware/auth/teamleaderAuth";
|
||||
import { register } from "../Controllers/user_auth.controller";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/", requireAuthentication, requireTeamleaderAuthentication, register);
|
||||
|
||||
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, requireTeamleaderAuthentication, deleteTeam);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user