add notes on update

This commit is contained in:
2025-05-28 18:00:21 +05:30
parent 32c8ae0b60
commit 2b5653abad
4 changed files with 84 additions and 14 deletions

View File

@@ -50,7 +50,7 @@ export async function updateNotificationHandler(
const updatedNotification = await updateNotification( const updatedNotification = await updateNotification(
notifId, notifId,
input, input,
req.user.tenantId req.user
); );
if (!updatedNotification) if (!updatedNotification)

View File

@@ -10,6 +10,8 @@ import {
notificationModel, notificationModel,
UpdateNotificationInput, UpdateNotificationInput,
} from "./notification.schema"; } from "./notification.schema";
import { getUser } from "../user/user.service";
import { createNote } from "../note/note.service";
export async function createNotification( export async function createNotification(
input: CreateNotificationInput, input: CreateNotificationInput,
@@ -30,11 +32,11 @@ export async function createNotification(
export async function updateNotification( export async function updateNotification(
notifId: string, notifId: string,
input: UpdateNotificationInput, input: UpdateNotificationInput,
tenantId: string user: AuthenticatedUser
) { ) {
return await notificationModel const updateNotificationResult = await notificationModel
.findOneAndUpdate( .findOneAndUpdate(
{ $and: [{ tenantId: tenantId }, { pid: notifId }] }, { $and: [{ tenantId: user.tenantId }, { pid: notifId }] },
{ {
...input, ...input,
updatedAt: new Date(), updatedAt: new Date(),
@@ -42,6 +44,35 @@ export async function updateNotification(
{ new: true } { new: true }
) )
.populate({ path: "assignedTo", select: "pid name avatar" }); .populate({ path: "assignedTo", select: "pid name avatar" });
if (updateNotificationResult) {
for (const key in input) {
if (["status", "assignedTo"].includes(key)) {
let msg = "";
if (input[key] === null) {
msg = `Cleared ${key}`;
} else if (key == "assignedTo") {
const user = await getUser(input.assignedTo);
if (!user) continue;
msg = `Assigned to ${user.firstName + " " + user.lastName}`;
} else {
msg = `Updated ${key} to '${input[key]}'`;
}
await createNote(
{
content: msg,
},
notifId,
user
);
}
}
}
return updateNotificationResult;
} }
export async function listNotifications( export async function listNotifications(

View File

@@ -11,6 +11,8 @@ import { permitPipeline } from "../utils/pipeline";
import { AuthenticatedUser } from "../auth"; import { AuthenticatedUser } from "../auth";
import mongoose from "mongoose"; import mongoose from "mongoose";
import { noteModel } from "../note/note.schema"; import { noteModel } from "../note/note.schema";
import { getUser } from "../user/user.service";
import { createNote } from "../note/note.service";
export async function createPermit( export async function createPermit(
input: CreatePermitInput, input: CreatePermitInput,
@@ -181,15 +183,27 @@ export async function updatePermit(
if (updatePermitResult) { if (updatePermitResult) {
for (const key in input) { for (const key in input) {
if (["manualStatus", "utility"].includes(key)) { if (["manualStatus", "utility", "assignedTo"].includes(key)) {
await noteModel.create({ let msg = "";
tenantId: user.tenantId,
pid: generateId(), if (input[key] === null) {
resourceId: permitId, msg = `Cleared ${key}`;
content: `Updated ${key} to '${input[key]}'`, } else if (key == "assignedTo") {
createdAt: new Date(), const user = await getUser(input.assignedTo);
createdBy: user.userId, if (!user) continue;
});
msg = `Assigned to ${user.firstName + " " + user.lastName}`;
} else {
msg = `Updated ${key} to '${input[key]}'`;
}
await createNote(
{
content: msg,
},
permitId,
user
);
} }
} }

View File

@@ -8,6 +8,7 @@ import {
processedModel, processedModel,
UpdateProcessedInput, UpdateProcessedInput,
} from "./processed.schema"; } from "./processed.schema";
import { createNote } from "../note/note.service";
export async function getProcessedPermit(permitId: String, tenantId: String) { export async function getProcessedPermit(permitId: String, tenantId: String) {
return await processedModel.findOne({ return await processedModel.findOne({
@@ -20,7 +21,7 @@ export async function updateProcessed(
permitId: string, permitId: string,
user: AuthenticatedUser user: AuthenticatedUser
) { ) {
return await processedModel const updateProcessedResult = await processedModel
.findOneAndUpdate( .findOneAndUpdate(
{ {
$and: [{ tenantId: user.tenantId }, { pid: permitId }], $and: [{ tenantId: user.tenantId }, { pid: permitId }],
@@ -32,6 +33,30 @@ export async function updateProcessed(
.populate({ path: "client", select: "pid name avatar" }) .populate({ path: "client", select: "pid name avatar" })
.populate({ path: "assignedTo", select: "pid name avatar" }) .populate({ path: "assignedTo", select: "pid name avatar" })
.populate({ path: "createdBy", select: "pid name avatar" }); .populate({ path: "createdBy", select: "pid name avatar" });
if (updateProcessedResult) {
for (const key in input) {
if (["manualStatus", "utility"].includes(key)) {
let msg = "";
if (input[key] === null) {
msg = `Cleared ${key}`;
} else {
msg = `Updated ${key} to '${input[key]}'`;
}
await createNote(
{
content: msg,
},
permitId,
user
);
}
}
return updateProcessedResult;
}
} }
export async function listProcessedPermits( export async function listProcessedPermits(