add mark all read route

This commit is contained in:
2025-06-25 15:57:40 +05:30
parent c00b5d29f2
commit c5729d968e
3 changed files with 45 additions and 1 deletions

View File

@@ -1,6 +1,11 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { PageQueryParams } from "../pagination";
import { getUnreadCount, getUserAlerts, markAsRead } from "./alert.service";
import {
getUnreadCount,
getUserAlerts,
markAllRead,
markAsRead,
} from "./alert.service";
export async function listAlertsHandler(
req: FastifyRequest,
@@ -29,6 +34,18 @@ export async function markReadHandler(req: FastifyRequest, res: FastifyReply) {
}
}
export async function markAllReadHandler(
req: FastifyRequest,
res: FastifyReply
) {
try {
const updateResult = await markAllRead(req.user);
return res.code(200).send();
} catch (err) {
return err;
}
}
export async function getUnreadCountHandler(
req: FastifyRequest,
res: FastifyReply

View File

@@ -2,6 +2,7 @@ import { FastifyInstance } from "fastify";
import {
getUnreadCountHandler,
listAlertsHandler,
markAllReadHandler,
markReadHandler,
} from "./alert.controller";
import { $alert } from "./alert.schema";
@@ -35,6 +36,15 @@ export async function alertRoutes(fastify: FastifyInstance) {
markReadHandler
);
fastify.post(
"/markAllRead",
{
config: { requiredClaims: ["alert:write"] },
preHandler: [fastify.authorize],
},
markAllReadHandler
);
fastify.get(
"/count",
{

View File

@@ -93,6 +93,23 @@ export async function markAsRead(alertId: string, user: AuthenticatedUser) {
return modifiedAlert;
}
export async function markAllRead(user: AuthenticatedUser) {
const filters: Array<object> = [
{ recipientType: "user", recipientId: user.userId },
];
if (user.role == "client")
filters.push({ recipientType: "team", recipientId: user.orgId });
else filters.push({ recipientType: "team" });
const updatedResult = await alertsModel.updateMany(
{ $or: filters },
{ $addToSet: { readBy: user.userId } }
);
return updatedResult;
}
export async function getUnreadCount(user: AuthenticatedUser) {
const filters: Array<object> = [
{ recipientType: "user", recipientId: user.userId },