feat: add team alerts

This commit is contained in:
2026-01-19 11:56:32 +05:30
parent 46c07e23ad
commit c10b3629fc
9 changed files with 226 additions and 70 deletions

View File

@@ -21,7 +21,7 @@ import { arrayDiff } from "../utils/diff";
export async function createNotification(
input: CreateNotificationInput,
tenantId: string
tenantId: string,
) {
const notification = await notificationModel.create({
...input,
@@ -30,6 +30,9 @@ export async function createNotification(
createdAt: new Date(),
});
if (input.assignedToOrg) {
}
return await notificationModel
.findOne({ pid: notification.pid })
.populate({ path: "assignedTo", select: "pid name avatar" });
@@ -37,7 +40,7 @@ export async function createNotification(
export async function getNotification(
notifId: string,
user: AuthenticatedUser
user: AuthenticatedUser,
) {
return await notificationModel
.findOne({
@@ -50,11 +53,19 @@ export async function getNotification(
export async function updateNotification(
notifId: string,
input: UpdateNotificationInput,
user: AuthenticatedUser
user: AuthenticatedUser,
) {
if (input.assignedToOrg && input.assignedTo) {
input.assignedTo = [];
} else if (input.assignedToOrg) {
input.assignedTo = [];
} else if (input.assignedTo) {
input.assignedToOrg = null;
}
const oldNotification = await notificationModel.findOne(
{ pid: notifId },
{ assignedTo: 1 }
{ assignedTo: 1, assignedToOrg: 1 },
);
const updateNotificationResult = await notificationModel
@@ -64,7 +75,7 @@ export async function updateNotification(
...input,
updatedAt: new Date(),
},
{ new: true }
{ new: true },
)
.populate({ path: "assignedTo", select: "pid name avatar" });
@@ -85,12 +96,12 @@ export async function updateNotification(
},
notifId,
"notifications",
user
user,
);
} else if (key == "assignedTo") {
const newAssignees = arrayDiff(
updateNotificationResult.assignedTo.map((item) => item._id),
oldNotification.assignedTo
oldNotification.assignedTo,
);
if (newAssignees.length > 0) {
@@ -112,7 +123,7 @@ export async function updateNotification(
{
client: updateNotificationResult.client.toString(),
county: updateNotificationResult.county.id.toString(),
}
},
);
}
@@ -122,9 +133,35 @@ export async function updateNotification(
},
notifId,
"notifications",
user
user,
);
}
} else if (key == "assignedToOrg") {
if (
oldNotification.assignedToOrg ==
updateNotificationResult.assignedToOrg
)
continue;
const orgName =
input.assignedToOrg == "agent"
? "Suncoast"
: updateNotificationResult.clientData.name;
await createAlert(
user.tenantId,
`${orgName} assigned to ${updateNotificationResult.permitNumber}`,
"team",
input.assignedToOrg == "client"
? updateNotificationResult.client.toString()
: process.env.SUNCOAST_ID,
updateNotificationResult.pid,
"permits",
{
client: updateNotificationResult.client.toString(),
county: updateNotificationResult.county.id.toString(),
},
);
}
}
}
@@ -134,7 +171,7 @@ export async function updateNotification(
export async function listNotifications(
params: PageQueryParams,
user: AuthenticatedUser
user: AuthenticatedUser,
) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
@@ -173,7 +210,7 @@ export async function listNotifications(
let { taggedFilter, taggedUserFilterIndex } = getTaggedUsersFilter(
filterObj,
sortObj
sortObj,
);
if (taggedUserFilterIndex != -1) filterObj.splice(taggedUserFilterIndex, 1);
@@ -246,7 +283,7 @@ export async function listNotifications(
],
},
},
]
],
);
const notifications = await notificationModel.aggregate(pipeline);