mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
patching complications after merging and review suggestions
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import { Prisma, Organisation, Admin, AdminLevel, Team } from "@prisma/client";
|
import { Prisma, Organisation, Admin, AdminLevel, Team } from "@prisma/client";
|
||||||
import { PrismaClientKnownRequestError, PrismaClientUnknownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError, PrismaClientUnknownRequestError } 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 ForwardableError from "../Middleware/error/ForwardableError";
|
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
@@ -17,11 +16,12 @@ import {
|
|||||||
|
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
|
|
||||||
|
|
||||||
const InitialDisciplineBody = z.object({
|
const InitialDisciplineBody = z.object({
|
||||||
name: z.string().min(1),
|
name: z.string().min(1),
|
||||||
minTeamSize: z.number(),
|
minTeamSize: z.number(),
|
||||||
maxTeamSize: z.number(),
|
maxTeamSize: z.number(),
|
||||||
|
briefDescription: z.string(),
|
||||||
|
fullDescription: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const disciplineRefiner = [
|
const disciplineRefiner = [
|
||||||
@@ -29,7 +29,9 @@ const disciplineRefiner = [
|
|||||||
{ message: "The minTeamSize must be smaller or equal to the maxTeamSize" },
|
{ message: "The minTeamSize must be smaller or equal to the maxTeamSize" },
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const DisciplineBody = InitialDisciplineBody.refine(...disciplineRefiner);
|
const DisciplineBody = InitialDisciplineBody.partial({ briefDescription: true, fullDescription: true }).refine(
|
||||||
|
...disciplineRefiner
|
||||||
|
);
|
||||||
const updateDisciplineBody = InitialDisciplineBody.partial().refine(...disciplineRefiner);
|
const updateDisciplineBody = InitialDisciplineBody.partial().refine(...disciplineRefiner);
|
||||||
|
|
||||||
const basicDiscipline = {
|
const basicDiscipline = {
|
||||||
@@ -132,84 +134,6 @@ export const getDiscipline = async (req: Request<GetDisciplineQueryParams>, res:
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateDiscipline = async (req: Request<{ pid: string }>, res: Response) => {
|
|
||||||
if (req.auth?.permission_level !== "ELEVATED") {
|
|
||||||
res.status(403).json(createInsufficientPermissionsError());
|
|
||||||
}
|
|
||||||
|
|
||||||
const { pid } = req.params;
|
|
||||||
|
|
||||||
const result = UpdateDisciplineBody.safeParse(req.body);
|
|
||||||
|
|
||||||
if (result.success === false) {
|
|
||||||
return res.status(400).json(
|
|
||||||
generateInvalidBodyError({
|
|
||||||
name: DataType.STRING,
|
|
||||||
minTeamSize: DataType.NUMBER,
|
|
||||||
maxTeamSize: DataType.NUMBER,
|
|
||||||
briefDescription: DataType.STRING,
|
|
||||||
["fullDescription?"]: DataType.STRING,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = result.data;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const discipline = await prisma.discipline.update({
|
|
||||||
where: { pid },
|
|
||||||
data: {
|
|
||||||
name: body.name,
|
|
||||||
minTeamSize: body.minTeamSize,
|
|
||||||
maxTeamSize: body.maxTeamSize,
|
|
||||||
briefDescription: body.briefDescription,
|
|
||||||
fullDescription: body.fullDescription,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
minTeamSize: true,
|
|
||||||
maxTeamSize: true,
|
|
||||||
briefDescription: true,
|
|
||||||
fullDescription: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!discipline) {
|
|
||||||
throw new NotFoundError("discipline", pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
type: "success",
|
|
||||||
payload: {
|
|
||||||
discipline,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} 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 CreateDisciplineBody {
|
interface CreateDisciplineBody {
|
||||||
name?: string;
|
name?: string;
|
||||||
minTeamSize?: number;
|
minTeamSize?: number;
|
||||||
@@ -272,6 +196,8 @@ export const updateDiscipline = async (req: Request<{ pid: string }>, res: Respo
|
|||||||
name: DataType.STRING,
|
name: DataType.STRING,
|
||||||
minTeamSize: DataType.NUMBER,
|
minTeamSize: DataType.NUMBER,
|
||||||
maxTeamSize: DataType.NUMBER,
|
maxTeamSize: DataType.NUMBER,
|
||||||
|
briefDescription: DataType.STRING,
|
||||||
|
["fullDescription?"]: DataType.STRING,
|
||||||
},
|
},
|
||||||
result.error
|
result.error
|
||||||
)
|
)
|
||||||
@@ -288,6 +214,8 @@ export const updateDiscipline = async (req: Request<{ pid: string }>, res: Respo
|
|||||||
name: body.name,
|
name: body.name,
|
||||||
minTeamSize: body.minTeamSize,
|
minTeamSize: body.minTeamSize,
|
||||||
maxTeamSize: body.maxTeamSize,
|
maxTeamSize: body.maxTeamSize,
|
||||||
|
briefDescription: body.briefDescription,
|
||||||
|
fullDescription: body.fullDescription,
|
||||||
},
|
},
|
||||||
select: basicDiscipline,
|
select: basicDiscipline,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,9 +8,13 @@ import { createInsufficientPermissionsError, DataType, generateError, generateIn
|
|||||||
|
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
|
|
||||||
|
export const dateSchema = z.preprocess((arg) => {
|
||||||
|
if (typeof arg == "string" || arg instanceof Date) return new Date(arg);
|
||||||
|
}, z.date());
|
||||||
|
|
||||||
const EventBody = z.object({
|
const EventBody = z.object({
|
||||||
name: z.string(),
|
name: z.string().min(1),
|
||||||
date: z.string(),
|
date: dateSchema,
|
||||||
briefDescription: z.string(),
|
briefDescription: z.string(),
|
||||||
fullDescription: z.string(),
|
fullDescription: z.string(),
|
||||||
});
|
});
|
||||||
@@ -21,27 +25,32 @@ const CreateEventBody = EventBody.partial({
|
|||||||
fullDescription: true,
|
fullDescription: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getAllEvents = async (req: Request, res: Response) => {
|
const basicEvent = {
|
||||||
const events = await prisma.event.findMany({
|
pid: true,
|
||||||
|
name: true,
|
||||||
|
date: true,
|
||||||
|
briefDescription: true,
|
||||||
|
fullDescription: true,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
const detailedEvent = {
|
||||||
|
pid: true,
|
||||||
|
name: true,
|
||||||
|
date: true,
|
||||||
|
briefDescription: true,
|
||||||
|
fullDescription: true,
|
||||||
|
visual: { select: { pid: true, description: true } },
|
||||||
|
disciplines: {
|
||||||
select: {
|
select: {
|
||||||
pid: true,
|
pid: true,
|
||||||
name: true,
|
name: true,
|
||||||
date: true,
|
|
||||||
briefDescription: true,
|
|
||||||
fullDescription: true,
|
|
||||||
visual: { select: { pid: true, description: true } },
|
|
||||||
disciplines: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
}},
|
|
||||||
organisations: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export const getAllEvents = async (req: Request, res: Response) => {
|
||||||
|
const events = await prisma.event.findMany({
|
||||||
|
select: detailedEvent,
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
@@ -68,27 +77,7 @@ export const getEvent = async (req: Request, res: Response) => {
|
|||||||
where: {
|
where: {
|
||||||
pid: eventId,
|
pid: eventId,
|
||||||
},
|
},
|
||||||
select: {
|
select: detailedEvent,
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
date: true,
|
|
||||||
briefDescription: true,
|
|
||||||
fullDescription: true,
|
|
||||||
visual: { select: { pid: true, description: true } },
|
|
||||||
disciplines: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
briefDescription: true,
|
|
||||||
fullDescription: true,
|
|
||||||
}},
|
|
||||||
organisations: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!event) {
|
if (!event) {
|
||||||
@@ -134,19 +123,20 @@ export const addEvent = async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
const result = CreateEventBody.safeParse(req.body);
|
const result = CreateEventBody.safeParse(req.body);
|
||||||
|
|
||||||
if(result.success === false){
|
if (result.success === false) {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError(
|
||||||
name: DataType.STRING,
|
{
|
||||||
date: DataType.DATETIME,
|
name: DataType.STRING,
|
||||||
briefDescription: DataType.STRING,
|
date: DataType.DATETIME,
|
||||||
["fullDescription?"]: DataType.STRING,
|
briefDescription: DataType.STRING,
|
||||||
})
|
["fullDescription?"]: DataType.STRING,
|
||||||
|
},
|
||||||
|
result.error
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Check if date is valid
|
|
||||||
|
|
||||||
const event = await prisma.event.create({
|
const event = await prisma.event.create({
|
||||||
data: {
|
data: {
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
@@ -154,13 +144,7 @@ export const addEvent = async (req: Request, res: Response) => {
|
|||||||
briefDescription: req.body.briefDescription,
|
briefDescription: req.body.briefDescription,
|
||||||
fullDescription: req.body.fullDescription,
|
fullDescription: req.body.fullDescription,
|
||||||
},
|
},
|
||||||
select: {
|
select: basicEvent,
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
date: true,
|
|
||||||
briefDescription: true,
|
|
||||||
fullDescription: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
@@ -214,10 +198,6 @@ export const updateEvent = async (req: Request<{ pid: string }>, res: Response)
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!event) {
|
|
||||||
throw new NotFoundError("event", pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
type: "success",
|
type: "success",
|
||||||
payload: {
|
payload: {
|
||||||
@@ -225,24 +205,8 @@ export const updateEvent = async (req: Request<{ pid: string }>, res: Response)
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
return res.status(500).json({
|
throw new NotFoundError("discipline", pid);
|
||||||
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;
|
throw e;
|
||||||
@@ -267,7 +231,7 @@ export const deleteEvent = async (req: Request<DeleteEventQueryParams>, res: Res
|
|||||||
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") {
|
||||||
return res.status(404).json(generateError(`The event with the ID ${pid} could not be found`));
|
throw new NotFoundError("discipline", pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { NextFunction, Request, response, Response } from "express";
|
import { Request, 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";
|
||||||
@@ -8,12 +8,8 @@ import prisma from "../lib/prisma";
|
|||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { generateInvalidBodyError, DataType } from "./common";
|
import { generateInvalidBodyError, DataType } from "./common";
|
||||||
import { type } from "os";
|
|
||||||
import { unlink } from "fs/promises";
|
import { unlink } from "fs/promises";
|
||||||
import ForwardableError from "../Middleware/error/ForwardableError";
|
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||||
import SchemaError from "../Middleware/error/SchemaError";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { updateEvent } from "./event.controller";
|
|
||||||
|
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
|
|
||||||
@@ -101,7 +97,6 @@ export const uploadImage = async (req: Request, res: Response) => {
|
|||||||
const fileName = file.md5 + (fileIsSvg ? ".svg" : "." + fileType?.ext);
|
const fileName = file.md5 + (fileIsSvg ? ".svg" : "." + fileType?.ext);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//generate record
|
|
||||||
const media = await prisma.media.create({
|
const media = await prisma.media.create({
|
||||||
data: {
|
data: {
|
||||||
pid: fileName,
|
pid: fileName,
|
||||||
@@ -192,12 +187,7 @@ export const deleteMedia = async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO: maybe create a function that adds the tableToUpdate based on path
|
export const linkMedia = async (req: Request<{ pid: string }, {}, { mediaPid: string }>, res: Response) => {
|
||||||
// and call it before calling (un)linkMedia
|
|
||||||
|
|
||||||
export const linkMedia = async (
|
|
||||||
req: Request<{ pid: string }, {}, { mediaPid: 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());
|
||||||
}
|
}
|
||||||
@@ -206,7 +196,7 @@ export const linkMedia = async (
|
|||||||
const { mediaPid } = req.body;
|
const { mediaPid } = req.body;
|
||||||
const tableToUpdate = req.originalUrl.split("/");
|
const tableToUpdate = req.originalUrl.split("/");
|
||||||
|
|
||||||
if(typeof mediaPid !== "string") {
|
if (typeof mediaPid !== "string") {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError({
|
||||||
mediaPid: DataType.UUID,
|
mediaPid: DataType.UUID,
|
||||||
@@ -231,9 +221,7 @@ export const linkMedia = async (
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const unlinkMedia = async (
|
export const unlinkMedia = async (req: Request<{ pid: string; mediaPid: string }>, res: Response) => {
|
||||||
req: Request<{ pid: string, mediaPid: 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());
|
||||||
}
|
}
|
||||||
@@ -249,29 +237,20 @@ export const unlinkMedia = async (
|
|||||||
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") {
|
||||||
console.log("not found error");
|
|
||||||
throw new NotFoundError(tableToUpdate[2], pid);
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function getPrismaUpdateFKT( tableToUpdate: string ): Function {
|
function getPrismaUpdateFKT(tableToUpdate: string): Function {
|
||||||
switch(tableToUpdate) {
|
switch (tableToUpdate) {
|
||||||
case "events": return prisma.event.update;
|
case "events":
|
||||||
case "disciplines": return prisma.discipline.update;
|
return prisma.event.update;
|
||||||
default: return prisma.roleSchema.update;
|
case "disciplines":
|
||||||
|
return prisma.discipline.update;
|
||||||
|
default:
|
||||||
|
return prisma.roleSchema.update;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
} from "./common";
|
} from "./common";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
|
|
||||||
function validateOranisationName(name: string) {
|
function validateOranisationName(name: string) {
|
||||||
return name.length > 0;
|
return name.length > 0;
|
||||||
@@ -187,16 +188,12 @@ export const updateOrganisation = async (
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
if (e.code === "P2025") {
|
throw new NotFoundError("discipline", pid);
|
||||||
return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`));
|
|
||||||
}
|
|
||||||
} else if (e instanceof PrismaClientUnknownRequestError) {
|
|
||||||
return res.status(400).send(generateError("Unkonwn error occured. This could be due to malformed IDs"));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(500).json(genericError);
|
throw e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
interface DeleteOrganisationQueryParams {
|
interface DeleteOrganisationQueryParams {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
import { Job, Prisma } from "@prisma/client";
|
import { Job, Prisma } from "@prisma/client";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
import { requireResponsibleForGroup } from "../Middleware/auth/auth";
|
import { requireConfiguredAuthentication, requireResponsibleForGroup } from "../Middleware/auth/auth";
|
||||||
|
|
||||||
//TODO: add TeamleaderAuthentification
|
//TODO: add TeamleaderAuthentification
|
||||||
|
|
||||||
@@ -44,8 +44,8 @@ const returnedParticipant = {
|
|||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
// REVIEW: Location of this endpoints (/groups, /teams, /participants, ...?)
|
// at: POST api/teams/:teamPid/participant/
|
||||||
export const createParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
export const createParticipant = async (req: Request<{ teamPid: string }>, res: Response) => {
|
||||||
const result = ParticipantBody.safeParse(req.body);
|
const result = ParticipantBody.safeParse(req.body);
|
||||||
|
|
||||||
if (result.success === false) {
|
if (result.success === false) {
|
||||||
@@ -61,17 +61,7 @@ export const createParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
const body = result.data;
|
const body = result.data;
|
||||||
const { pid } = req.params;
|
const { teamPid } = req.params;
|
||||||
|
|
||||||
/*
|
|
||||||
if (!req.auth?.isAuthenticated || req.teamleader?.team != pid) {
|
|
||||||
return res.status(500).json(AUTH_ERROR);
|
|
||||||
}
|
|
||||||
if (req.teamleader?.team != pid) {
|
|
||||||
return res.status(500).json(AUTH_ERROR);
|
|
||||||
}
|
|
||||||
requireResponsibleForGroup(req.auth, req.body.groupId);
|
|
||||||
*/
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const participant = await prisma.participant.create({
|
const participant = await prisma.participant.create({
|
||||||
@@ -80,7 +70,7 @@ export const createParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
lastName: body.lastName,
|
lastName: body.lastName,
|
||||||
relevance: "MEMBER",
|
relevance: "MEMBER",
|
||||||
group: { connect: { pid: body.groupId } },
|
group: { connect: { pid: body.groupId } },
|
||||||
team: { connect: { pid } },
|
team: { connect: { pid: teamPid } },
|
||||||
},
|
},
|
||||||
select: returnedParticipant,
|
select: returnedParticipant,
|
||||||
});
|
});
|
||||||
@@ -93,16 +83,15 @@ export const createParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
.json(generateError(`Could not link to team with ID '${pid}, or group with ID ${body.groupId}'`));
|
.json(generateError(`Could not link to team with ID '${teamPid}, or group with ID ${body.groupId}'`));
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// at: PATCH api/participants/:pid/
|
||||||
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
export const updateParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||||
//insert TeamleaderAuth
|
const result = ParticipantBody.partial().safeParse(req.body);
|
||||||
|
|
||||||
const result = ParticipantBody.partial().safeParse(req.body); // Should be partial, right?
|
|
||||||
|
|
||||||
if (result.success === false) {
|
if (result.success === false) {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
@@ -144,9 +133,8 @@ export const updateParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// at: DELETE api/participants/:pid/
|
||||||
export const deleteParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
export const deleteParticipant = async (req: Request<{ pid: string }>, res: Response) => {
|
||||||
//insert TeamleaderAuth
|
|
||||||
|
|
||||||
const { pid } = req.params;
|
const { pid } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -155,7 +143,7 @@ export const deleteParticipant = async (req: Request<{ pid: string }>, res: Resp
|
|||||||
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") {
|
||||||
return res.status(404).json(generateError(`The participant with the ID ${pid} could not be found`));
|
throw new NotFoundError("participant", pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@@ -8,6 +8,30 @@ import { createInsufficientPermissionsError, DataType, generateInvalidBodyError
|
|||||||
|
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
|
|
||||||
|
const detailedRole = {
|
||||||
|
pid: true,
|
||||||
|
score: true,
|
||||||
|
schema: {
|
||||||
|
select: {
|
||||||
|
pid: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
participant: {
|
||||||
|
select: {
|
||||||
|
pid: true,
|
||||||
|
firstName: true,
|
||||||
|
lastName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
team: {
|
||||||
|
select: {
|
||||||
|
pid: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param teamPid: Pid of the team to add the roles to
|
* @param teamPid: Pid of the team to add the roles to
|
||||||
@@ -94,66 +118,3 @@ export async function assignParticipantToRole(req: Request<{ pid: string }>, res
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateRoleScore = async (req: Request<{ pid: string }, {}, { score: string }>, res: Response) => {
|
|
||||||
if (req.auth?.permission_level != "ELEVATED") {
|
|
||||||
res.status(403).json(createInsufficientPermissionsError());
|
|
||||||
}
|
|
||||||
|
|
||||||
const { score } = req.body;
|
|
||||||
|
|
||||||
if (typeof score !== "string") {
|
|
||||||
res.status(400).json(generateInvalidBodyError({ score: DataType.STRING }));
|
|
||||||
}
|
|
||||||
|
|
||||||
const { pid } = req.params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const role = await prisma.role.update({
|
|
||||||
where: { pid },
|
|
||||||
data: { score },
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
score: true,
|
|
||||||
schema: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
participant: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
firstName: true,
|
|
||||||
lastName: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
team: {
|
|
||||||
select: {
|
|
||||||
pid: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
type: "success",
|
|
||||||
payload: {
|
|
||||||
role,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
|
||||||
throw new NotFoundError("role", pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function deleteRolesFromTeam(teamPid: string) {
|
|
||||||
await prisma.role.deleteMany({
|
|
||||||
where: { team: { pid: teamPid } },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ const RoleSchemaBody = z.object({
|
|||||||
schema: z.string(),
|
schema: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const UpdateRoleSchema = RoleSchemaBody.partial();
|
const UpdateBody = RoleSchemaBody.partial();
|
||||||
|
|
||||||
const roleSchema = {
|
const roleSchema = {
|
||||||
pid: true,
|
pid: true,
|
||||||
@@ -133,3 +133,51 @@ export const createRoleSchema = async (
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const UpdateRoleSchema = 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,
|
||||||
|
schema: DataType.STRING,
|
||||||
|
},
|
||||||
|
result.error
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = result.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = await prisma.roleSchema.update({
|
||||||
|
where: { pid },
|
||||||
|
data: {
|
||||||
|
name: body.name,
|
||||||
|
schema: body.schema,
|
||||||
|
},
|
||||||
|
select: roleSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
type: "success",
|
||||||
|
payload: {
|
||||||
|
schema,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
|
throw new NotFoundError("discipline", pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import prisma from "../lib/prisma";
|
|||||||
import { createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
import { createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
||||||
import { requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth";
|
import { requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
|
|
||||||
const TeamBody = z.object({
|
const TeamBody = z.object({
|
||||||
teamName: z.string().min(1),
|
teamName: z.string().min(1),
|
||||||
@@ -69,18 +71,26 @@ export const updateTeam = async (req: Request, res: Response) => {
|
|||||||
return res.status(401).json(createInsufficientPermissionsError("STANDARD"));
|
return res.status(401).json(createInsufficientPermissionsError("STANDARD"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const team = prisma.team.update({
|
try {
|
||||||
where: {
|
const team = prisma.team.update({
|
||||||
pid: body.pid,
|
where: {
|
||||||
},
|
pid: body.pid,
|
||||||
data: {
|
},
|
||||||
name: body.teamName,
|
data: {
|
||||||
discipline: { connect: { pid: body.disciplineId } },
|
name: body.teamName,
|
||||||
leaderEmail: body.leaderEmail,
|
discipline: { connect: { pid: body.disciplineId } },
|
||||||
},
|
leaderEmail: body.leaderEmail,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
res.status(204).json(team);
|
res.status(204).json(team);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
|
throw new NotFoundError("discipline", body.pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteTeam = async (req: Request, res: Response) => {
|
export const deleteTeam = async (req: Request, res: Response) => {
|
||||||
|
|||||||
@@ -1,29 +1,28 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import teamRouter from "./team.routes";
|
import teamRouter from "./team.routes";
|
||||||
import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller";
|
import { createParticipant, deleteParticipant, updateParticipant } from "../Controllers/participant.controller";
|
||||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
import { requireAuthentication, requireConfiguredAuthentication } from "../Middleware/auth/auth";
|
||||||
import { requireTeamleaderAuthentication } from "../Middleware/auth/teamleaderAuth";
|
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
teamRouter.post<"/:pid/participant/", { pid: string }>(
|
teamRouter.post<"/:teamPid/participant/", { teamPid: string }>(
|
||||||
"/:pid/participant/",
|
"/:teamPid/participant/",
|
||||||
requireAuthentication,
|
requireAuthentication,
|
||||||
requireTeamleaderAuthentication,
|
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
|
||||||
createParticipant
|
createParticipant
|
||||||
);
|
);
|
||||||
|
|
||||||
teamRouter.patch<"/:pid/participant/", { pid: string }>(
|
router.patch<"/:pid/", { pid: string }>(
|
||||||
"/:pid/participant/",
|
"/:pid/",
|
||||||
requireAuthentication,
|
requireAuthentication,
|
||||||
requireTeamleaderAuthentication,
|
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
|
||||||
updateParticipant
|
updateParticipant
|
||||||
);
|
);
|
||||||
|
|
||||||
teamRouter.delete<"/:pid/participant/", { pid: string }>(
|
router.delete<"/:pid/", { pid: string }>(
|
||||||
"/:pid/participant/",
|
"/:pid/",
|
||||||
requireAuthentication,
|
requireAuthentication,
|
||||||
requireTeamleaderAuthentication,
|
requireConfiguredAuthentication({ optional: true, type: { admin: true, teamleader: true } }),
|
||||||
deleteParticipant
|
deleteParticipant
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -7,3 +7,5 @@ const router = Express.Router();
|
|||||||
router.get<"team/:teamPid/", { teamPid: string }>("team/:teamPid/", getRolesForTeam);
|
router.get<"team/:teamPid/", { teamPid: string }>("team/:teamPid/", getRolesForTeam);
|
||||||
|
|
||||||
router.put<"/:pid/participant", { pid: string }>("/:pid/participant", assignParticipantToRole);
|
router.put<"/:pid/participant", { pid: string }>("/:pid/participant", assignParticipantToRole);
|
||||||
|
|
||||||
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user