Files
Checkmate/server/src/domain/notifications/notification.service.ts
T

187 lines
7.4 KiB
TypeScript
Raw Normal View History

import type { Monitor } from "@/domain/monitors/monitor.type.js";
2026-06-09 12:34:15 -07:00
import type { Notification } from "@/domain/notifications/notification.type.js";
import type { MonitorStatusResponse } from "@/types/network.js";
import type { NotificationMessage } from "@/domain/notifications/notification.type.js";
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js";
import { INotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
2026-09-20 17:37:42 -07:00
import type { MonitorActionDecision } from "@/worker/worker.interface.js";
2026-06-09 12:34:15 -07:00
import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
2026-02-16 18:43:03 +00:00
import { ILogger } from "@/utils/logger.js";
2026-06-09 12:34:15 -07:00
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
import type { NotificationChannel } from "@/domain/notifications/notification.type.js";
export type NotificationProviderRegistry = Record<NotificationChannel, INotificationProvider>;
2026-01-18 22:52:59 +00:00
export interface INotificationsService {
2026-03-11 13:28:48 -07:00
createNotification: (notificationData: Partial<Notification>, userId: string, teamId: string) => Promise<Notification>;
2026-01-21 13:51:04 -08:00
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-03-11 13:28:48 -07:00
sendTestNotification: (notification: Partial<Notification>) => Promise<boolean>;
2026-01-19 21:39:02 +00:00
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;
private providers: NotificationProviderRegistry;
private settingsService: ISettingsService;
private logger: ILogger;
2026-02-16 20:18:25 +00:00
private notificationMessageBuilder: INotificationMessageBuilder;
2026-01-19 18:58:17 +00:00
constructor({
notificationsRepository,
monitorsRepository,
providers,
settingsService,
logger,
notificationMessageBuilder,
}: {
notificationsRepository: INotificationsRepository;
monitorsRepository: IMonitorsRepository;
providers: NotificationProviderRegistry;
settingsService: ISettingsService;
logger: ILogger;
notificationMessageBuilder: INotificationMessageBuilder;
}) {
2026-01-19 00:48:20 +00:00
this.notificationsRepository = notificationsRepository;
2026-01-21 13:51:04 -08:00
this.monitorsRepository = monitorsRepository;
this.providers = providers;
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,
2026-02-16 20:55:00 +00:00
decision: MonitorActionDecision,
2026-02-17 17:28:43 +00:00
notificationMessage: NotificationMessage | undefined
2026-02-16 18:25:35 +00:00
): Promise<boolean> => {
2026-02-17 17:28:43 +00:00
if (!notificationMessage) {
this.logger.warn({
message: "Notification message not provided",
2026-02-16 21:58:58 +00:00
service: SERVICE_NAME,
method: "send",
});
2026-02-17 17:28:43 +00:00
return false;
2026-02-16 21:58:58 +00:00
}
2026-02-17 17:28:43 +00:00
// Route to provider based on notification type
const provider = this.providers[notification.type];
if (!provider) {
this.logger.warn({
message: `Unknown notification type: ${notification.type}`,
service: SERVICE_NAME,
method: "send",
});
return false;
2026-01-19 18:58:17 +00:00
}
return await provider.sendMessage(notification, notificationMessage);
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 20:55:00 +00:00
// Build notification message once for all notifications
const settings = this.settingsService.getSettings();
const clientHost = settings.clientHost || "Host not defined";
const notificationMessage = this.notificationMessageBuilder.buildMessage(monitor, monitorStatusResponse, decision, clientHost);
const tasks = notifications.map((notification) => this.send(notification, monitor, monitorStatusResponse, decision, notificationMessage));
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-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
2026-03-11 13:40:35 -07:00
sendTestNotification = async (notification: Partial<Notification>) => {
const type = notification.type;
if (!type) {
this.logger.warn({
message: "Notification type not provided",
service: SERVICE_NAME,
method: "sendTestNotification",
});
return false;
}
const provider = this.providers[type];
if (!provider) {
this.logger.warn({
message: `Unknown notification type: ${notification.type}`,
service: SERVICE_NAME,
method: "sendTestNotification",
});
return false;
2026-01-19 21:39:02 +00:00
}
return await provider.sendTestAlert(notification);
2026-01-19 21:39:02 +00:00
};
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
2026-03-11 13:28:48 -07:00
createNotification = async (notificationData: Partial<Notification>, userId: string, teamId: string): Promise<Notification> => {
notificationData.userId = userId;
notificationData.teamId = teamId;
2026-01-21 13:51:04 -08:00
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> => {
await this.monitorsRepository.removeNotificationFromMonitors(id);
const deleted = await this.notificationsRepository.deleteById(id, teamId);
// App settings hold notification ids for the egress recovered alert; drop the deleted one so it never dangles.
// After the team-scoped delete, so a request for another team's id (404 above) cannot touch instance settings.
await this.settingsService.removeEgressNotification(id);
2026-01-21 13:51:04 -08:00
return deleted;
};
2026-01-18 06:02:07 +00:00
}