mirror of
https://github.com/detleph/server.git
synced 2026-09-04 00:26:03 +02:00
more bug fixes and add update to event
This commit is contained in:
@@ -18,7 +18,7 @@ require("express-async-errors");
|
||||
const basicDiscipline = {
|
||||
pid: true,
|
||||
name: true,
|
||||
visual: { select: { pid: true, location: true } },
|
||||
visual: { select: { pid: true } },
|
||||
maxTeamSize: true,
|
||||
minTeamSize: true,
|
||||
event: { select: { pid: true, name: true } },
|
||||
@@ -206,7 +206,7 @@ export const addVisual = async (req: Request<visualParams, {}, visualBody>, res:
|
||||
const discipline = await prisma.discipline.update({
|
||||
where: { pid: disciplinePid },
|
||||
data: {
|
||||
visual: { connect: { pid: req.body.mediaPid } }
|
||||
visual: {connect: {pid: req.body.mediaPid}}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||
import { Request, Response } from "express";
|
||||
import { z } from "zod";
|
||||
import prisma from "../lib/prisma";
|
||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
|
||||
@@ -13,7 +14,7 @@ export const getAllEvents = async (req: Request, res: Response) => {
|
||||
name: true,
|
||||
briefDescription: true,
|
||||
fullDescription: true,
|
||||
visual: {select: {pid: true, description: true}},
|
||||
visual: { select: { pid: true, description: true } },
|
||||
date: true,
|
||||
pid: true,
|
||||
id: false,
|
||||
@@ -51,7 +52,7 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
date: true,
|
||||
pid: true,
|
||||
id: false,
|
||||
visual: {select: {pid: true, description: true}}
|
||||
visual: { select: { pid: true, description: true } }
|
||||
},
|
||||
});
|
||||
|
||||
@@ -73,7 +74,6 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
message: `Internal Server error occured. Try again later`,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (e instanceof Prisma.PrismaClientUnknownRequestError) {
|
||||
return res.status(500).json({
|
||||
@@ -85,7 +85,6 @@ export const getEvent = async (req: Request, res: Response) => {
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
throw e;
|
||||
@@ -138,6 +137,89 @@ 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());
|
||||
}
|
||||
|
||||
const { pid } = req.params;
|
||||
|
||||
const result = UpdateBody.safeParse(req.body);
|
||||
|
||||
if (result.success === false) {
|
||||
return res.status(400).json(generateInvalidBodyError({
|
||||
name: DataType.STRING,
|
||||
date: DataType.DATETIME,
|
||||
briefDescription: DataType.STRING,
|
||||
["fullDescription?"]: DataType.STRING,
|
||||
}));
|
||||
}
|
||||
|
||||
const body = result.data;
|
||||
|
||||
try {
|
||||
const event = await prisma.event.update({
|
||||
where: { pid },
|
||||
data: {
|
||||
name: body.name,
|
||||
date: body.date,
|
||||
briefDescription: body.briefDescription,
|
||||
fullDescription: body.fullDescription
|
||||
},
|
||||
select: {
|
||||
pid: true,
|
||||
name: true,
|
||||
date: true,
|
||||
briefDescription: true,
|
||||
fullDescription: true
|
||||
}
|
||||
})
|
||||
|
||||
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,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface DeleteEventQueryParams {
|
||||
pid: string;
|
||||
}
|
||||
@@ -174,14 +256,14 @@ interface visualBody {
|
||||
}
|
||||
|
||||
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED"){
|
||||
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}));
|
||||
res.status(400).json(generateInvalidBodyError({ mediaPid: DataType.STRING }));
|
||||
}
|
||||
|
||||
const event = await prisma.event.update({
|
||||
@@ -197,12 +279,12 @@ export const addVisual = async (req: Request<visualParams, {}, visualBody>, res:
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
payload: { }
|
||||
payload: {}
|
||||
})
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED"){
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ const roleSchema = {
|
||||
name: true,
|
||||
schema: true,
|
||||
discipline: { select: { pid: true, name: true } },
|
||||
visual: { select: { pid: true, location: true } },
|
||||
visual: { select: { pid: true } },
|
||||
} as const;
|
||||
|
||||
export const _getAllRoleSchemas = async (res: Response, disciplinePid: string | undefined) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Express from "express";
|
||||
import { string } from "zod";
|
||||
import { addEvent, addVisual, deleteEvent, deleteVisual, getAllEvents, getEvent } from "../Controllers/event.controller";
|
||||
import { addEvent, addVisual, deleteEvent, deleteVisual, getAllEvents, getEvent, updateEvent } 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.patch<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, updateEvent);
|
||||
|
||||
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, deleteEvent);
|
||||
|
||||
router.post<"/:eventPid/media", { eventPid: string }>("/:eventPid/media", requireAuthentication, addVisual);
|
||||
|
||||
Reference in New Issue
Block a user