mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Added updateDiscipline
This commit is contained in:
@@ -4,6 +4,7 @@ import { Request, Response } from "express";
|
|||||||
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";
|
||||||
|
import { number, z } from "zod";
|
||||||
import {
|
import {
|
||||||
createInsufficientPermissionsError,
|
createInsufficientPermissionsError,
|
||||||
DataType,
|
DataType,
|
||||||
@@ -15,6 +16,17 @@ import {
|
|||||||
|
|
||||||
require("express-async-errors");
|
require("express-async-errors");
|
||||||
|
|
||||||
|
const DisciplineBody = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
maxTeamSize: z.number(),
|
||||||
|
minTeamSize: z.number(),
|
||||||
|
briefDescription: z.string(),
|
||||||
|
fullDescription: z.string(),
|
||||||
|
eventPid: z.string(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const UpdateDisciplineBody = DisciplineBody.partial();
|
||||||
|
|
||||||
const basicDiscipline = {
|
const basicDiscipline = {
|
||||||
pid: true,
|
pid: true,
|
||||||
name: true,
|
name: true,
|
||||||
@@ -115,12 +127,90 @@ 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;
|
||||||
maxTeamSize?: number;
|
maxTeamSize?: number;
|
||||||
briefDescription: string;
|
briefDescription?: string;
|
||||||
fullDescription: string;
|
fullDescription?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// require: auth(ELEVATED)
|
// require: auth(ELEVATED)
|
||||||
|
|||||||
Reference in New Issue
Block a user