mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Add function for deleting events
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import { DataType, generateInvalidBodyError } from "./common";
|
import { createInsufficientPermissionsError, DataType, generateError, generateInvalidBodyError } from "./common";
|
||||||
|
|
||||||
export const getAllEvents = async (req: Request, res: Response) => {
|
export const getAllEvents = async (req: Request, res: Response) => {
|
||||||
const events = await prisma.event.findMany({
|
const events = await prisma.event.findMany({
|
||||||
@@ -118,3 +119,28 @@ export const addEvent = async (req: Request, res: Response) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface DeleteEventQueryParams {
|
||||||
|
pid: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// requires: auth(ELEVATED)
|
||||||
|
export const deleteEvent = (req: Request<DeleteEventQueryParams>, res: Response) => {
|
||||||
|
if (req.auth?.permission_level !== "ELEVATED") {
|
||||||
|
res.status(403).json(createInsufficientPermissionsError());
|
||||||
|
}
|
||||||
|
|
||||||
|
const { pid } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
prisma.event.delete({ where: { pid } });
|
||||||
|
|
||||||
|
return res.status(204).end();
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
|
return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`));
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user