merged f-endpoints and f-roles

This commit is contained in:
Laurin
2022-05-31 07:44:26 +02:00
parent 0c0b2c3ced
commit 6edaf890b1
10 changed files with 255 additions and 325 deletions
+83 -61
View File
@@ -1,10 +1,10 @@
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";
import { number, z } from "zod";
import {
createInsufficientPermissionsError,
DataType,
@@ -36,6 +36,8 @@ const basicDiscipline = {
visual: { select: { pid: true } },
maxTeamSize: true,
minTeamSize: true,
briefDescription: true,
fullDescription: true,
event: { select: { pid: true, name: true } },
roles: { select: { pid: true, name: true } },
} as const;
@@ -128,10 +130,90 @@ 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;
maxTeamSize?: number;
briefDescription?: string;
fullDescription?: string;
}
// require: auth(ELEVATED)
@@ -241,63 +323,3 @@ export const deleteDiscipline = async (req: Request<{ pid: string }>, res: Respo
throw e;
}
};
interface visualParams {
disciplinePid: string;
}
interface visualBody {
mediaPid: string;
}
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { disciplinePid } = req.params;
const discipline = await prisma.discipline.update({
where: { pid: disciplinePid },
data: {
visual: { connect: { pid: req.body.mediaPid } },
},
});
// TODO: This does not work and should be updated in all addVisual-type code segments
// Reason: update throw a PrismaClientKnownRequestError with code P2025 if the record to update could not be found
if (!discipline) {
throw new NotFoundError("discipline", disciplinePid);
}
return res.status(200).json({
type: "success",
payload: {},
});
};
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { disciplinePid, pid } = req.params;
try {
await prisma.discipline.update({
where: {
pid: disciplinePid,
},
data: {
visual: { disconnect: { pid } },
},
});
return res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("discipline", disciplinePid); // Refer: Last todo; This is a correct example
}
throw e;
}
};
+47 -88
View File
@@ -1,6 +1,6 @@
import { Prisma } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
import { Request, Response } from "express";
import e, { Request, Response } from "express";
import { z } from "zod";
import prisma from "../lib/prisma";
import NotFoundError from "../Middleware/error/NotFoundError";
@@ -8,16 +8,39 @@ import { createInsufficientPermissionsError, DataType, generateError, generateIn
require("express-async-errors");
const EventBody = z.object({
name: z.string(),
date: z.string(),
briefDescription: z.string(),
fullDescription: z.string(),
});
const UpdateBody = EventBody.partial();
const CreateEventBody = EventBody.partial({
fullDescription: true,
});
export const getAllEvents = async (req: Request, res: Response) => {
const events = await prisma.event.findMany({
select: {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
visual: { select: { pid: true, description: true } },
date: true,
pid: true,
id: false,
disciplines: {
select: {
pid: true,
name: true,
}},
organisations: {
select: {
pid: true,
name: true,
}
}
},
});
@@ -46,13 +69,25 @@ export const getEvent = async (req: Request, res: Response) => {
pid: eventId,
},
select: {
pid: true,
name: true,
date: true,
briefDescription: true,
fullDescription: true,
date: true,
pid: true,
id: false,
visual: { select: { pid: true, description: true } },
disciplines: {
select: {
pid: true,
name: true,
briefDescription: true,
fullDescription: true,
}},
organisations: {
select: {
pid: true,
name: true,
}
}
},
});
@@ -96,12 +131,10 @@ export const addEvent = async (req: Request, res: Response) => {
if (req.auth?.permission_level !== "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
if (
typeof req.body.name !== "string" ||
typeof req.body.date !== "string" ||
typeof req.body.briefDescription !== "string" ||
(req.body.fullDescription && typeof req.body.fullDescription !== "string")
) {
const result = CreateEventBody.safeParse(req.body);
if(result.success === false){
return res.status(400).json(
generateInvalidBodyError({
name: DataType.STRING,
@@ -122,10 +155,9 @@ export const addEvent = async (req: Request, res: Response) => {
fullDescription: req.body.fullDescription,
},
select: {
pid: true,
name: true,
date: true,
pid: true,
id: false,
briefDescription: true,
fullDescription: true,
},
@@ -139,15 +171,6 @@ export const addEvent = async (req: Request, res: Response) => {
});
};
const EventBody = z.object({
name: z.string(),
date: z.string(),
briefDescription: z.string(),
fullDescription: z.string(),
});
const UpdateBody = EventBody.partial();
export const updateEvent = async (req: Request<{ pid: string }>, res: Response) => {
if (req.auth?.permission_level !== "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
@@ -250,67 +273,3 @@ export const deleteEvent = async (req: Request<DeleteEventQueryParams>, res: Res
throw e;
}
};
// REVIEW: This code **will** need to be de-duplicated
interface visualParams {
eventPid: string;
}
interface visualBody {
mediaPid: string;
}
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { eventPid } = req.params;
if (typeof req.body.mediaPid !== "string") {
res.status(400).json(generateInvalidBodyError({ mediaPid: DataType.STRING }));
}
const event = await prisma.event.update({
where: { pid: eventPid },
data: {
visual: { connect: { pid: req.body.mediaPid } },
},
});
if (!event) {
throw new NotFoundError("event", eventPid);
}
return res.status(200).json({
type: "success",
payload: {},
});
};
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { eventPid, pid } = req.params;
try {
await prisma.event.update({
where: {
pid: eventPid,
},
data: {
visual: { disconnect: { pid } },
},
});
return res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("discipline", eventPid);
}
throw e;
}
};
+87 -1
View File
@@ -1,4 +1,4 @@
import { Request, response, Response } from "express";
import { NextFunction, Request, response, Response } from "express";
import fs from "fs";
import isSvg from "is-svg";
import { fromBuffer as fileTypeFromBuffer } from "file-type";
@@ -12,6 +12,8 @@ import { type } from "os";
import { unlink } from "fs/promises";
import ForwardableError from "../Middleware/error/ForwardableError";
import SchemaError from "../Middleware/error/SchemaError";
import { z } from "zod";
import { updateEvent } from "./event.controller";
require("express-async-errors");
@@ -189,3 +191,87 @@ export const deleteMedia = async (req: Request, res: Response) => {
throw e;
}
};
//TODO: maybe create a function that adds the tableToUpdate based on path
// and call it before calling (un)linkMedia
export const linkMedia = async (
req: Request<{ pid: string }, {}, { mediaPid: string }>,
res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { pid } = req.params;
const { mediaPid } = req.body;
const tableToUpdate = req.originalUrl.split("/");
if(typeof mediaPid !== "string") {
return res.status(400).json(
generateInvalidBodyError({
mediaPid: DataType.UUID,
})
);
}
const updatedRec = await getPrismaUpdateFKT(tableToUpdate[2])({
where: { pid },
data: {
visual: { connect: { pid: mediaPid } },
},
});
if (!updatedRec) {
throw new NotFoundError(tableToUpdate[2], pid);
}
return res.status(200).json({
type: "success",
payload: {},
});
};
export const unlinkMedia = async (
req: Request<{ pid: string, mediaPid: string }>,
res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { pid, mediaPid } = req.params;
const tableToUpdate = req.originalUrl.split("/");
try {
await getPrismaUpdateFKT(tableToUpdate[2])({
where: { pid },
data: { visual: { disconnect: { pid: mediaPid } } },
});
return res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
console.log("not found error");
throw new NotFoundError(tableToUpdate[2], 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: {
eventId: DataType.UUID,
},
},
});
}
throw e;
}
};
function getPrismaUpdateFKT( tableToUpdate: string ): Function {
switch(tableToUpdate) {
case "events": return prisma.event.update;
case "disciplines": return prisma.discipline.update;
default: return prisma.roleSchema.update;
}
}
-126
View File
@@ -133,129 +133,3 @@ export const createRoleSchema = async (
throw e;
}
};
export const updateRoleSchema = async (req: Request<{ pid: string }>, res: Response) => {
if (req.auth?.permission_level !== "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { pid } = req.params;
const result = UpdateRoleSchema.safeParse(req.body);
if (result.success === false) {
return res.status(400).json(
generateInvalidBodyError(
{
name: DataType.STRING,
schema: DataType.RESULT_SCHEMA,
},
result.error
)
);
}
const { name, schema } = result.data;
const validatedSchema = parseSchema(schema);
try {
const schema = await prisma.roleSchema.update({
where: { pid },
data: {
name: name,
schema: validatedSchema,
},
select: roleSchema,
});
res.status(200).json({
type: "success",
payload: schema,
});
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("roleSchema", pid);
}
throw e;
}
};
export const deleteRoleSchema = async (req: Request<{ pid: string }>, res: Response) => {
if (req.auth?.permission_level !== "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { pid } = req.params;
try {
await prisma.roleSchema.delete({ where: { pid } });
return res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return res.status(404).json(generateError(`The RoleSchema with the ID ${pid} could not be found`));
}
throw e;
}
};
interface visualParams {
schemaPid: string;
}
interface visualBody {
mediaPid: string;
}
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { schemaPid } = req.params;
const schema = await prisma.roleSchema.update({
where: { pid: schemaPid },
data: {
visual: { connect: { pid: req.body.mediaPid } },
},
});
if (!schema) {
throw new NotFoundError("role_schema", schemaPid);
}
return res.status(200).json({
type: "success",
payload: {},
});
};
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
if (req.auth?.permission_level != "ELEVATED") {
res.status(403).json(createInsufficientPermissionsError());
}
const { schemaPid, pid } = req.params;
try {
await prisma.roleSchema.update({
where: {
pid: schemaPid,
},
data: {
visual: { disconnect: { pid } },
},
});
return res.status(204).end();
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("role_schema", schemaPid);
}
throw e;
}
};