From c5729d968e97a1a861bcb045fecc2d5517ea3133 Mon Sep 17 00:00:00 2001 From: Akhil Meka Date: Wed, 25 Jun 2025 15:57:40 +0530 Subject: [PATCH] add mark all read route --- src/alert/alert.controller.ts | 19 ++++++++++++++++++- src/alert/alert.route.ts | 10 ++++++++++ src/alert/alert.service.ts | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/alert/alert.controller.ts b/src/alert/alert.controller.ts index 7eeada5..dc36f4d 100644 --- a/src/alert/alert.controller.ts +++ b/src/alert/alert.controller.ts @@ -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 diff --git a/src/alert/alert.route.ts b/src/alert/alert.route.ts index ae725b8..e5321ab 100644 --- a/src/alert/alert.route.ts +++ b/src/alert/alert.route.ts @@ -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", { diff --git a/src/alert/alert.service.ts b/src/alert/alert.service.ts index 4cb5ab4..6965a0a 100644 --- a/src/alert/alert.service.ts +++ b/src/alert/alert.service.ts @@ -93,6 +93,23 @@ export async function markAsRead(alertId: string, user: AuthenticatedUser) { return modifiedAlert; } +export async function markAllRead(user: AuthenticatedUser) { + const filters: Array = [ + { 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 = [ { recipientType: "user", recipientId: user.userId },