added a PATCH/notifications/bulk/monitor

This commit is contained in:
ashu130698 committed 2026-03-03 23:28:11 +05:30
1 parent 868cbae66f
commit 9e06758997
6 files changed
+66 -101

No files matched your search

@@ -154,6 +154,41 @@ class NotificationController {
}
};
bulkEditMonitorNotifications = async (req: Request, res: Response, next: NextFunction) => {
try {
const teamId = requireTeamId(req.user?.teamId);
const { monitorIds, notificationIds, action } = req.body;
if (!monitorIds || !Array.isArray(monitorIds) || monitorIds.length === 0) {
throw new AppError({ message: "Monitor IDs are required", status: 400 });
}
if (!notificationIds || !Array.isArray(notificationIds) || notificationIds.length === 0) {
throw new AppError({ message: "Notification IDs are required", status: 400 });
}
if (!action || !["add", "remove", "set"].includes(action)) {
throw new AppError({ message: "Action must be 'add', 'remove', or 'set'", status: 400 });
}
const modifiedCount = await this.monitorsRepository.bulkUpdateNotifications(
monitorIds,
notificationIds,
action,
teamId
);
return res.status(200).json({
success: true,
msg: `Notifications updated successfully on ${modifiedCount} monitor(s)`,
data: { modifiedCount },
});
} catch (error) {
next(error);
}
};
testAllNotifications = async (req: Request, res: Response, next: NextFunction) => {
try {
const monitorId = req.body.monitorId;
@@ -41,6 +41,7 @@ export interface IMonitorsRepository {
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<MonitorsSummary>;
findGroupsByTeamId(teamId: string): Promise<string[]>;
removeNotificationFromMonitors(notificationId: string): Promise<void>;
bulkUpdateNotifications(monitorIds: string[], notificationIds: string[], action: "add" | "remove" | "set", teamId: string): Promise<number>;
deleteByTeamIdsNotIn(teamIds: string[]): Promise<number>;
findAllMonitorIds(): Promise<string[]>;
}
@@ -298,6 +298,32 @@ class MongoMonitorsRepository implements IMonitorsRepository {
await MonitorModel.updateMany({ notifications: notificationId }, { $pull: { notifications: notificationId } });
};
bulkUpdateNotifications = async (
monitorIds: string[],
notificationIds: string[],
action: "add" | "remove" | "set",
teamId: string
): Promise<number> => {
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
const filter = { _id: { $in: objectIds }, teamId: new mongoose.Types.ObjectId(teamId) };
let update;
switch (action) {
case "add":
update = { $addToSet: { notifications: { $each: notificationIds } } };
break;
case "remove":
update = { $pull: { notifications: { $in: notificationIds } } };
break;
case "set":
update = { $set: { notifications: notificationIds } };
break;
}
const result = await MonitorModel.updateMany(filter, update);
return result.modifiedCount;
};
private mapDocuments = (documents: MonitorDocument[]): Monitor[] => {
if (!documents?.length) {
return [];
+2
View File
@@ -17,6 +17,8 @@ class NotificationRoutes {
this.router.get("/team", this.notificationController.getNotificationsByTeamId);
this.router.patch("/bulk/monitors", this.notificationController.bulkEditMonitorNotifications);
this.router.get("/:id", this.notificationController.getNotificationById);
this.router.delete("/:id", this.notificationController.deleteNotification);
this.router.patch("/:id", this.notificationController.editNotification);