diff --git a/src/Controllers/group.controllers.ts b/src/Controllers/group.controllers.ts index 012e6f8..eaea779 100644 --- a/src/Controllers/group.controllers.ts +++ b/src/Controllers/group.controllers.ts @@ -6,11 +6,13 @@ import prisma from "../lib/prisma"; import NotFoundError from "../Middleware/error/NotFoundError"; import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError, genericError, handleCreateByName } from "./common"; -const updateGroupBody = z.object({ - name: z.string(), - user_limit: z.number(), - level: z.number(), -}).partial(); +const updateGroupBody = z + .object({ + name: z.string().min(1), + user_limit: z.number().int().positive(), + level: z.number().int().nonnegative(), + }) + .partial(); const basicGroup = { pid: true, @@ -131,11 +133,14 @@ export const updateGroup = async (req: Request<{ pid: string }>, res: Response) if(result.success === false){ return res.status(400).json( - generateInvalidBodyError({ - name: DataType.STRING, - user_limit: DataType.NUMBER, - level: DataType.NUMBER, - }) + generateInvalidBodyError( + { + name: DataType.STRING, + user_limit: DataType.NUMBER, + level: DataType.NUMBER, + }, + result.error + ) ); } @@ -153,39 +158,17 @@ export const updateGroup = async (req: Request<{ pid: string }>, res: Response) select: basicGroup, }); - if(!group) { - throw new NotFoundError("group", pid); - } - res.status(200).json({ type: "success", - payload: group, + payload: { group }, }); } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - return res.status(500).json({ - type: "error", - payload: { - message: `Internal Server error occured. Try again later`, - }, - }); - } - if (e instanceof Prisma.PrismaClientUnknownRequestError) { - return res.status(500).json({ - type: "error", - payload: { - message: "Unknown error occurred with your request. Check if your parameters are correct", - schema: { - name: DataType.STRING, - user_limit: DataType.NUMBER, - level: DataType.NUMBER, - }, - }, - }); - } + if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") { + throw new NotFoundError("group", pid) + } - throw e; + throw e; } } @@ -206,7 +189,7 @@ export const deleteGroup = async (req: Request, res: Res return res.status(204).end(); } catch (e) { if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") { - return res.status(404).json(generateError(`The group with the ID ${pid} could not be found`)); + throw new NotFoundError("group", pid) } throw e; diff --git a/src/Controllers/participant.controller.ts b/src/Controllers/participant.controller.ts index d5696d1..8a1bb55 100644 --- a/src/Controllers/participant.controller.ts +++ b/src/Controllers/participant.controller.ts @@ -8,10 +8,13 @@ import NotFoundError from "../Middleware/error/NotFoundError"; //TODO: add TeamleaderAuthentification +// 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({ firstName: z.string(), lastName: z.string(), - groupId: z.string(), + groupId: z.string().uuid(), //job: z.enum(["TEAMLEADER", "MEMBER"]), }); @@ -30,6 +33,7 @@ const returnedParticipant = { } }, } as const; +// REVIEW: Location of this endpoints (/groups, /teams, /participants, ...?) export const createParticipant = async (req: Request<{ pid: string}>, res: Response) => { //insert TeamleaderAuth @@ -41,7 +45,7 @@ export const createParticipant = async (req: Request<{ pid: string}>, res: Respo firstname: DataType.STRING, lastName: DataType.STRING, groupId: DataType.UUID, - }) + }, result.error) ); } @@ -62,7 +66,7 @@ export const createParticipant = async (req: Request<{ pid: string}>, res: Respo return res.status(201).json({ type: "success", - payload: participant, + payload: { participant }, }); } catch (e) { if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") { @@ -75,7 +79,7 @@ export const createParticipant = async (req: Request<{ pid: string}>, res: Respo export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => { //insert TeamleaderAuth - const result = ParticipantBody.safeParse(req.body); + const result = ParticipantBody.partial().safeParse(req.body); // Should be partial, right? if(result.success === false){ return res.status(400).json( @@ -83,7 +87,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp firstname: DataType.STRING, lastName: DataType.STRING, groupId: DataType.UUID, - }) + }, result.error) ); } @@ -101,38 +105,16 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp select: returnedParticipant, }); - if(!participant) { - throw new NotFoundError("participant", pid); - } - res.status(200).json({ type: "success", - payload: participant, + payload: { participant }, }); } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - return res.status(500).json({ - type: "error", - payload: { - message: `Internal Server error occured. Try again later`, - }, - }); + if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") { + throw new NotFoundError("participant", pid) } - if (e instanceof Prisma.PrismaClientUnknownRequestError) { - return res.status(500).json({ - type: "error", - payload: { - message: "Unknown error occurred with your request. Check if your parameters are correct", - schema: { - firstname: DataType.STRING, - lastName: DataType.STRING, - groupId: DataType.UUID, - }, - }, - }); - } - + throw e; } }