mirror of
https://github.com/detleph/server.git
synced 2026-09-04 00:26:03 +02:00
added updateRoleSchema
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } 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 { DurationSchemaT, parseSchema, PointSchemaT } from "../lib/result_schema";
|
import { DurationSchemaT, parseSchema, PointSchemaT } from "../lib/result_schema";
|
||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
@@ -13,7 +14,15 @@ import {
|
|||||||
validateName,
|
validateName,
|
||||||
} from "./common";
|
} from "./common";
|
||||||
|
|
||||||
|
const RoleSchemaBody = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
schema: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const UpdateRoleSchema = RoleSchemaBody.partial();
|
||||||
|
|
||||||
const roleSchema = {
|
const roleSchema = {
|
||||||
|
pid: true,
|
||||||
name: true,
|
name: true,
|
||||||
schema: true,
|
schema: true,
|
||||||
discipline: { select: { pid: true, name: 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 {
|
interface visualParams {
|
||||||
schemaPid: string;
|
schemaPid: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user