mirror of
https://github.com/detleph/server.git
synced 2026-09-07 23:44:40 +02:00
Add error handling and comment code
This commit is contained in:
@@ -6,11 +6,13 @@ import prisma from "../lib/prisma";
|
|||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError, genericError, handleCreateByName } from "./common";
|
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError, genericError, handleCreateByName } from "./common";
|
||||||
|
|
||||||
const updateGroupBody = z.object({
|
const updateGroupBody = z
|
||||||
name: z.string(),
|
.object({
|
||||||
user_limit: z.number(),
|
name: z.string().min(1),
|
||||||
level: z.number(),
|
user_limit: z.number().int().positive(),
|
||||||
}).partial();
|
level: z.number().int().nonnegative(),
|
||||||
|
})
|
||||||
|
.partial();
|
||||||
|
|
||||||
const basicGroup = {
|
const basicGroup = {
|
||||||
pid: true,
|
pid: true,
|
||||||
@@ -131,11 +133,14 @@ export const updateGroup = async (req: Request<{ pid: string }>, res: Response)
|
|||||||
|
|
||||||
if(result.success === false){
|
if(result.success === false){
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError(
|
||||||
name: DataType.STRING,
|
{
|
||||||
user_limit: DataType.NUMBER,
|
name: DataType.STRING,
|
||||||
level: DataType.NUMBER,
|
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,
|
select: basicGroup,
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!group) {
|
|
||||||
throw new NotFoundError("group", pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
payload: group,
|
payload: { group },
|
||||||
});
|
});
|
||||||
|
|
||||||
} 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("group", 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,
|
|
||||||
user_limit: DataType.NUMBER,
|
|
||||||
level: DataType.NUMBER,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +189,7 @@ export const deleteGroup = async (req: Request<DeleteGroupQueryParams>, res: Res
|
|||||||
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 group with the ID ${pid} could not be found`));
|
throw new NotFoundError("group", pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@@ -8,10 +8,13 @@ import NotFoundError from "../Middleware/error/NotFoundError";
|
|||||||
|
|
||||||
//TODO: add TeamleaderAuthentification
|
//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({
|
const ParticipantBody = z.object({
|
||||||
firstName: z.string(),
|
firstName: z.string(),
|
||||||
lastName: z.string(),
|
lastName: z.string(),
|
||||||
groupId: z.string(),
|
groupId: z.string().uuid(),
|
||||||
//job: z.enum(["TEAMLEADER", "MEMBER"]),
|
//job: z.enum(["TEAMLEADER", "MEMBER"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -30,6 +33,7 @@ const returnedParticipant = {
|
|||||||
} },
|
} },
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
// 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
|
//insert TeamleaderAuth
|
||||||
|
|
||||||
@@ -41,7 +45,7 @@ export const createParticipant = async (req: Request<{ pid: string}>, res: Respo
|
|||||||
firstname: DataType.STRING,
|
firstname: DataType.STRING,
|
||||||
lastName: DataType.STRING,
|
lastName: DataType.STRING,
|
||||||
groupId: DataType.UUID,
|
groupId: DataType.UUID,
|
||||||
})
|
}, result.error)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +66,7 @@ export const createParticipant = async (req: Request<{ pid: string}>, res: Respo
|
|||||||
|
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
payload: participant,
|
payload: { participant },
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
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) => {
|
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||||
//insert TeamleaderAuth
|
//insert TeamleaderAuth
|
||||||
|
|
||||||
const result = ParticipantBody.safeParse(req.body);
|
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(
|
||||||
@@ -83,7 +87,7 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
firstname: DataType.STRING,
|
firstname: DataType.STRING,
|
||||||
lastName: DataType.STRING,
|
lastName: DataType.STRING,
|
||||||
groupId: DataType.UUID,
|
groupId: DataType.UUID,
|
||||||
})
|
}, result.error)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,36 +105,14 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
select: returnedParticipant,
|
select: returnedParticipant,
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!participant) {
|
|
||||||
throw new NotFoundError("participant", pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
payload: participant,
|
payload: { participant },
|
||||||
});
|
});
|
||||||
|
|
||||||
} 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("participant", 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: {
|
|
||||||
firstname: DataType.STRING,
|
|
||||||
lastName: DataType.STRING,
|
|
||||||
groupId: DataType.UUID,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
Reference in New Issue
Block a user