Skip to content

Commit

Permalink
patching complications after merging and review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Flexla54 committed Jun 1, 2022
1 parent 95784a2 commit 5696952
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 310 deletions.
90 changes: 9 additions & 81 deletions src/Controllers/discipline.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Prisma, Organisation, Admin, AdminLevel, Team } from "@prisma/client";
import { PrismaClientKnownRequestError, PrismaClientUnknownRequestError } from "@prisma/client/runtime";
import { Request, Response } from "express";
import { z } from "zod";
import prisma from "../lib/prisma";
import ForwardableError from "../Middleware/error/ForwardableError";
import NotFoundError from "../Middleware/error/NotFoundError";
Expand All @@ -17,19 +16,22 @@ import {

require("express-async-errors");


const InitialDisciplineBody = z.object({
name: z.string().min(1),
minTeamSize: z.number(),
maxTeamSize: z.number(),
briefDescription: z.string(),
fullDescription: z.string(),
});

const disciplineRefiner = [
(args: any) => (args.minTeamSize && args.maxTeamSize ? args.minTeamSize <= args.maxTeamSize : true),
{ message: "The minTeamSize must be smaller or equal to the maxTeamSize" },
] as const;

const DisciplineBody = InitialDisciplineBody.refine(...disciplineRefiner);
const DisciplineBody = InitialDisciplineBody.partial({ briefDescription: true, fullDescription: true }).refine(
...disciplineRefiner
);
const updateDisciplineBody = InitialDisciplineBody.partial().refine(...disciplineRefiner);

const basicDiscipline = {
Expand Down Expand Up @@ -132,84 +134,6 @@ export const getDiscipline = async (req: Request<GetDisciplineQueryParams>, res:
});
};

export const updateDiscipline = async (req: Request<{ pid: string }>, res: Response) => {
if (req.auth?.permission_level !== "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}

const { pid } = req.params;

const result = UpdateDisciplineBody.safeParse(req.body);

if (result.success === false) {
return res.status(400).json(
generateInvalidBodyError({
name: DataType.STRING,
minTeamSize: DataType.NUMBER,
maxTeamSize: DataType.NUMBER,
briefDescription: DataType.STRING,
["fullDescription?"]: DataType.STRING,
})
);
}

const body = result.data;

try {
const discipline = await prisma.discipline.update({
where: { pid },
data: {
name: body.name,
minTeamSize: body.minTeamSize,
maxTeamSize: body.maxTeamSize,
briefDescription: body.briefDescription,
fullDescription: body.fullDescription,
},
select: {
pid: true,
name: true,
minTeamSize: true,
maxTeamSize: true,
briefDescription: true,
fullDescription: true,
},
});

if (!discipline) {
throw new NotFoundError("discipline", pid);
}

res.status(200).json({
type: "success",
payload: {
discipline,
},
});
} 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,
},
},
});
}

throw e;
}
};

interface CreateDisciplineBody {
name?: string;
minTeamSize?: number;
Expand Down Expand Up @@ -272,6 +196,8 @@ export const updateDiscipline = async (req: Request<{ pid: string }>, res: Respo
name: DataType.STRING,
minTeamSize: DataType.NUMBER,
maxTeamSize: DataType.NUMBER,
briefDescription: DataType.STRING,
["fullDescription?"]: DataType.STRING,
},
result.error
)
Expand All @@ -288,6 +214,8 @@ export const updateDiscipline = async (req: Request<{ pid: string }>, res: Respo
name: body.name,
minTeamSize: body.minTeamSize,
maxTeamSize: body.maxTeamSize,
briefDescription: body.briefDescription,
fullDescription: body.fullDescription,
},
select: basicDiscipline,
});
Expand Down
122 changes: 43 additions & 79 deletions src/Controllers/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import { createInsufficientPermissionsError, DataType, generateError, generateIn

require("express-async-errors");

export const dateSchema = z.preprocess((arg) => {
if (typeof arg == "string" || arg instanceof Date) return new Date(arg);
}, z.date());

const EventBody = z.object({
name: z.string(),
date: z.string(),
name: z.string().min(1),
date: dateSchema,
briefDescription: z.string(),
fullDescription: z.string(),
});
Expand All @@ -21,27 +25,32 @@ const CreateEventBody = EventBody.partial({
fullDescription: true,
});

export const getAllEvents = async (req: Request, res: Response) => {
const events = await prisma.event.findMany({
const basicEvent = {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
} as const;

const detailedEvent = {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
visual: { select: { pid: true, description: true } },
disciplines: {
select: {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
visual: { select: { pid: true, description: true } },
disciplines: {
select: {
pid: true,
name: true,
}},
organisations: {
select: {
pid: true,
name: true,
}
}
},
},
} as const;

export const getAllEvents = async (req: Request, res: Response) => {
const events = await prisma.event.findMany({
select: detailedEvent,
});

res.status(200).json({
Expand All @@ -68,27 +77,7 @@ export const getEvent = async (req: Request, res: Response) => {
where: {
pid: eventId,
},
select: {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
visual: { select: { pid: true, description: true } },
disciplines: {
select: {
pid: true,
name: true,
briefDescription: true,
fullDescription: true,
}},
organisations: {
select: {
pid: true,
name: true,
}
}
},
select: detailedEvent,
});

if (!event) {
Expand Down Expand Up @@ -134,33 +123,28 @@ export const addEvent = async (req: Request, res: Response) => {

const result = CreateEventBody.safeParse(req.body);

if(result.success === false){
if (result.success === false) {
return res.status(400).json(
generateInvalidBodyError({
name: DataType.STRING,
date: DataType.DATETIME,
briefDescription: DataType.STRING,
["fullDescription?"]: DataType.STRING,
})
generateInvalidBodyError(
{
name: DataType.STRING,
date: DataType.DATETIME,
briefDescription: DataType.STRING,
["fullDescription?"]: DataType.STRING,
},
result.error
)
);
}

//TODO: Check if date is valid

const event = await prisma.event.create({
data: {
name: req.body.name,
date: req.body.date,
briefDescription: req.body.briefDescription,
fullDescription: req.body.fullDescription,
},
select: {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
},
select: basicEvent,
});

res.status(201).json({
Expand Down Expand Up @@ -214,35 +198,15 @@ export const updateEvent = async (req: Request<{ pid: string }>, res: Response)
},
});

if (!event) {
throw new NotFoundError("event", pid);
}

res.status(200).json({
type: "success",
payload: {
event,
},
});
} 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("discipline", pid);
}

throw e;
Expand All @@ -267,7 +231,7 @@ export const deleteEvent = async (req: Request<DeleteEventQueryParams>, res: Res
return res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return res.status(404).json(generateError(`The event with the ID ${pid} could not be found`));
throw new NotFoundError("discipline", pid);
}

throw e;
Expand Down
Loading

0 comments on commit 5696952

Please sign in to comment.