Improve validation and error handling

+ Check code and routes
+ Comment code
This commit is contained in:
Stephan
2022-05-29 13:25:11 +02:00
parent c41bbe9ab4
commit 197fd9c654
4 changed files with 27 additions and 67 deletions
+5 -26
View File
@@ -23,7 +23,7 @@ export async function createRolesForTeam(teamPid: string) {
} }
const roles = await prisma.role.createMany({ 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; return roles.count;
@@ -61,7 +61,7 @@ export async function assignParticipantToRole(req: Request<{ pid: string }>, res
const zBody = AssignParticipantToRoleBody.safeParse(req.body); const zBody = AssignParticipantToRoleBody.safeParse(req.body);
if (zBody.success === false) { 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; 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 } } } }); await prisma.role.update({ where: { pid: rolePid }, data: { participant: { connect: { pid: participantPid } } } });
return res.status(200).json({ return res.status(200).json({
@@ -107,8 +108,6 @@ export const updateRoleScore = async (req: Request<{ pid: string }, {}, { score:
const { pid } = req.params; const { pid } = req.params;
try { try {
const role = await prisma.role.update({ const role = await prisma.role.update({
where: { pid }, 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({ res.status(200).json({
type: "success", type: "success",
payload: { payload: {
@@ -144,24 +139,8 @@ export const updateRoleScore = async (req: Request<{ pid: string }, {}, { score:
}); });
} catch (e) { } catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) { if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
return res.status(500).json({ throw new NotFoundError("role", pid)
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,
},
},
});
} }
throw e; throw e;
+9 -28
View File
@@ -16,7 +16,7 @@ import {
} from "./common"; } from "./common";
const RoleSchemaBody = z.object({ const RoleSchemaBody = z.object({
name: z.string(), name: z.string().min(1),
schema: z.string(), schema: z.string(),
}); });
@@ -148,50 +148,31 @@ export const updateRoleSchema = async (req: Request<{ pid: string }>, res: Respo
generateInvalidBodyError({ generateInvalidBodyError({
name: DataType.STRING, name: DataType.STRING,
schema: DataType.RESULT_SCHEMA, schema: DataType.RESULT_SCHEMA,
}) }, result.error)
); );
} }
const body = result.data; const {name, schema} = result.data;
const validatedSchema = parseSchema(schema);
try { try {
const schema = await prisma.roleSchema.update({ const schema = await prisma.roleSchema.update({
where: { pid }, where: { pid },
data: { data: {
name: body.name, name: name,
schema: body.schema, schema: validatedSchema,
}, },
select: roleSchema, select: roleSchema,
}); });
if (!schema) {
throw new NotFoundError("schema", pid);
}
res.status(200).json({ res.status(200).json({
type: "success", type: "success",
payload: schema, payload: schema,
}); });
} catch (e) { } catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) { if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
return res.status(500).json({ throw new NotFoundError("roleSchema", pid)
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,
},
},
});
} }
throw e; throw e;
+11 -11
View File
@@ -9,12 +9,12 @@ import { createRolesForTeam } from "./role.controller";
import { z } from "zod"; import { z } from "zod";
const TeamBody = z.object({ const TeamBody = z.object({
teamName: z.string(), teamName: z.string().min(1),
leaderEmail: z.string(), leaderEmail: z.string().email(),
disciplineId: z.string(), disciplineId: z.string().uuid(),
partFirstName: z.string(), partFirstName: z.string().min(1),
partLastName: z.string(), partLastName: z.string().min(1),
partGroupId: z.string(), partGroupId: z.string().uuid(),
}) })
interface CreateTeamBody { interface CreateTeamBody {
@@ -26,22 +26,22 @@ interface CreateTeamBody {
partGroupId: string; partGroupId: string;
} }
// 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) {
res.status(400).json( return res.status(400).json(
generateInvalidBodyError({ generateInvalidBodyError({
teamName: DataType.STRING, teamName: DataType.STRING,
leaderEmail: DataType.STRING, leaderEmail: DataType.STRING,
disciplineId: DataType.STRING, disciplineId: DataType.UUID,
partFirstName: DataType.STRING, partFirstName: DataType.STRING,
partLastName: DataType.STRING, partLastName: DataType.STRING,
partGroupId: DataType.STRING, partGroupId: DataType.UUID,
}) }, result.error)
); );
return;
} }
const body = result.data; const body = result.data;
+2 -2
View File
@@ -3,7 +3,7 @@ import { assignParticipantToRole, getRolesForTeam } from "../Controllers/role.co
const router = Express.Router(); 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.get<"team/:teamPid/", { teamPid: string }>("team/:teamPid/", getRolesForTeam);
router.patch<"/:pid/", { pid: string }>("/:pid/", assignParticipantToRole) router.put<"/:pid/participant", { pid: string }>("/:pid/participant", assignParticipantToRole);