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