mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
bug fixes in Discipline, Roleschema, Event & Media
This commit is contained in:
@@ -220,12 +220,12 @@ export const addVisual = async (req: Request<visualParams, {}, visualBody>, res:
|
||||
})
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED"){
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { disciplinePid } = req.params;
|
||||
const { disciplinePid, pid } = req.params;
|
||||
|
||||
try {
|
||||
await prisma.discipline.update({
|
||||
@@ -233,7 +233,7 @@ export const deleteVisual = async (req: Request<visualParams, {}, visualBody>, r
|
||||
pid: disciplinePid,
|
||||
},
|
||||
data: {
|
||||
visual: { disconnect: { pid: req.body.mediaPid } }
|
||||
visual: { disconnect: { pid } }
|
||||
}
|
||||
});
|
||||
return res.status(204).end();
|
||||
|
||||
@@ -5,6 +5,8 @@ import prisma from "../lib/prisma";
|
||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
|
||||
|
||||
require("express-async-errors");
|
||||
|
||||
export const getAllEvents = async (req: Request, res: Response) => {
|
||||
const events = await prisma.event.findMany({
|
||||
select: {
|
||||
@@ -53,6 +55,10 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
},
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
throw new NotFoundError("event", eventId);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
type: "success",
|
||||
payload: {
|
||||
@@ -61,7 +67,7 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
res.status(500).json({
|
||||
return res.status(500).json({
|
||||
type: "error",
|
||||
payload: {
|
||||
message: `Internal Server error occured. Try again later`,
|
||||
@@ -70,7 +76,7 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
return;
|
||||
}
|
||||
if (e instanceof Prisma.PrismaClientUnknownRequestError) {
|
||||
res.status(500).json({
|
||||
return res.status(500).json({
|
||||
type: "error",
|
||||
payload: {
|
||||
message: "Unknown error occurred with your request. Check if your parameters are correct",
|
||||
@@ -81,6 +87,8 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -92,14 +100,15 @@ export const addEvent = async (req: Request, res: Response) => {
|
||||
if (
|
||||
typeof req.body.name !== "string" ||
|
||||
typeof req.body.date !== "string" ||
|
||||
typeof req.body.description !== "string" ||
|
||||
req.body.fullDescription && req.body.fullDescription !== "string"
|
||||
typeof req.body.briefDescription !== "string" ||
|
||||
req.body.fullDescription && typeof req.body.fullDescription !== "string"
|
||||
) {
|
||||
generateInvalidBodyError({
|
||||
return res.status(400).json(generateInvalidBodyError({
|
||||
name: DataType.STRING,
|
||||
date: DataType.DATETIME,
|
||||
description: DataType.STRING,
|
||||
});
|
||||
briefDescription: DataType.STRING,
|
||||
["fullDescription?"]: DataType.STRING,
|
||||
}));
|
||||
}
|
||||
|
||||
//TODO: Check if date is valid
|
||||
@@ -108,7 +117,8 @@ export const addEvent = async (req: Request, res: Response) => {
|
||||
data: {
|
||||
name: req.body.name,
|
||||
date: req.body.date,
|
||||
briefDescription: req.body.description,
|
||||
briefDescription: req.body.briefDescription,
|
||||
fullDescription: req.body.fullDescription,
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
@@ -133,7 +143,7 @@ interface DeleteEventQueryParams {
|
||||
}
|
||||
|
||||
// requires: auth(ELEVATED)
|
||||
export const deleteEvent = (req: Request<DeleteEventQueryParams>, res: Response) => {
|
||||
export const deleteEvent = async (req: Request<DeleteEventQueryParams>, res: Response) => {
|
||||
if (req.auth?.permission_level !== "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
@@ -141,7 +151,7 @@ export const deleteEvent = (req: Request<DeleteEventQueryParams>, res: Response)
|
||||
const { pid } = req.params;
|
||||
|
||||
try {
|
||||
prisma.event.delete({ where: { pid } });
|
||||
await prisma.event.delete({ where: { pid } });
|
||||
|
||||
return res.status(204).end();
|
||||
} catch (e) {
|
||||
@@ -191,12 +201,12 @@ export const addVisual = async (req: Request<visualParams, {}, visualBody>, res:
|
||||
})
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED"){
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { eventPid } = req.params;
|
||||
const { eventPid, pid } = req.params;
|
||||
|
||||
try {
|
||||
await prisma.event.update({
|
||||
@@ -204,7 +214,7 @@ export const deleteVisual = async (req: Request<visualParams, {}, visualBody>, r
|
||||
pid: eventPid,
|
||||
},
|
||||
data: {
|
||||
visual: { disconnect: { pid: req.body.mediaPid } }
|
||||
visual: { disconnect: { pid } },
|
||||
}
|
||||
});
|
||||
return res.status(204).end();
|
||||
|
||||
@@ -132,9 +132,9 @@ export const getAllMedia = async (req: Request, res: Response) => {
|
||||
const medias = await prisma.media.findMany({
|
||||
select: {
|
||||
description: true,
|
||||
events: true,
|
||||
disciplines: true,
|
||||
roles: true,
|
||||
events: {select: {pid: true}},
|
||||
disciplines: {select: {pid: true}},
|
||||
roles: {select: {pid: true}},
|
||||
pid: true,
|
||||
id: false,
|
||||
},
|
||||
|
||||
@@ -156,12 +156,12 @@ export const addVisual = async (req: Request<visualParams, {}, visualBody>, res:
|
||||
})
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string}>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED"){
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { schemaPid } = req.params;
|
||||
const { schemaPid, pid } = req.params;
|
||||
|
||||
try {
|
||||
await prisma.roleSchema.update({
|
||||
@@ -169,7 +169,7 @@ export const deleteVisual = async (req: Request<visualParams, {}, visualBody>, r
|
||||
pid: schemaPid,
|
||||
},
|
||||
data: {
|
||||
visual: { disconnect: { pid: req.body.mediaPid } }
|
||||
visual: { disconnect: { pid } }
|
||||
}
|
||||
});
|
||||
return res.status(204).end();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Express from "express";
|
||||
import { string } from "zod";
|
||||
import { addEvent, addVisual, deleteVisual, getAllEvents, getEvent } from "../Controllers/event.controller";
|
||||
import { addEvent, addVisual, deleteEvent, deleteVisual, getAllEvents, getEvent } from "../Controllers/event.controller";
|
||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||
const router = Express.Router();
|
||||
|
||||
@@ -10,6 +10,8 @@ router.get("/:eventId", getEvent);
|
||||
|
||||
router.post("/", requireAuthentication, addEvent);
|
||||
|
||||
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, deleteEvent);
|
||||
|
||||
router.post<"/:eventPid/media", { eventPid: string }>("/:eventPid/media", requireAuthentication, addVisual);
|
||||
|
||||
router.delete<"/:eventPid/media/:pid", { eventPid: string; pid: string }>(
|
||||
|
||||
@@ -2,6 +2,9 @@ import express from "express";
|
||||
import fileUpload from "express-fileupload";
|
||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||
import { deleteMedia, getAllMedia, getMediaMeta, uploadImage } from "../Controllers/media.controller";
|
||||
import eventRouter from "./event.routes";
|
||||
import disciplineRouter from "./discipline.routes";
|
||||
import roleSchemaRouter from "./role_schema.routes";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -11,6 +14,7 @@ router.use("/", express.static("/app/media", { redirect: false }));
|
||||
router.post("/", requireAuthentication, fileUpload(), uploadImage);
|
||||
|
||||
router.get("/", requireAuthentication, getAllMedia); // Reading all files should be a senstive operation
|
||||
|
||||
router.get("/:pid/meta", getMediaMeta);
|
||||
|
||||
router.delete("/:pid", requireAuthentication, deleteMedia);
|
||||
|
||||
Reference in New Issue
Block a user