Files
Checkmate/server/src/service/infrastructure/notificationsService.ts
T

176 lines
7.5 KiB
TypeScript
Raw Normal View History

import type { HardwareStatusPayload, Monitor, MonitorStatusResponse, Notification, MonitorStatus } from "@/types/index.js";
2026-01-21 13:51:04 -08:00
import { IMonitorsRepository, INotificationsRepository } from "@/repositories/index.js";
2026-01-19 21:39:02 +00:00
import { INotificationProvider } from "./notificationProviders/INotificationProvider.js";
2026-02-15 18:20:42 +00:00
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
import type { ISettingsService } from "@/service/system/settingsService.js";
2026-02-16 18:43:03 +00:00
import { ILogger } from "@/utils/logger.js";
2026-02-16 20:18:25 +00:00
import type { INotificationMessageBuilder } from "@/service/infrastructure/notificationMessageBuilder.js";
2026-01-18 22:52:59 +00:00
export interface INotificationsService {
2026-01-21 13:51:04 -08:00
createNotification: (notificationData: Partial<Notification>) => Promise<Notification>;
findById: (id: string, teamId: string) => Promise<Notification>;
findNotificationsByTeamId: (teamId: string) => Promise<Notification[]>;
updateById(id: string, teamId: string, updateData: Partial<Notification>): Promise<Notification>;
deleteById: (id: string, teamId: string) => Promise<Notification>;
2026-02-15 18:20:42 +00:00
handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise<boolean>;
2026-01-21 13:51:04 -08:00
2026-01-19 21:39:02 +00:00
sendTestNotification: (notification: Notification) => Promise<boolean>;
testAllNotifications: (notificationIds: string[]) => Promise<boolean>;
2026-01-18 22:52:59 +00:00
}
2026-01-19 21:39:02 +00:00
const SERVICE_NAME = "NotificationsService";
2026-01-18 22:52:59 +00:00
export class NotificationsService implements INotificationsService {
2026-01-19 21:39:02 +00:00
static SERVICE_NAME = SERVICE_NAME;
2026-01-19 00:48:20 +00:00
private notificationsRepository: INotificationsRepository;
2026-01-21 13:51:04 -08:00
private monitorsRepository: IMonitorsRepository;
2026-01-19 21:39:02 +00:00
private webhookProvider: INotificationProvider;
private emailProvider: INotificationProvider;
private slackProvider: INotificationProvider;
private discordProvider: INotificationProvider;
private pagerDutyProvider: INotificationProvider;
private matrixProvider: INotificationProvider;
2026-02-16 18:43:03 +00:00
private logger: ILogger;
private settingsService: ISettingsService;
2026-02-16 20:18:25 +00:00
private notificationMessageBuilder: INotificationMessageBuilder;
2026-01-19 18:58:17 +00:00
2026-01-19 19:30:46 +00:00
constructor(
notificationsRepository: INotificationsRepository,
2026-01-21 13:51:04 -08:00
monitorsRepository: IMonitorsRepository,
2026-01-19 21:39:02 +00:00
webhookProvider: INotificationProvider,
emailProvider: INotificationProvider,
slackProvider: INotificationProvider,
discordProvider: INotificationProvider,
pagerDutyProvider: INotificationProvider,
matrixProvider: INotificationProvider,
settingsService: ISettingsService,
2026-02-16 20:18:25 +00:00
logger: ILogger,
notificationMessageBuilder: INotificationMessageBuilder
2026-01-19 19:30:46 +00:00
) {
2026-01-19 00:48:20 +00:00
this.notificationsRepository = notificationsRepository;
2026-01-21 13:51:04 -08:00
this.monitorsRepository = monitorsRepository;
2026-01-19 21:39:02 +00:00
this.webhookProvider = webhookProvider;
this.emailProvider = emailProvider;
this.slackProvider = slackProvider;
this.discordProvider = discordProvider;
this.pagerDutyProvider = pagerDutyProvider;
this.matrixProvider = matrixProvider;
this.settingsService = settingsService;
2026-01-19 21:39:02 +00:00
this.logger = logger;
2026-02-16 20:18:25 +00:00
this.notificationMessageBuilder = notificationMessageBuilder;
2026-01-19 00:48:20 +00:00
}
2026-02-16 18:25:35 +00:00
private send = async (
notification: Notification,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision
): Promise<boolean> => {
const settings = this.settingsService.getSettings();
const clientHost = settings.clientHost || "Host not defined";
2026-02-16 18:25:35 +00:00
2026-01-19 18:58:17 +00:00
switch (notification.type) {
2026-01-19 19:30:46 +00:00
case "email":
2026-02-16 18:25:35 +00:00
return await this.emailProvider.sendAlert(notification, monitor, monitorStatusResponse, decision, clientHost);
2026-01-19 19:30:46 +00:00
case "slack":
2026-02-16 18:25:35 +00:00
return await this.slackProvider.sendAlert(notification, monitor, monitorStatusResponse, decision, clientHost);
2026-01-19 19:30:46 +00:00
case "discord":
2026-02-16 18:25:35 +00:00
return await this.discordProvider.sendAlert(notification, monitor, monitorStatusResponse, decision, clientHost);
2026-01-19 21:39:02 +00:00
case "pager_duty":
2026-02-16 18:25:35 +00:00
return await this.pagerDutyProvider.sendAlert(notification, monitor, monitorStatusResponse, decision, clientHost);
2026-01-19 21:39:02 +00:00
case "matrix":
2026-02-16 18:25:35 +00:00
return await this.matrixProvider.sendAlert(notification, monitor, monitorStatusResponse, decision, clientHost);
2026-01-19 19:30:46 +00:00
case "webhook":
2026-02-16 18:25:35 +00:00
return await this.webhookProvider.sendAlert(notification, monitor, monitorStatusResponse, decision, clientHost);
2026-01-19 19:30:46 +00:00
default:
return false;
2026-01-19 18:58:17 +00:00
}
2026-01-19 00:48:20 +00:00
};
2026-02-16 18:25:35 +00:00
private sendNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => {
2026-01-19 18:58:17 +00:00
const notificationIds = monitor.notifications ?? [];
const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds);
2026-01-28 18:23:22 +05:30
2026-02-16 18:39:41 +00:00
const tasks = notifications.map((notification) => this.send(notification, monitor, monitorStatusResponse, decision));
2026-01-28 18:23:22 +05:30
2026-01-19 18:58:17 +00:00
const outcomes = await Promise.all(tasks);
const succeeded = outcomes.filter(Boolean).length;
const failed = outcomes.length - succeeded;
if (failed > 0) {
2026-01-19 21:39:02 +00:00
this.logger.warn({
message: `Notification send completed with ${succeeded} success, ${failed} failure(s)`,
service: SERVICE_NAME,
2026-02-16 18:39:41 +00:00
method: "sendNotifications",
2026-01-19 21:39:02 +00:00
});
2026-01-19 18:58:17 +00:00
}
2026-02-16 18:39:41 +00:00
// Return true if all notifications succeeded
2026-01-19 18:58:17 +00:00
return succeeded === notifications.length;
2026-01-19 00:48:20 +00:00
};
2026-02-15 18:20:42 +00:00
handleNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => {
if (!decision.shouldSendNotification) {
return false;
2026-01-18 23:38:40 +00:00
}
2026-02-16 20:18:25 +00:00
const settings = this.settingsService.getSettings();
const clientHost = settings.clientHost || "Host not defined";
const notificationMessage = this.notificationMessageBuilder.buildMessage(monitor, monitorStatusResponse, decision, clientHost);
2026-02-15 18:20:42 +00:00
// Send notifications based on decision
2026-02-16 18:25:35 +00:00
return await this.sendNotifications(monitor, monitorStatusResponse, decision);
2026-01-18 22:52:59 +00:00
};
2026-01-19 21:39:02 +00:00
sendTestNotification = async (notification: Notification) => {
switch (notification.type) {
case "email":
return await this.emailProvider.sendTestAlert(notification);
case "slack":
return await this.slackProvider.sendTestAlert(notification);
case "discord":
return await this.discordProvider.sendTestAlert(notification);
case "pager_duty":
return await this.pagerDutyProvider.sendTestAlert(notification);
case "matrix":
return await this.matrixProvider.sendTestAlert(notification);
case "webhook":
return await this.webhookProvider.sendTestAlert(notification);
default:
return false;
}
};
testAllNotifications = async (notificationIds: string[]) => {
const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds);
const tasks = notifications.map((notification) => this.sendTestNotification(notification));
const outcomes = await Promise.all(tasks);
const succeeded = outcomes.filter(Boolean).length;
const failed = outcomes.length - succeeded;
if (failed > 0) {
return false;
}
return true;
};
2026-01-21 13:51:04 -08:00
createNotification = async (notificationData: Partial<Notification>): Promise<Notification> => {
return await this.notificationsRepository.create(notificationData);
};
findById = async (id: string, teamId: string): Promise<Notification> => {
return await this.notificationsRepository.findById(id, teamId);
};
findNotificationsByTeamId = async (teamId: string): Promise<Notification[]> => {
return await this.notificationsRepository.findByTeamId(teamId);
};
updateById = async (id: string, teamId: string, updateData: Partial<Notification>): Promise<Notification> => {
return await this.notificationsRepository.updateById(id, teamId, updateData);
};
deleteById = async (id: string, teamId: string): Promise<Notification> => {
const deleted = await this.notificationsRepository.deleteById(id, teamId);
await this.monitorsRepository.removeNotificationFromMonitors(id);
return deleted;
};
2026-01-18 06:02:07 +00:00
}