From 4fb9f2e49650f0a50d22a9398d179879a8e64351 Mon Sep 17 00:00:00 2001 From: Laurin <60652077+Flexla54@users.noreply.github.com> Date: Sun, 29 May 2022 02:44:30 +0200 Subject: [PATCH] added updateRoleSchema --- src/Controllers/role_schema.controller.ts | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/Controllers/role_schema.controller.ts b/src/Controllers/role_schema.controller.ts index 36265de..62a0dfa 100644 --- a/src/Controllers/role_schema.controller.ts +++ b/src/Controllers/role_schema.controller.ts @@ -1,6 +1,7 @@ import { Prisma } from "@prisma/client"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; import { Request, Response } from "express"; +import { z } from "zod"; import prisma from "../lib/prisma"; import { DurationSchemaT, parseSchema, PointSchemaT } from "../lib/result_schema"; import NotFoundError from "../Middleware/error/NotFoundError"; @@ -13,7 +14,15 @@ import { validateName, } from "./common"; +const RoleSchemaBody = z.object({ + name: z.string(), + schema: z.string(), +}); + +const UpdateRoleSchema = RoleSchemaBody.partial(); + const roleSchema = { + pid: true, name: true, schema: true, discipline: { select: { pid: true, name: true } }, @@ -124,6 +133,70 @@ export const createRoleSchema = async ( } }; +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 = UpdateRoleSchema.safeParse(req.body); + + if (result.success === false) { + return res.status(400).json( + generateInvalidBodyError({ + name: DataType.STRING, + schema: DataType.RESULT_SCHEMA, + }) + ); + } + + const body = result.data; + + try { + const schema = await prisma.roleSchema.update({ + where: { pid }, + data: { + name: body.name, + schema: body.schema, + }, + select: roleSchema, + }); + + if (!schema) { + throw new NotFoundError("schema", pid); + } + + res.status(200).json({ + type: "success", + payload: schema, + }); + } 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: { + name: DataType.STRING, + schema: DataType.RESULT_SCHEMA, + }, + }, + }); + } + + throw e; + } +}; + interface visualParams { schemaPid: string; }