add config routes

This commit is contained in:
2025-02-18 15:10:51 +05:30
parent fc9abbbec4
commit 5d5cee2d7e
8 changed files with 203 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { getConfig, resetConfig, updateConfig } from "./config.service";
import { UpdateConfigInput } from "./config.schema";
export async function getConfigHandler(req: FastifyRequest, res: FastifyReply) {
try {
const config = await getConfig(req.user);
return res.code(200).send(config);
} catch (err) {
return err;
}
}
export async function updateConfigHandler(
req: FastifyRequest,
res: FastifyReply
) {
const input = req.body as UpdateConfigInput;
try {
const updatedConfig = await updateConfig(input, req.user);
return res.code(200).send(updatedConfig);
} catch (err) {
return err;
}
}
export async function resetConfigHandler(
req: FastifyRequest,
res: FastifyReply
) {
try {
const newConfig = await resetConfig(req.user);
return res.code(200).send(newConfig);
} catch (err) {
return err;
}
}

View File

@@ -0,0 +1,39 @@
import { FastifyInstance } from "fastify";
import {
getConfigHandler,
resetConfigHandler,
updateConfigHandler,
} from "./config.controller";
import { $config } from "./config.schema";
export async function configRoutes(fastify: FastifyInstance) {
fastify.get(
"/",
{
config: { requiredClaims: ["config:read"] },
preHandler: [fastify.authorize],
},
getConfigHandler
);
fastify.patch(
"/",
{
schema: {
body: $config("updateConfigInput"),
},
config: { requiredClaims: ["config:write"] },
preHandler: [fastify.authorize],
},
updateConfigHandler
);
fastify.post(
"/reset",
{
config: { requiredClaims: ["config:write"] },
preHandler: [fastify.authorize],
},
resetConfigHandler
);
}

View File

@@ -0,0 +1,33 @@
import { buildJsonSchemas } from "fastify-zod";
import mongoose from "mongoose";
import { z } from "zod";
export const configModel = mongoose.model(
"config",
new mongoose.Schema({
tenantId: {
type: String,
unique: true,
},
statusMap: Object,
updatedAt: Date,
updatedBy: {
type: mongoose.Types.ObjectId,
ref: "user",
},
}),
"config"
);
const updateConfigInput = z.object({
statusMap: z.record(z.string(), z.array(z.string())).optional(),
});
export type UpdateConfigInput = z.infer<typeof updateConfigInput>;
export const { schemas: configSchemas, $ref: $config } = buildJsonSchemas(
{
updateConfigInput,
},
{ $id: "config" }
);

View File

@@ -0,0 +1,77 @@
import { AuthenticatedUser } from "../auth";
import { configModel, UpdateConfigInput } from "./config.schema";
export async function getConfig(user: AuthenticatedUser) {
return await configModel
.findOne({ tenantId: user.tenantId })
.populate({ path: "updatedBy", select: "pid name avatar" });
}
export async function updateConfig(
input: UpdateConfigInput,
user: AuthenticatedUser
) {
return await configModel
.findOneAndUpdate(
{ tenantId: user.tenantId },
{
updatedAt: new Date(),
updatedBy: user.userId,
...input,
},
{ new: true }
)
.populate({ path: "updatedBy", select: "pid name avatar" });
}
export async function resetConfig(user: AuthenticatedUser) {
return await configModel
.findOneAndUpdate(
{ tenantId: user.tenantId },
{
statusMap: {
Submitted: ["Submitted", "New", "Plans Received"],
"In Review": [
"In Process",
"In Progress",
"In Review",
"Plan Review In Process",
],
"Awaiting Client Reply": [
"Rejected",
"Revision",
"Revision Required",
"Revisions Required",
"Review Verification",
"Awaiting Revisions",
"Pending Client Input",
"Pending Additional Review",
"Awaiting Client Reply",
"Ready to Issue",
"Pending Permit Issuance",
"Approved with Conditions",
],
Issued: ["Permit Issued", "Issued"],
Completed: [
"Approved",
"Administrative Close",
"Closed",
"Closed - Supp-Rev Approved",
"Closed - Void",
"Closed - Finaled",
"Closed - COC Issued",
"Closed - Withdrawn",
"Closed - CO Issued",
"Complete",
"CO Approved",
],
Expired: ["Expired"],
"Cancel/Void": ["Cancel", "Canceled", "Withdrawn"],
},
updatedAt: new Date(),
updatedBy: user.userId,
},
{ new: true }
)
.populate({ path: "updatedBy", select: "pid name avatar" });
}

View File

@@ -9,6 +9,7 @@ import { rtsRoutes } from "./rts/rts.route";
import { taskRoutes } from "./task/task.route";
import { realTimeRoutes } from "./realtime/realtime.route";
import { notificationRoutes } from "./notification/notification.route";
import { configRoutes } from "./config/config.route";
export default async function routes(fastify: FastifyInstance) {
fastify.addHook("preHandler", authHandler);
@@ -20,5 +21,6 @@ export default async function routes(fastify: FastifyInstance) {
fastify.register(rtsRoutes, { prefix: "/rts" });
fastify.register(taskRoutes, { prefix: "/tasks" });
fastify.register(notificationRoutes, { prefix: "/notifications" });
fastify.register(configRoutes, { prefix: "/config" });
fastify.register(realTimeRoutes);
}

View File

@@ -17,6 +17,7 @@ import { taskSchemas } from "./task/task.schema";
import { notificationSchemas } from "./notification/notification.schema";
import { noteSchemas } from "./note/note.schema";
import { webAuthnRoutes } from "./webauthn/webauthn.route";
import { configSchemas } from "./config/config.schema";
const app = fastify({ logger: true, trustProxy: true });
@@ -47,6 +48,7 @@ for (const schema of [
...taskSchemas,
...notificationSchemas,
...noteSchemas,
...configSchemas,
]) {
app.addSchema(schema);
}

View File

@@ -21,4 +21,6 @@ export type Claim =
| "task:write"
| "task:delete"
| "notification:read"
| "notification:write";
| "notification:write"
| "config:read"
| "config:write";

View File

@@ -26,6 +26,8 @@ export const rules: Record<
"task:delete",
"notification:read",
"notification:write",
"config:read",
"config:write",
],
hiddenFields: {
orgs: ["__v"],
@@ -36,7 +38,13 @@ export const rules: Record<
},
},
builder: {
claims: ["permit:read", "file:upload", "file:download", "org:read"],
claims: [
"permit:read",
"file:upload",
"file:download",
"org:read",
"config:read",
],
hiddenFields: {
orgs: ["__v", "isClient", "name"],
permits: ["__v"],