created changes endpoint
This commit is contained in:
@@ -3,6 +3,7 @@ import { CreatePermitInput, UpdatePermitInput } from "./permit.schema";
|
|||||||
import {
|
import {
|
||||||
createPermit,
|
createPermit,
|
||||||
deletePermit,
|
deletePermit,
|
||||||
|
getchanges,
|
||||||
getPermit,
|
getPermit,
|
||||||
listPermits,
|
listPermits,
|
||||||
searchPermit,
|
searchPermit,
|
||||||
@@ -102,3 +103,17 @@ export async function searchPermitHandler(
|
|||||||
return err;
|
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,6 +1,7 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import {
|
import {
|
||||||
deletePermitHandler,
|
deletePermitHandler,
|
||||||
|
getPermitChangesHandler,
|
||||||
getPermitHandler,
|
getPermitHandler,
|
||||||
listPermitsHandler,
|
listPermitsHandler,
|
||||||
searchPermitHandler,
|
searchPermitHandler,
|
||||||
@@ -120,6 +121,21 @@ 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);
|
await noteRoutes(fastify);
|
||||||
|
|
||||||
fastify.addHook("onSend", hideFields("permits"));
|
fastify.addHook("onSend", hideFields("permits"));
|
||||||
|
|||||||
@@ -156,6 +156,21 @@ const updatePermitInput = z.object({
|
|||||||
export type CreatePermitInput = z.infer<typeof createPermitInput>;
|
export type CreatePermitInput = z.infer<typeof createPermitInput>;
|
||||||
export type UpdatePermitInput = z.infer<typeof updatePermitInput>;
|
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(
|
export const { schemas: permitSchemas, $ref: $permit } = buildJsonSchemas(
|
||||||
{
|
{
|
||||||
createPermitInput,
|
createPermitInput,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
|||||||
import { userModel } from "../user/user.schema";
|
import { userModel } from "../user/user.schema";
|
||||||
import { generateId } from "../utils/id";
|
import { generateId } from "../utils/id";
|
||||||
import {
|
import {
|
||||||
|
changesModel,
|
||||||
CreatePermitInput,
|
CreatePermitInput,
|
||||||
permitFields,
|
permitFields,
|
||||||
permitModel,
|
permitModel,
|
||||||
@@ -177,6 +178,19 @@ export async function updatePermit(
|
|||||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||||
|
|
||||||
if (updatePermitResult) {
|
if (updatePermitResult) {
|
||||||
|
for (const key in input) {
|
||||||
|
if (["manualStatus", "utility"].includes(key)) {
|
||||||
|
await changesModel.create({
|
||||||
|
tenantId: user.tenantId,
|
||||||
|
permitId: permitId,
|
||||||
|
field: key,
|
||||||
|
value: input[key],
|
||||||
|
updatedBy: user.userId,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dbEvents.emit(
|
dbEvents.emit(
|
||||||
"change",
|
"change",
|
||||||
{
|
{
|
||||||
@@ -326,3 +340,19 @@ 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