Files
permit-api/src/rts/rts.route.ts

129 lines
2.5 KiB
TypeScript

import { FastifyInstance } from "fastify";
import { $rts } from "./rts.schema";
import {
createRtsHandler,
deleteRtsHandler,
getRtsHandler,
listRtsHandler,
newFilesHandler,
updateRtsHandler,
} from "./rts.controller";
import { hideFields } from "../auth";
import { noteRoutes } from "../note/note.route";
import { getUniqueFields } from "../unique";
export async function rtsRoutes(fastify: FastifyInstance) {
fastify.post(
"/",
{
schema: {
body: $rts("rtsCreateInput"),
},
config: { requiredClaims: ["rts:write"] },
preHandler: [fastify.authorize],
},
createRtsHandler
);
fastify.get(
"/:rtsId",
{
schema: {
params: {
type: "object",
properties: {
rtsId: { type: "string" },
},
},
},
config: { requiredClaims: ["rts:read"] },
preHandler: [fastify.authorize],
},
getRtsHandler
);
fastify.get(
"/",
{
schema: {
querystring: $rts("pageQueryParams"),
},
config: { requiredClaims: ["rts:read"] },
preHandler: [fastify.authorize],
},
listRtsHandler
);
fastify.patch(
"/:rtsId",
{
schema: {
params: {
type: "object",
properties: { rtsId: { type: "string" } },
},
body: $rts("rtsUpdateInput"),
},
},
updateRtsHandler
);
fastify.delete(
"/:rtsId",
{
schema: {
params: {
type: "object",
properties: { rtsId: { type: "string" } },
},
},
config: { requiredClaims: ["rts:delete"] },
preHandler: [fastify.authorize],
},
deleteRtsHandler
);
fastify.post(
"/:rtsId/files",
{
schema: {
body: $rts("rtsNewUpload"),
},
config: { requiredClaims: ["rts:write"] },
preHandler: [fastify.authorize],
},
newFilesHandler
);
fastify.get(
"/fields/:field",
{
schema: {
params: {
type: "object",
properties: {
field: { type: "string" },
},
},
},
config: { requiredClaims: ["rts:read"] },
preHandler: [fastify.authorize],
},
async (req, res) => {
const { field } = req.params as { field: string };
try {
const uniqueValues = await getUniqueFields(field, "rts", req.user);
return res.code(200).send(uniqueValues);
} catch (err) {
return err;
}
}
);
await noteRoutes(fastify);
fastify.addHook("onSend", hideFields("rts"));
}