mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
adding team router/cont and participant router
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { Request, Response } from "express";
|
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 { Job, Prisma } from "@prisma/client";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
|
import { requireResponsibleForGroup } from "../Middleware/auth/auth";
|
||||||
|
|
||||||
//TODO: add TeamleaderAuthentification
|
//TODO: add TeamleaderAuthentification
|
||||||
|
|
||||||
@@ -12,127 +19,145 @@ import NotFoundError from "../Middleware/error/NotFoundError";
|
|||||||
// 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 ParticipantBody = z.object({
|
||||||
firstName: z.string(),
|
firstName: z.string(),
|
||||||
lastName: z.string(),
|
lastName: z.string(),
|
||||||
groupId: z.string().uuid(),
|
groupId: z.string().uuid(),
|
||||||
//job: z.enum(["TEAMLEADER", "MEMBER"]),
|
//job: z.enum(["TEAMLEADER", "MEMBER"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
const returnedParticipant = {
|
const returnedParticipant = {
|
||||||
pid: true,
|
pid: true,
|
||||||
firstName: true,
|
firstName: true,
|
||||||
lastName: true,
|
lastName: true,
|
||||||
relevance: true,
|
relevance: true,
|
||||||
team: { select: {
|
team: {
|
||||||
pid: true,
|
select: {
|
||||||
name: true,
|
pid: true,
|
||||||
} },
|
name: true,
|
||||||
group: { select: {
|
},
|
||||||
pid: true,
|
},
|
||||||
name: true,
|
group: {
|
||||||
} },
|
select: {
|
||||||
|
pid: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
// REVIEW: Location of this endpoints (/groups, /teams, /participants, ...?)
|
// REVIEW: Location of this endpoints (/groups, /teams, /participants, ...?)
|
||||||
export const createParticipant = async (req: Request<{ pid: string}>, res: Response) => {
|
export const createParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||||
//insert TeamleaderAuth
|
const result = ParticipantBody.safeParse(req.body);
|
||||||
|
|
||||||
const result = ParticipantBody.safeParse(req.body);
|
if (result.success === false) {
|
||||||
|
return res.status(400).json(
|
||||||
|
generateInvalidBodyError(
|
||||||
|
{
|
||||||
|
firstname: DataType.STRING,
|
||||||
|
lastName: DataType.STRING,
|
||||||
|
groupId: DataType.UUID,
|
||||||
|
},
|
||||||
|
result.error
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const body = result.data;
|
||||||
|
const { pid } = req.params;
|
||||||
|
|
||||||
if(result.success === false){
|
/*
|
||||||
return res.status(400).json(
|
if (!req.auth?.isAuthenticated || req.teamleader?.team != pid) {
|
||||||
generateInvalidBodyError({
|
return res.status(500).json(AUTH_ERROR);
|
||||||
firstname: DataType.STRING,
|
}
|
||||||
lastName: DataType.STRING,
|
if (req.teamleader?.team != pid) {
|
||||||
groupId: DataType.UUID,
|
return res.status(500).json(AUTH_ERROR);
|
||||||
}, result.error)
|
}
|
||||||
);
|
requireResponsibleForGroup(req.auth, req.body.groupId);
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const participant = await prisma.participant.create({
|
||||||
|
data: {
|
||||||
|
firstName: body.firstName,
|
||||||
|
lastName: body.lastName,
|
||||||
|
relevance: "MEMBER",
|
||||||
|
group: { connect: { pid: body.groupId } },
|
||||||
|
team: { connect: { pid } },
|
||||||
|
},
|
||||||
|
select: returnedParticipant,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(201).json({
|
||||||
|
type: "success",
|
||||||
|
payload: { participant },
|
||||||
|
});
|
||||||
|
} 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}'`));
|
||||||
}
|
}
|
||||||
|
throw e;
|
||||||
const body = result.data;
|
}
|
||||||
const { pid } = req.params;
|
};
|
||||||
|
|
||||||
try {
|
|
||||||
const participant = await prisma.participant.create({
|
|
||||||
data: {
|
|
||||||
firstName: body.firstName,
|
|
||||||
lastName: body.lastName,
|
|
||||||
relevance: "MEMBER",
|
|
||||||
group: { connect: { pid: body.groupId } },
|
|
||||||
team: { connect: { pid } },
|
|
||||||
},
|
|
||||||
select: returnedParticipant,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(201).json({
|
|
||||||
type: "success",
|
|
||||||
payload: { participant },
|
|
||||||
});
|
|
||||||
} 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}'`));
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||||
//insert TeamleaderAuth
|
//insert TeamleaderAuth
|
||||||
|
|
||||||
const result = ParticipantBody.partial().safeParse(req.body); // Should be partial, right?
|
const result = ParticipantBody.partial().safeParse(req.body); // Should be partial, right?
|
||||||
|
|
||||||
if(result.success === false){
|
if (result.success === false) {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError(
|
||||||
firstname: DataType.STRING,
|
{
|
||||||
lastName: DataType.STRING,
|
firstname: DataType.STRING,
|
||||||
groupId: DataType.UUID,
|
lastName: DataType.STRING,
|
||||||
}, result.error)
|
groupId: DataType.UUID,
|
||||||
);
|
},
|
||||||
|
result.error
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = result.data;
|
||||||
|
const { pid } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const participant = await prisma.participant.update({
|
||||||
|
where: { pid },
|
||||||
|
data: {
|
||||||
|
firstName: body.firstName,
|
||||||
|
lastName: body.lastName,
|
||||||
|
group: { connect: { pid: body.groupId } },
|
||||||
|
},
|
||||||
|
select: returnedParticipant,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
type: "success",
|
||||||
|
payload: { participant },
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
|
throw new NotFoundError("participant", pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = result.data;
|
throw e;
|
||||||
const { pid } = req.params;
|
}
|
||||||
|
};
|
||||||
try {
|
|
||||||
const participant = await prisma.participant.update({
|
|
||||||
where: { pid },
|
|
||||||
data: {
|
|
||||||
firstName: body.firstName,
|
|
||||||
lastName: body.lastName,
|
|
||||||
group: { connect: { pid: body.groupId, } },
|
|
||||||
},
|
|
||||||
select: returnedParticipant,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
type: "success",
|
|
||||||
payload: { participant },
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
|
||||||
throw new NotFoundError("participant", pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const deleteParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
export const deleteParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||||
//insert TeamleaderAuth
|
//insert TeamleaderAuth
|
||||||
|
|
||||||
const { pid } = req.params;
|
const { pid } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.participant.delete({ where: { pid } });
|
await prisma.participant.delete({ where: { pid } });
|
||||||
|
|
||||||
return res.status(204).end();
|
return res.status(204).end();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
return res.status(404).json(generateError(`The participant with the ID ${pid} could not be found`));
|
return res.status(404).json(generateError(`The participant with the ID ${pid} could not be found`));
|
||||||
}
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const TeamBody = z.object({
|
|||||||
partFirstName: z.string().min(1),
|
partFirstName: z.string().min(1),
|
||||||
partLastName: z.string().min(1),
|
partLastName: z.string().min(1),
|
||||||
partGroupId: z.string().uuid(),
|
partGroupId: z.string().uuid(),
|
||||||
})
|
});
|
||||||
|
|
||||||
interface CreateTeamBody {
|
interface CreateTeamBody {
|
||||||
teamName: string;
|
teamName: string;
|
||||||
@@ -28,19 +28,21 @@ interface CreateTeamBody {
|
|||||||
|
|
||||||
// TODO: Some kind of auth (Teamleader probably)
|
// TODO: Some kind of auth (Teamleader probably)
|
||||||
export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Response) => {
|
export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Response) => {
|
||||||
|
|
||||||
const result = TeamBody.safeParse(req.body);
|
const result = TeamBody.safeParse(req.body);
|
||||||
|
|
||||||
if (result.success === false) {
|
if (result.success === false) {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError(
|
||||||
teamName: DataType.STRING,
|
{
|
||||||
leaderEmail: DataType.STRING,
|
teamName: DataType.STRING,
|
||||||
disciplineId: DataType.UUID,
|
leaderEmail: DataType.STRING,
|
||||||
partFirstName: DataType.STRING,
|
disciplineId: DataType.UUID,
|
||||||
partLastName: DataType.STRING,
|
partFirstName: DataType.STRING,
|
||||||
partGroupId: DataType.UUID,
|
partLastName: DataType.STRING,
|
||||||
}, result.error)
|
partGroupId: DataType.UUID,
|
||||||
|
},
|
||||||
|
result.error
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +60,8 @@ export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Respon
|
|||||||
lastName: body.partLastName,
|
lastName: body.partLastName,
|
||||||
relevance: "TEAMLEADER",
|
relevance: "TEAMLEADER",
|
||||||
group: { connect: { pid: body.partGroupId } },
|
group: { connect: { pid: body.partGroupId } },
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
pid: true,
|
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);
|
createRolesForTeam(team.pid);
|
||||||
|
|
||||||
const usid = nanoid();
|
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