feat: make assignedTo field accept multiple values for all collections

This commit is contained in:
2025-11-18 13:32:47 +05:30
parent b7f63479a6
commit 8f5c0a1827
12 changed files with 202 additions and 108 deletions

View File

@@ -17,6 +17,7 @@ import {
import { getUser } from "../user/user.service";
import { createNote } from "../note/note.service";
import { createAlert } from "../alert/alert.service";
import { arrayDiff } from "../utils/diff";
export async function createNotification(
input: CreateNotificationInput,
@@ -51,6 +52,11 @@ export async function updateNotification(
input: UpdateNotificationInput,
user: AuthenticatedUser
) {
const oldNotification = await notificationModel.findOne(
{ pid: notifId },
{ assignedTo: 1 }
);
const updateNotificationResult = await notificationModel
.findOneAndUpdate(
{ $and: [{ tenantId: user.tenantId }, { pid: notifId }] },
@@ -64,16 +70,11 @@ export async function updateNotification(
if (updateNotificationResult) {
for (const key in input) {
if (["status", "assignedTo"].includes(key)) {
if (["status"].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]}'`;
}
@@ -86,23 +87,39 @@ export async function updateNotification(
"notifications",
user
);
} else if (key == "assignedTo") {
const newAssignees = arrayDiff(
updateNotificationResult.assignedTo.map((item) => item._id),
oldNotification.assignedTo
);
if (newAssignees.length > 0) {
let msg = "Assigned to:\n\n";
for (const assignee of newAssignees) {
const user = await getUser(assignee);
if (!user) continue;
msg += `${user.firstName + " " + user.lastName}\n`;
await createAlert(
user.tenantId,
`You are assigned to ${updateNotificationResult.permitNumber}`,
"user",
assignee,
updateNotificationResult.pid,
"permits"
);
}
if (key === "assignedTo") {
await createNote(
{ content: msg },
updateNotificationResult.permitId,
{
content: msg,
},
notifId,
"notifications",
user
);
await createAlert(
user.tenantId,
`You are assigned to ${updateNotificationResult.permitNumber}`,
"user",
input.assignedTo,
updateNotificationResult.permitId,
"permits"
);
}
}
}
@@ -190,13 +207,14 @@ export async function listNotifications(
updatedAt: 1,
taggedUsers: 1,
assignedTo: {
$let: {
vars: { assignedTo: { $arrayElemAt: ["$assignedTo", 0] } },
$map: {
input: "$assignedTo",
as: "user",
in: {
_id: "$$assignedTo._id",
pid: "$$assignedTo.pid",
name: "$$assignedTo.name",
avatar: "$$assignedTo.avatar",
_id: "$$user._id",
pid: "$$user.pid",
name: "$$user.name",
avatar: "$$user.avatar",
},
},
},