added new fields to permit model, removed changes endpoints
This commit is contained in:
@@ -3,7 +3,6 @@ import { CreatePermitInput, UpdatePermitInput } from "./permit.schema";
|
||||
import {
|
||||
createPermit,
|
||||
deletePermit,
|
||||
getchanges,
|
||||
getPermit,
|
||||
listPermits,
|
||||
searchPermit,
|
||||
@@ -103,17 +102,3 @@ export async function searchPermitHandler(
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPermitChangesHandler(
|
||||
req: FastifyRequest,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const { permitId } = req.params as { permitId: string };
|
||||
|
||||
try {
|
||||
const changes = await getchanges(permitId, req.user);
|
||||
return res.code(200).send(changes);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import {
|
||||
deletePermitHandler,
|
||||
getPermitChangesHandler,
|
||||
getPermitHandler,
|
||||
listPermitsHandler,
|
||||
searchPermitHandler,
|
||||
@@ -121,21 +120,6 @@ export async function permitRoutes(fastify: FastifyInstance) {
|
||||
}
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/:permitId/changes",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: "object",
|
||||
properties: { permitId: { type: "string" } },
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
getPermitChangesHandler
|
||||
);
|
||||
|
||||
await noteRoutes(fastify);
|
||||
|
||||
fastify.addHook("onSend", hideFields("permits"));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { z } from "zod";
|
||||
import { string, z } from "zod";
|
||||
import mongoose from "mongoose";
|
||||
import { buildJsonSchemas } from "fastify-zod";
|
||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||
@@ -65,6 +65,10 @@ const permitSchema = new mongoose.Schema({
|
||||
lastUpdateDate: Date,
|
||||
statusUpdated: Date,
|
||||
issuedDate: Date,
|
||||
communityName: String,
|
||||
lot: String,
|
||||
jobNumber: String,
|
||||
startDate: Date,
|
||||
}).index({ tenantId: 1, permitNumber: 1 }, { unique: true });
|
||||
|
||||
export const permitFields = Object.keys(permitSchema.paths).filter(
|
||||
@@ -151,26 +155,15 @@ const updatePermitInput = z.object({
|
||||
.optional(),
|
||||
assignedTo: z.string().optional(),
|
||||
newPayment: z.array(z.any()).optional(),
|
||||
communityName: z.string().optional(),
|
||||
lot: z.string().optional(),
|
||||
jobNumber: z.string().optional(),
|
||||
startDate: z.date().optional(),
|
||||
});
|
||||
|
||||
export type CreatePermitInput = z.infer<typeof createPermitInput>;
|
||||
export type UpdatePermitInput = z.infer<typeof updatePermitInput>;
|
||||
|
||||
export const changesModel = mongoose.model(
|
||||
"change",
|
||||
new mongoose.Schema({
|
||||
tenantId: String,
|
||||
permitId: String,
|
||||
field: String,
|
||||
value: String,
|
||||
updatedBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "user",
|
||||
},
|
||||
updatedAt: Date,
|
||||
})
|
||||
);
|
||||
|
||||
export const { schemas: permitSchemas, $ref: $permit } = buildJsonSchemas(
|
||||
{
|
||||
createPermitInput,
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { orgModel } from "../organization/organization.schema";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { userModel } from "../user/user.schema";
|
||||
import { generateId } from "../utils/id";
|
||||
import {
|
||||
changesModel,
|
||||
CreatePermitInput,
|
||||
permitFields,
|
||||
permitModel,
|
||||
@@ -13,6 +10,7 @@ import { ChangeEvent, dbEvents } from "../realtime";
|
||||
import { permitPipeline } from "../utils/pipeline";
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import mongoose from "mongoose";
|
||||
import { noteModel } from "../note/note.schema";
|
||||
|
||||
export async function createPermit(
|
||||
input: CreatePermitInput,
|
||||
@@ -121,6 +119,10 @@ export async function listPermits(
|
||||
lastUpdateDate: 1,
|
||||
statusUpdated: 1,
|
||||
issuedDate: 1,
|
||||
communityName: 1,
|
||||
lot: 1,
|
||||
jobNumber: 1,
|
||||
startDate: 1,
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
||||
@@ -180,13 +182,13 @@ export async function updatePermit(
|
||||
if (updatePermitResult) {
|
||||
for (const key in input) {
|
||||
if (["manualStatus", "utility"].includes(key)) {
|
||||
await changesModel.create({
|
||||
await noteModel.create({
|
||||
tenantId: user.tenantId,
|
||||
permitId: permitId,
|
||||
field: key,
|
||||
value: input[key],
|
||||
updatedBy: user.userId,
|
||||
updatedAt: new Date(),
|
||||
pid: generateId(),
|
||||
resourceId: permitId,
|
||||
content: `Updated ${key} to '${input[key]}'`,
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -303,6 +305,10 @@ export async function searchPermit(
|
||||
lastUpdateDate: 1,
|
||||
statusUpdated: 1,
|
||||
issuedDate: 1,
|
||||
communityName: 1,
|
||||
lot: 1,
|
||||
jobNumber: 1,
|
||||
startDate: 1,
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
||||
@@ -340,19 +346,3 @@ export async function searchPermit(
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function getchanges(permitId: string, user: AuthenticatedUser) {
|
||||
if (user.role == "client") {
|
||||
const permit = await permitModel.findOne({
|
||||
pid: permitId,
|
||||
tenantId: user.tenantId,
|
||||
});
|
||||
if (!permit || permit.client.toString() !== user.orgId) return [];
|
||||
}
|
||||
|
||||
return await changesModel
|
||||
.find({
|
||||
$and: [{ tenantId: user.tenantId }, { permitId: permitId }],
|
||||
})
|
||||
.populate({ path: "updatedBy", select: "_id pid name avatar" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user