feat: add metaFields to alerts

This commit is contained in:
2025-12-15 10:11:05 +05:30
parent e3f09cd4aa
commit 7dc3386847
7 changed files with 57 additions and 11 deletions

View File

@@ -11,6 +11,11 @@ export const alertsModel = mongoose.model(
title: { type: String, required: true }, title: { type: String, required: true },
referenceId: String, referenceId: String,
referenceCollection: String, referenceCollection: String,
metaFields: {
client: { type: String, ref: "organization" },
county: { type: String, ref: "organization" },
permitType: String,
},
recipientType: { recipientType: {
type: String, type: String,
required: true, required: true,
@@ -31,6 +36,13 @@ const alertResponse = z.object({
read: z.boolean(), read: z.boolean(),
referenceId: z.string().optional(), referenceId: z.string().optional(),
referenceCollection: z.string().optional(), referenceCollection: z.string().optional(),
metaFields: z
.object({
client: z.any().optional(),
county: z.any().optional(),
permitType: z.string().optional(),
})
.optional(),
recipientType: z.enum(["user", "team"]), recipientType: z.enum(["user", "team"]),
createdAt: z.date(), createdAt: z.date(),
}); });

View File

@@ -10,7 +10,12 @@ export async function createAlert(
recipientType: "user" | "team", recipientType: "user" | "team",
recipientId: string, recipientId: string,
referenceId?: string, referenceId?: string,
referenceCollection?: string referenceCollection?: string,
metaFields?: {
client?: String;
county?: String;
permitType?: String;
}
) { ) {
const newAlert = await alertsModel.create({ const newAlert = await alertsModel.create({
tenantId, tenantId,
@@ -20,6 +25,7 @@ export async function createAlert(
recipientId, recipientId,
referenceId, referenceId,
referenceCollection, referenceCollection,
metaFields,
}); });
dbEvents.emit( dbEvents.emit(
@@ -65,7 +71,9 @@ export async function getUserAlerts(
}) })
.sort({ createdAt: -1 }) .sort({ createdAt: -1 })
.limit(pageSize) .limit(pageSize)
.skip((page - 1) * pageSize); .skip((page - 1) * pageSize)
.populate({ path: "metaFields.client", select: "pid name avatar" })
.populate({ path: "metaFields.county", select: "pid name avatar" });
const modifiedAlerts = alerts.map((alert) => { const modifiedAlerts = alerts.map((alert) => {
return { return {

View File

@@ -108,7 +108,11 @@ export async function updateNotification(
"user", "user",
assignee, assignee,
updateNotificationResult.pid, updateNotificationResult.pid,
"permits" "permits",
{
client: updateNotificationResult.client.toString(),
county: updateNotificationResult.county.id.toString(),
}
); );
} }

View File

@@ -175,7 +175,11 @@ export async function updatePayment(
"user", "user",
assignee, assignee,
updatedPayment.pid, updatedPayment.pid,
"tasks" "tasks",
{
client: updatedPayment.client.toString(),
county: updatedPayment.county.id.toString(),
}
); );
} }
} }

View File

@@ -278,7 +278,6 @@ export async function updatePermit(
{ ...input, lastUpdateDate: new Date() }, { ...input, lastUpdateDate: new Date() },
{ new: true } { new: true }
) )
.populate({ path: "county", 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" });
@@ -319,7 +318,11 @@ export async function updatePermit(
"user", "user",
userId, userId,
updatePermitResult.pid, updatePermitResult.pid,
"permits" "permits",
{
client: updatePermitResult.client.toString(),
county: updatePermitResult.county.id.toString(),
}
); );
} }
} }
@@ -358,7 +361,11 @@ export async function updatePermit(
"user", "user",
assignee, assignee,
updatePermitResult.pid, updatePermitResult.pid,
"permits" "permits",
{
client: updatePermitResult.client.toString(),
county: updatePermitResult.county.id.toString(),
}
); );
} }

View File

@@ -113,7 +113,11 @@ export async function updateProcessed(
"user", "user",
assignee, assignee,
updateProcessedResult.pid, updateProcessedResult.pid,
"processed" "processed",
{
client: updateProcessedResult.client.toString(),
county: updateProcessedResult.county.id.toString(),
}
); );
} }

View File

@@ -244,8 +244,8 @@ export async function updateRts(
new: true, new: true,
}) })
.populate({ path: "createdBy", select: "pid name avatar" }) .populate({ path: "createdBy", select: "pid name avatar" })
.populate({ path: "county", select: "pid name avatar" }) .populate({ path: "county", select: "_id pid name avatar" })
.populate({ path: "client", select: "pid name avatar" }) .populate({ path: "client", select: "_id pid name avatar" })
.populate({ path: "assignedTo", select: "pid name avatar" }); .populate({ path: "assignedTo", select: "pid name avatar" });
if (updateRts && input.permitType) { if (updateRts && input.permitType) {
@@ -278,7 +278,14 @@ export async function updateRts(
"user", "user",
assignee, assignee,
updatedRts.pid, updatedRts.pid,
"rts" "rts",
{
//@ts-ignore
client: updatedRts.client._id.toString(),
//@ts-ignore
county: updatedRts.county._id.toString(),
permitType: updatedRts.permitType,
}
); );
} }