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 = {
|
const basicDiscipline = {
|
||||||
pid: true,
|
pid: true,
|
||||||
name: true,
|
name: true,
|
||||||
visual: { select: { pid: true, location: true } },
|
visual: { select: { pid: true } },
|
||||||
maxTeamSize: true,
|
maxTeamSize: true,
|
||||||
minTeamSize: true,
|
minTeamSize: true,
|
||||||
event: { select: { pid: true, name: 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({
|
const discipline = await prisma.discipline.update({
|
||||||
where: { pid: disciplinePid },
|
where: { pid: disciplinePid },
|
||||||
data: {
|
data: {
|
||||||
visual: { connect: { pid: req.body.mediaPid } }
|
visual: {connect: {pid: req.body.mediaPid}}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
|
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
|
||||||
@@ -13,7 +14,7 @@ export const getAllEvents = async (req: Request, res: Response) => {
|
|||||||
name: true,
|
name: true,
|
||||||
briefDescription: true,
|
briefDescription: true,
|
||||||
fullDescription: true,
|
fullDescription: true,
|
||||||
visual: {select: {pid: true, description: true}},
|
visual: { select: { pid: true, description: true } },
|
||||||
date: true,
|
date: true,
|
||||||
pid: true,
|
pid: true,
|
||||||
id: false,
|
id: false,
|
||||||
@@ -51,7 +52,7 @@ export const getEvent = async (req: Request, res: Response) => {
|
|||||||
date: true,
|
date: true,
|
||||||
pid: true,
|
pid: true,
|
||||||
id: false,
|
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`,
|
message: `Internal Server error occured. Try again later`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (e instanceof Prisma.PrismaClientUnknownRequestError) {
|
if (e instanceof Prisma.PrismaClientUnknownRequestError) {
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
@@ -85,7 +85,6 @@ export const getEvent = async (req: Request, res: Response) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw e;
|
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 {
|
interface DeleteEventQueryParams {
|
||||||
pid: string;
|
pid: string;
|
||||||
}
|
}
|
||||||
@@ -174,16 +256,16 @@ interface visualBody {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
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());
|
res.status(403).json(createInsufficientPermissionsError());
|
||||||
}
|
}
|
||||||
|
|
||||||
const { eventPid } = req.params;
|
const { eventPid } = req.params;
|
||||||
|
|
||||||
if (typeof req.body.mediaPid !== "string") {
|
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({
|
const event = await prisma.event.update({
|
||||||
where: { pid: eventPid },
|
where: { pid: eventPid },
|
||||||
data: {
|
data: {
|
||||||
@@ -197,25 +279,25 @@ export const addVisual = async (req: Request<visualParams, {}, visualBody>, res:
|
|||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
payload: { }
|
payload: {}
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
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());
|
res.status(403).json(createInsufficientPermissionsError());
|
||||||
}
|
}
|
||||||
|
|
||||||
const { eventPid, pid } = req.params;
|
const { eventPid, pid } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.event.update({
|
await prisma.event.update({
|
||||||
where: {
|
where: {
|
||||||
pid: eventPid,
|
pid: eventPid,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
visual: { disconnect: { pid } },
|
visual: { disconnect: { pid } },
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return res.status(204).end();
|
return res.status(204).end();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const roleSchema = {
|
|||||||
name: true,
|
name: true,
|
||||||
schema: true,
|
schema: true,
|
||||||
discipline: { select: { pid: true, name: true } },
|
discipline: { select: { pid: true, name: true } },
|
||||||
visual: { select: { pid: true, location: true } },
|
visual: { select: { pid: true } },
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const _getAllRoleSchemas = async (res: Response, disciplinePid: string | undefined) => {
|
export const _getAllRoleSchemas = async (res: Response, disciplinePid: string | undefined) => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import Express from "express";
|
import Express from "express";
|
||||||
import { string } from "zod";
|
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";
|
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||||
const router = Express.Router();
|
const router = Express.Router();
|
||||||
|
|
||||||
@@ -10,6 +10,8 @@ router.get("/:eventId", getEvent);
|
|||||||
|
|
||||||
router.post("/", requireAuthentication, addEvent);
|
router.post("/", requireAuthentication, addEvent);
|
||||||
|
|
||||||
|
router.patch<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, updateEvent);
|
||||||
|
|
||||||
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, deleteEvent);
|
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, deleteEvent);
|
||||||
|
|
||||||
router.post<"/:eventPid/media", { eventPid: string }>("/:eventPid/media", requireAuthentication, addVisual);
|
router.post<"/:eventPid/media", { eventPid: string }>("/:eventPid/media", requireAuthentication, addVisual);
|
||||||
|
|||||||
Reference in New Issue
Block a user