-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_schema.controller.ts
135 lines (112 loc) · 3.63 KB
/
role_schema.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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";
import SchemaError from "../Middleware/error/SchemaError";
import {
createInsufficientPermissionsError,
DataType,
generateError,
generateInvalidBodyError,
NAME_ERROR,
validateName,
} from "./common";
const RoleSchemaBody = z.object({
name: z.string().min(1),
schema: z.string(),
});
const UpdateRoleSchema = RoleSchemaBody.partial();
const roleSchema = {
pid: true,
name: true,
schema: true,
discipline: { select: { pid: true, name: true } },
visual: { select: { pid: true } },
} as const;
export const _getAllRoleSchemas = async (res: Response, disciplinePid: string | undefined) => {
const schemas = await prisma.roleSchema.findMany({
where: { discipline: { pid: disciplinePid } },
select: roleSchema,
});
return res.status(200).json({
type: "success",
payload: {
roleSchemas: schemas,
},
});
};
interface GetAllRoleSchemasSearchParams {
disciplinePid?: string;
}
export const getAllRoleSchemas = async (req: Request<{}, {}, {}, GetAllRoleSchemasSearchParams>, res: Response) => {
return _getAllRoleSchemas(res, req.query.disciplinePid);
};
interface GetAllRoleSchemasWithParamQueryParams {
organisationPid: string;
}
export const getAllRoleSchemasWithParam = async (
req: Request<GetAllRoleSchemasWithParamQueryParams>,
res: Response
) => {
return _getAllRoleSchemas(res, req.params.organisationPid);
};
interface GetRoleSchemaQueryParams {
pid: string;
}
export const getRoleSchema = async (req: Request<GetRoleSchemaQueryParams>, res: Response) => {
const { pid } = req.params;
const schema = await prisma.roleSchema.findUnique({ where: { pid }, select: roleSchema });
if (!schema) {
throw new NotFoundError("roleSchema", pid);
}
return res.status(200).json({
type: "success",
payload: {
roleSchema: {
...schema,
discipline: {
...schema.discipline,
_links: [{ rel: "self", type: "GET", href: `/api/disciplines/${schema.discipline.pid}` }],
},
},
},
});
};
interface CreateRoleSchemaBody {
name?: string;
schema?: any;
}
// at: POST /discipline/:disciplinePid/role-schemas
// requires: auth(ELEVATED)
export const createRoleSchema = async (
req: Request<{ disciplinePid: string }, {}, CreateRoleSchemaBody>,
res: Response
) => {
if (req.auth?.permission_level !== "ELEVATED") {
return res.status(403).json(createInsufficientPermissionsError());
}
const { name, schema: resultSchema } = req.body;
if (typeof name !== "string") {
return res.status(400).json(generateInvalidBodyError({ name: DataType.STRING, schema: DataType.RESULT_SCHEMA }));
}
if (!validateName(name)) {
return res.status(400).json(NAME_ERROR);
}
// Validate the result schema (Errors should be handled by the default error handler)
const validatedSchema = parseSchema(resultSchema);
try {
const schema = await prisma.roleSchema.create({
data: { name, schema: validatedSchema, discipline: { connect: { pid: req.params.disciplinePid } } },
select: roleSchema,
});
return res.status(201).json({ type: "success", payload: { roleSchema: schema } });
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
throw new NotFoundError("discipline", req.params.disciplinePid);
}
throw e;
}
};