add config routes
This commit is contained in:
38
src/config/config.controller.ts
Normal file
38
src/config/config.controller.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
39
src/config/config.route.ts
Normal file
39
src/config/config.route.ts
Normal 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
|
||||
);
|
||||
}
|
||||
33
src/config/config.schema.ts
Normal file
33
src/config/config.schema.ts
Normal 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" }
|
||||
);
|
||||
77
src/config/config.service.ts
Normal file
77
src/config/config.service.ts
Normal 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" });
|
||||
}
|
||||
Reference in New Issue
Block a user