add mark all read route
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
{
|
||||
|
||||
@@ -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 },
|
||||
|
||||
Reference in New Issue
Block a user