import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; import { PageQueryParams } from "../pagination"; import { getProcessedPermit, listProcessedPermits, updateProcessed, } from "./processed.service"; import { $processed, UpdateProcessedInput } from "./processed.schema"; import { noteRoutes } from "../note/note.route"; import { getUniqueFields } from "../unique"; export async function processedRoutes(fastify: FastifyInstance) { fastify.get( "/", { schema: { querystring: $processed("pageQueryParams"), }, config: { requiredClaims: ["permit:read"] }, preHandler: [fastify.authorize], }, async (req: FastifyRequest, res: FastifyReply) => { const params = req.query as PageQueryParams; try { const permits = await listProcessedPermits(params, req.user); return res.code(200).send(permits); } catch (err) { return err; } } ); fastify.get( "/search", { schema: { querystring: $processed("pageQueryParams"), }, config: { requiredClaims: ["permit:read"] }, preHandler: [fastify.authorize], }, async (req: FastifyRequest, res: FastifyReply) => { const params = req.query as PageQueryParams; try { const permits = await listProcessedPermits(params, req.user); return res.code(200).send(permits); } catch (err) { return err; } } ); fastify.get( "/:permitId", { schema: { params: { type: "object", properties: { permitId: { type: "string" } }, }, }, config: { requiredClaims: ["permit:read"] }, preHandler: [fastify.authorize], }, async (req: FastifyRequest, res: FastifyReply) => { const { permitId } = req.params as { permitId: string }; try { const permit = await getProcessedPermit(permitId, req.user.tenantId); return res.code(200).send(permit); } catch (err) { return err; } } ); fastify.patch( "/:permitId", { schema: { params: { type: "object", properties: { permitId: { type: "string" } }, }, body: $processed("updateProcessedInput"), }, config: { requiredClaims: ["permit:write"] }, preHandler: [fastify.authorize], }, async (req: FastifyRequest, res: FastifyReply) => { const { permitId } = req.params as { permitId: string }; const input = req.body as UpdateProcessedInput; try { const permit = await updateProcessed(input, permitId, req.user); return res.code(200).send(permit); } catch (err) { return err; } } ); fastify.get( "/fields/:field", { schema: { params: { type: "object", properties: { field: { type: "string" }, }, }, }, config: { requiredClaims: ["permit:read"] }, preHandler: [fastify.authorize], }, async (req: FastifyRequest, res: FastifyReply) => { const { field } = req.params as { field: string }; try { const uniqueValues = await getUniqueFields( field, "processed", req.user ); return res.code(200).send(uniqueValues); } catch (err) { return err; } } ); await noteRoutes(fastify); }