mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Add function to create disciplines
+ Fix async errors + Extract name error
This commit is contained in:
@@ -64,6 +64,13 @@ export const genericError = {
|
||||
},
|
||||
};
|
||||
|
||||
export const NAME_ERROR = {
|
||||
type: "error",
|
||||
payload: {
|
||||
message: "The name has to be at least 1 character long",
|
||||
},
|
||||
};
|
||||
|
||||
export function validateName(name: string) {
|
||||
return name.length > 0;
|
||||
}
|
||||
@@ -106,12 +113,7 @@ export async function handleCreateByName(
|
||||
}
|
||||
|
||||
if (!validateName(name)) {
|
||||
return res.status(400).json({
|
||||
type: "error",
|
||||
payload: {
|
||||
message: "The name has to be at least 1 character long",
|
||||
},
|
||||
});
|
||||
return res.status(400).json(NAME_ERROR);
|
||||
}
|
||||
|
||||
// Check if link object exsits
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
import { Prisma, Organisation, Admin, AdminLevel, Team } from "@prisma/client";
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||
import { Request, Response } from "express";
|
||||
import prisma from "../lib/prisma";
|
||||
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||
import {
|
||||
createInsufficientPermissionsError,
|
||||
DataType,
|
||||
generateError,
|
||||
generateInvalidBodyError,
|
||||
NAME_ERROR,
|
||||
validateName,
|
||||
} from "./common";
|
||||
|
||||
require("express-async-errors");
|
||||
|
||||
const basicDiscipline = {
|
||||
pid: true,
|
||||
@@ -74,7 +86,7 @@ export const getDiscipline = async (req: Request<GetDisciplineQueryParams>, res:
|
||||
});
|
||||
|
||||
if (!discipline) {
|
||||
throw new ForwardableError(404, `The discipline with ID ${pid} could not be found`);
|
||||
throw new NotFoundError("discipline", pid);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
@@ -100,3 +112,53 @@ export const getDiscipline = async (req: Request<GetDisciplineQueryParams>, res:
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
interface CreateDisciplineBody {
|
||||
name?: string;
|
||||
minTeamSize?: number;
|
||||
maxTeamSize?: number;
|
||||
}
|
||||
|
||||
// require: auth(ELEVATED)
|
||||
// at: POST /event/:eventPid/discipliens
|
||||
export const createDiscipline = async (req: Request<{ eventPid: string }, {}, CreateDisciplineBody>, res: Response) => {
|
||||
if (req.auth?.permission_level !== "ELEVATED") {
|
||||
return res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { name, minTeamSize, maxTeamSize } = req.body;
|
||||
|
||||
if (typeof name !== "string" || typeof minTeamSize !== "number" || typeof maxTeamSize !== "number") {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
name: DataType.STRING,
|
||||
minTeamSize: DataType.NUMBER,
|
||||
maxTeamSize: DataType.NUMBER,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (!validateName(name)) {
|
||||
return res.status(400).json(NAME_ERROR);
|
||||
}
|
||||
|
||||
try {
|
||||
const discipline = await prisma.discipline.create({
|
||||
data: { name, minTeamSize, maxTeamSize, event: { connect: { pid: req.params.eventPid } } },
|
||||
select: {
|
||||
pid: true,
|
||||
name: true,
|
||||
minTeamSize: true,
|
||||
maxTeamSize: true,
|
||||
event: { select: { pid: true, name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(201).json({ type: "success", payload: { discipline } });
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
return res.status(404).json(generateError(`Could not link to event with ID '${req.params.eventPid}'`));
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export default class ForwardableError extends Error {
|
||||
// If in different context
|
||||
private readonly __id = "CUSTOM_ERROR";
|
||||
protected readonly __oid?: string;
|
||||
|
||||
public readonly status: number;
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import ForwardableError from "./ForwardableError";
|
||||
|
||||
export default class NotFoundError extends ForwardableError {
|
||||
protected __oid = "NOT_FOUND_ERROR";
|
||||
|
||||
constructor(resource?: string, pid?: string) {
|
||||
super(404, `The requested ${resource ?? "resource"}${pid ? ` with PID '${pid}'` : ""} could not be found!`);
|
||||
}
|
||||
|
||||
static isNotFoundError(err: any): err is NotFoundError {
|
||||
return err.__oid === "NOT_FOUND_ERROR";
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
import express from "express";
|
||||
import { getAllDisciplines, getDiscipline } from "../Controllers/discipline.controller";
|
||||
import eventRouter from "./event.routes";
|
||||
import { createDiscipline, getAllDisciplines, getDiscipline } from "../Controllers/discipline.controller";
|
||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", getAllDisciplines); // TODO: Optional auth
|
||||
router.get("/:pid", getDiscipline);
|
||||
|
||||
eventRouter.post("/:eventPid/disciplines", requireAuthentication, createDiscipline);
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user