Files
Checkmate/server/src/service/infrastructure/notificationProviders/email.ts
T

176 lines
5.5 KiB
TypeScript
Raw Normal View History

2026-01-19 22:16:33 +00:00
const SERVICE_NAME = "EmailProvider";
2026-01-19 18:58:17 +00:00
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
2026-02-15 18:35:54 +00:00
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
2026-01-19 21:39:02 +00:00
import { buildHardwareAlerts, buildHardwareEmail, buildEmail, buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js";
2026-02-17 01:47:57 +00:00
import type { NotificationMessage } from "@/types/notificationMessage.js";
2026-01-19 18:58:17 +00:00
export class EmailProvider implements INotificationProvider {
2026-01-19 19:30:46 +00:00
private emailService: any;
2026-01-19 22:16:33 +00:00
private logger: any;
constructor(emailService: any, logger: any) {
2026-01-19 19:30:46 +00:00
this.emailService = emailService;
2026-01-19 22:16:33 +00:00
this.logger = logger;
2026-01-19 19:30:46 +00:00
}
private buildHardwareEmail = async (
clientHost: string,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision
) => {
2026-02-15 18:35:54 +00:00
// For status changes (recovery), use standard email format
if (decision.notificationReason === "status_change") {
return await buildEmail(this.emailService, monitor);
}
// For threshold breaches, use hardware alert format
const { alertsToSend } = buildHardwareAlerts(clientHost, monitor, monitorStatusResponse);
2026-01-19 19:30:46 +00:00
const html = buildHardwareEmail(this.emailService, monitor, alertsToSend);
return html;
};
2026-02-15 18:35:54 +00:00
async sendAlert(
notification: Notification,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision,
clientHost: string
2026-02-15 18:35:54 +00:00
): Promise<boolean> {
// For grouped notifications (identified by ":" in name), customize subject to indicate multiple services.
// Example: "2 services: Service A, Service B" becomes "Alert: 2 services are down"
const isGroupedNotification = monitor.name.includes(":");
2026-02-15 18:35:54 +00:00
// Build subject based on notification reason and monitor status
let subject: string;
if (isGroupedNotification) {
subject = `Alert: ${monitor.name} are down`;
} else if (decision.notificationReason === "threshold_breach") {
subject = `Monitor ${monitor.name} threshold breached`;
} else if (monitor.status === "up") {
subject = `Monitor ${monitor.name} is back up`;
} else {
subject = `Monitor ${monitor.name} is down`;
}
2026-01-19 19:30:46 +00:00
let html;
if (monitor.type === "hardware") {
html = await this.buildHardwareEmail(clientHost, monitor, monitorStatusResponse, decision);
2026-01-19 19:30:46 +00:00
} else {
html = await buildEmail(this.emailService, monitor);
}
if (!notification.address) {
return false;
}
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
2026-01-19 22:16:33 +00:00
if (!messageId) {
this.logger.warn({
message: "Email alert failed",
service: SERVICE_NAME,
method: "sendAlert",
});
return false;
}
2026-01-19 19:30:46 +00:00
return true;
2026-01-19 18:58:17 +00:00
}
async sendTestAlert(notification: Notification): Promise<boolean> {
2026-01-19 21:39:02 +00:00
const subject = "Test notification";
const html = await buildTestEmail(this.emailService);
2026-01-19 22:16:33 +00:00
2026-01-19 21:39:02 +00:00
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
2026-01-19 22:16:33 +00:00
if (!messageId) {
this.logger.warn({
message: "Email test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
});
return false;
}
2026-01-19 21:39:02 +00:00
return true;
2026-01-19 18:58:17 +00:00
}
2026-02-17 01:47:57 +00:00
async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> {
if (!notification.address) {
return false;
}
const subject = this.buildSubject(message);
const html = await this.buildEmailFromMessage(message);
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) {
this.logger.warn({
message: "Email notification failed via sendMessage",
service: SERVICE_NAME,
method: "sendMessage",
});
return false;
}
return true;
}
private buildSubject(message: NotificationMessage): string {
switch (message.type) {
case "monitor_down":
return `Monitor ${message.monitor.name} is down`;
case "monitor_up":
return `Monitor ${message.monitor.name} is back up`;
case "threshold_breach":
return `Monitor ${message.monitor.name} threshold breached`;
case "threshold_resolved":
return `Monitor ${message.monitor.name} thresholds resolved`;
default:
return `Alert: ${message.monitor.name}`;
}
}
private async buildEmailFromMessage(message: NotificationMessage): Promise<string> {
const context = {
title: message.content.title,
summary: message.content.summary,
monitorName: message.monitor.name,
monitorUrl: message.monitor.url,
monitorType: message.monitor.type,
monitorStatus: message.monitor.status,
headerColor: this.getColorForSeverity(message.severity),
thresholds: message.content.thresholds,
details: message.content.details,
incidentUrl: message.content.incident?.url,
};
this.logger.info({
message: "[DEBUG] Building email from message",
service: SERVICE_NAME,
method: "buildEmailFromMessage",
details: { context },
});
return await this.emailService.buildEmail("unifiedNotificationTemplate", context);
const html = await this.emailService.buildEmail("unifiedNotificationTemplate", context);
this.logger.info({
message: "[DEBUG] Email HTML generated",
service: SERVICE_NAME,
method: "buildEmailFromMessage",
details: {
htmlLength: html?.length || 0,
htmlPreview: html?.substring(0, 200),
},
});
return html;
}
private getColorForSeverity(severity: string): string {
const colorMap: Record<string, string> = {
critical: "red",
warning: "#f59e0b",
info: "#3b82f6",
success: "green",
};
return colorMap[severity] ?? "#3b82f6";
}
2026-01-19 18:58:17 +00:00
}