mirror of
https://github.com/detleph/server.git
synced 2026-09-04 16:46:04 +02:00
media bugfixes
This commit is contained in:
@@ -9,8 +9,6 @@ import { createInsufficientPermissionsError, DataType, generateError, generateIn
|
||||
require("express-async-errors");
|
||||
|
||||
const EventBody = z.object({
|
||||
pid: z.string().max(0),
|
||||
id: z.string().max(0),
|
||||
name: z.string(),
|
||||
date: z.string(),
|
||||
briefDescription: z.string(),
|
||||
|
||||
@@ -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";
|
||||
@@ -17,13 +17,6 @@ import { updateEvent } from "./event.controller";
|
||||
|
||||
require("express-async-errors");
|
||||
|
||||
const linkMediaBody = z.object({
|
||||
tableToUpdate: z.enum(["EVENT", "ROLE_SCHEMA", "DISCIPLINE"]),
|
||||
mediaPid: z.string(),
|
||||
});
|
||||
|
||||
const unlinkMediaBody = linkMediaBody.omit({ mediaPid: true, });
|
||||
|
||||
function createMediaLinks(fileName: string) {
|
||||
return [{ rel: "self", type: "GET", href: `/api/media/${fileName}` }];
|
||||
}
|
||||
@@ -203,27 +196,25 @@ export const deleteMedia = async (req: Request, res: Response) => {
|
||||
// and call it before calling (un)linkMedia
|
||||
|
||||
export const linkMedia = async (
|
||||
req: Request<{ pid: string }, {}, { mediaPid: string, tableToUpdate: string }>,
|
||||
req: Request<{ pid: string }, {}, { mediaPid: string }>,
|
||||
res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const zBody = linkMediaBody.safeParse(req.body);
|
||||
const { pid } = req.params;
|
||||
const { mediaPid } = req.body;
|
||||
const tableToUpdate = req.originalUrl.split("/");
|
||||
|
||||
if(zBody.success === false) {
|
||||
if(typeof mediaPid !== "string") {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
mediaPid: DataType.UUID,
|
||||
tableToUpdate: DataType.STRING,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const { pid } = req.params;
|
||||
const { mediaPid, tableToUpdate } = zBody.data;
|
||||
|
||||
const updatedRec = await getPrismaUpdateFKT(tableToUpdate)({
|
||||
const updatedRec = await getPrismaUpdateFKT(tableToUpdate[2])({
|
||||
where: { pid },
|
||||
data: {
|
||||
visual: { connect: { pid: mediaPid } },
|
||||
@@ -231,7 +222,7 @@ export const linkMedia = async (
|
||||
});
|
||||
|
||||
if (!updatedRec) {
|
||||
throw new NotFoundError(tableToUpdate, pid);
|
||||
throw new NotFoundError(tableToUpdate[2], pid);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
@@ -241,36 +232,36 @@ export const linkMedia = async (
|
||||
};
|
||||
|
||||
export const unlinkMedia = async (
|
||||
req: Request<{ pid: string, mediaPid: string }, {}, { tableToUpdate: string }>,
|
||||
req: Request<{ pid: string, mediaPid: string }>,
|
||||
res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const zBody = linkMediaBody.safeParse(req.body);
|
||||
|
||||
if(zBody.success === false) {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
mediaPid: DataType.UUID,
|
||||
tableToUpdate: DataType.STRING,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const { pid } = req.params;
|
||||
const { mediaPid, tableToUpdate } = zBody.data;
|
||||
const { pid, mediaPid } = req.params;
|
||||
const tableToUpdate = req.originalUrl.split("/");
|
||||
|
||||
try {
|
||||
await getPrismaUpdateFKT(tableToUpdate)({
|
||||
where: pid,
|
||||
data: { visual: { disconnect: { pid: mediaPid } },
|
||||
},
|
||||
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") {
|
||||
throw new NotFoundError(tableToUpdate, pid);
|
||||
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;
|
||||
@@ -279,8 +270,8 @@ export const unlinkMedia = async (
|
||||
|
||||
function getPrismaUpdateFKT( tableToUpdate: string ): Function {
|
||||
switch(tableToUpdate) {
|
||||
case "EVENT": return prisma.event.update;
|
||||
case "DISCIPLINE": return prisma.discipline.update;
|
||||
case "events": return prisma.event.update;
|
||||
case "disciplines": return prisma.discipline.update;
|
||||
default: return prisma.roleSchema.update;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user