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

@@ -13,11 +13,12 @@ import {
getTaggedUsersFilter,
PageQueryParams,
} from "../pagination";
import { getUserWithoutPopulate } from "../user/user.service";
import { getUser, getUserWithoutPopulate } from "../user/user.service";
import mongoose from "mongoose";
import { rtsPipeline } from "../utils/pipeline";
import { createAlert } from "../alert/alert.service";
import { createNote } from "../note/note.service";
import { arrayDiff } from "../utils/diff";
export async function createRts(
input: CreateRtsInput,
@@ -126,7 +127,7 @@ export async function listRts(
from: "users",
localField: "assignedTo",
foreignField: "_id",
as: "assignedToRec",
as: "assignedTo",
},
},
{
@@ -180,13 +181,14 @@ export async function listRts(
},
},
assignedTo: {
$let: {
vars: { assignedTo: { $arrayElemAt: ["$assignedToRec", 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",
},
},
},
@@ -222,6 +224,7 @@ export async function updateRts(
input: UpdateRtsInput,
user: AuthenticatedUser
) {
const oldRts = await rtsModel.findOne({ pid: id }, { assignedTo: 1 });
const updatedRts = await rtsModel
.findOneAndUpdate({ pid: id, tenantId: user.tenantId }, input, {
new: true,
@@ -241,14 +244,39 @@ export async function updateRts(
}
if (updatedRts && input.assignedTo) {
await createAlert(
user.tenantId,
`You are assigned to RTS`,
"user",
input.assignedTo,
updatedRts.pid,
"rts"
const newAssignees = arrayDiff(
updatedRts.assignedTo.map((item) => item._id),
oldRts.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 RTS`,
"user",
assignee,
updatedRts.pid,
"rts"
);
}
await createNote(
{
content: msg,
},
id,
"rts",
user
);
}
}
return updatedRts;