mirror of
https://github.com/detleph/server.git
synced 2026-09-04 16:46:04 +02:00
Added updateDiscipline
This commit is contained in:
@@ -4,6 +4,7 @@ import { Request, Response } from "express";
|
||||
import prisma from "../lib/prisma";
|
||||
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||
import { number, z } from "zod";
|
||||
import {
|
||||
createInsufficientPermissionsError,
|
||||
DataType,
|
||||
@@ -15,6 +16,17 @@ import {
|
||||
|
||||
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 = {
|
||||
pid: 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 {
|
||||
name?: string;
|
||||
minTeamSize?: number;
|
||||
maxTeamSize?: number;
|
||||
briefDescription: string;
|
||||
fullDescription: string;
|
||||
briefDescription?: string;
|
||||
fullDescription?: string;
|
||||
}
|
||||
|
||||
// require: auth(ELEVATED)
|
||||
|
||||
Reference in New Issue
Block a user