diff --git a/src/Controllers/role.controller.ts b/src/Controllers/role.controller.ts index 086343b..7995368 100644 --- a/src/Controllers/role.controller.ts +++ b/src/Controllers/role.controller.ts @@ -23,7 +23,7 @@ export async function createRolesForTeam(teamPid: string) { } const roles = await prisma.role.createMany({ - data: schemas.map((schema) => ({ schemaId: schema.id, score: "", teamId })), + data: schemas.map((schema) => ({ schemaId: schema.id, score: "", teamId })), // TODO: Use default score from schema? }); return roles.count; @@ -61,7 +61,7 @@ export async function assignParticipantToRole(req: Request<{ pid: string }>, res const zBody = AssignParticipantToRoleBody.safeParse(req.body); if (zBody.success === false) { - return res.status(400).json(generateInvalidBodyError({ participant: DataType.UUID })); + return res.status(400).json(generateInvalidBodyError({ participantPid: DataType.UUID }, zBody.error)); } const { participantPid } = zBody.data; @@ -83,6 +83,7 @@ export async function assignParticipantToRole(req: Request<{ pid: string }>, res }); } + // No error handling should be neccesary as the existence of the role and participant have already been checked above await prisma.role.update({ where: { pid: rolePid }, data: { participant: { connect: { pid: participantPid } } } }); return res.status(200).json({ @@ -107,8 +108,6 @@ export const updateRoleScore = async (req: Request<{ pid: string }, {}, { score: const { pid } = req.params; - - try { const role = await prisma.role.update({ where: { pid }, @@ -132,10 +131,6 @@ export const updateRoleScore = async (req: Request<{ pid: string }, {}, { score: } }); - if (!role) { - throw new NotFoundError("event", pid); - } - res.status(200).json({ type: "success", payload: { @@ -144,24 +139,8 @@ export const updateRoleScore = async (req: Request<{ pid: string }, {}, { score: }); } 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: { - eventId: DataType.UUID, - }, - }, - }); + if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") { + throw new NotFoundError("role", pid) } throw e; diff --git a/src/Controllers/role_schema.controller.ts b/src/Controllers/role_schema.controller.ts index dafcfe7..4b8adda 100644 --- a/src/Controllers/role_schema.controller.ts +++ b/src/Controllers/role_schema.controller.ts @@ -16,7 +16,7 @@ import { } from "./common"; const RoleSchemaBody = z.object({ - name: z.string(), + name: z.string().min(1), schema: z.string(), }); @@ -148,50 +148,31 @@ export const updateRoleSchema = async (req: Request<{ pid: string }>, res: Respo generateInvalidBodyError({ name: DataType.STRING, schema: DataType.RESULT_SCHEMA, - }) + }, result.error) ); } - const body = result.data; + const {name, schema} = result.data; + + const validatedSchema = parseSchema(schema); try { const schema = await prisma.roleSchema.update({ where: { pid }, data: { - name: body.name, - schema: body.schema, + name: name, + schema: validatedSchema, }, select: roleSchema, }); - if (!schema) { - throw new NotFoundError("schema", pid); - } - res.status(200).json({ type: "success", payload: schema, }); } 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, - schema: DataType.RESULT_SCHEMA, - }, - }, - }); + if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") { + throw new NotFoundError("roleSchema", pid) } throw e; diff --git a/src/Controllers/user_auth.controller.ts b/src/Controllers/user_auth.controller.ts index a8034eb..067addf 100644 --- a/src/Controllers/user_auth.controller.ts +++ b/src/Controllers/user_auth.controller.ts @@ -9,12 +9,12 @@ import { createRolesForTeam } from "./role.controller"; import { z } from "zod"; const TeamBody = z.object({ - teamName: z.string(), - leaderEmail: z.string(), - disciplineId: z.string(), - partFirstName: z.string(), - partLastName: z.string(), - partGroupId: z.string(), + teamName: z.string().min(1), + leaderEmail: z.string().email(), + disciplineId: z.string().uuid(), + partFirstName: z.string().min(1), + partLastName: z.string().min(1), + partGroupId: z.string().uuid(), }) interface CreateTeamBody { @@ -26,22 +26,22 @@ interface CreateTeamBody { partGroupId: string; } +// 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) { - res.status(400).json( + return res.status(400).json( generateInvalidBodyError({ teamName: DataType.STRING, leaderEmail: DataType.STRING, - disciplineId: DataType.STRING, + disciplineId: DataType.UUID, partFirstName: DataType.STRING, partLastName: DataType.STRING, - partGroupId: DataType.STRING, - }) + partGroupId: DataType.UUID, + }, result.error) ); - return; } const body = result.data; diff --git a/src/Routes/role.routes.ts b/src/Routes/role.routes.ts index 0c91fd1..5bbd03e 100644 --- a/src/Routes/role.routes.ts +++ b/src/Routes/role.routes.ts @@ -3,7 +3,7 @@ import { assignParticipantToRole, getRolesForTeam } from "../Controllers/role.co const router = Express.Router(); -//TO DO: maybe transfer getRolesForTeam to team router +//TO DO: maybe transfer getRolesForTeam to team router -> Seconded router.get<"team/:teamPid/", { teamPid: string }>("team/:teamPid/", getRolesForTeam); -router.patch<"/:pid/", { pid: string }>("/:pid/", assignParticipantToRole) \ No newline at end of file +router.put<"/:pid/participant", { pid: string }>("/:pid/participant", assignParticipantToRole);