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(
notifId,
input,
req.user.tenantId
req.user
);
if (!updatedNotification)

View File

@@ -10,6 +10,8 @@ import {
notificationModel,
UpdateNotificationInput,
} from "./notification.schema";
import { getUser } from "../user/user.service";
import { createNote } from "../note/note.service";
export async function createNotification(
input: CreateNotificationInput,
@@ -30,11 +32,11 @@ export async function createNotification(
export async function updateNotification(
notifId: string,
input: UpdateNotificationInput,
tenantId: string
user: AuthenticatedUser
) {
return await notificationModel
const updateNotificationResult = await notificationModel
.findOneAndUpdate(
{ $and: [{ tenantId: tenantId }, { pid: notifId }] },
{ $and: [{ tenantId: user.tenantId }, { pid: notifId }] },
{
...input,
updatedAt: new Date(),
@@ -42,6 +44,35 @@ export async function updateNotification(
{ new: true }
)
.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(