This commit is contained in:
Alex Holliday committed 2026-02-17 17:28:43 +00:00
1 parent fbb0e940c6
commit 232be7a405
13 files changed
+38 -855

No files matched your search

@@ -2,7 +2,7 @@ const SERVICE_NAME = "EmailProvider";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
import { buildHardwareAlerts, buildHardwareEmail, buildEmail, buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js";
import { buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js";
import type { NotificationMessage } from "@/types/notificationMessage.js";
export class EmailProvider implements INotificationProvider {
@@ -14,68 +14,6 @@ export class EmailProvider implements INotificationProvider {
this.logger = logger;
}
private buildHardwareEmail = async (
clientHost: string,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision
) => {
// 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);
const html = buildHardwareEmail(this.emailService, monitor, alertsToSend);
return html;
};
async sendAlert(
notification: Notification,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision,
clientHost: string
): 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(":");
// 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`;
}
let html;
if (monitor.type === "hardware") {
html = await this.buildHardwareEmail(clientHost, monitor, monitorStatusResponse, decision);
} else {
html = await buildEmail(this.emailService, monitor);
}
if (!notification.address) {
return false;
}
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) {
this.logger.warn({
message: "Email alert failed",
service: SERVICE_NAME,
method: "sendAlert",
});
return false;
}
return true;
}
async sendTestAlert(notification: Notification): Promise<boolean> {
const subject = "Test notification";
const html = await buildTestEmail(this.emailService);
@@ -103,7 +41,7 @@ export class EmailProvider implements INotificationProvider {
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) {
this.logger.warn({
message: "Email notification failed via sendMessage",
message: "Email notification failed",
service: SERVICE_NAME,
method: "sendMessage",
});
@@ -148,7 +86,6 @@ export class EmailProvider implements INotificationProvider {
details: { context },
});
return await this.emailService.buildEmail("unifiedNotificationTemplate", context);
const html = await this.emailService.buildEmail("unifiedNotificationTemplate", context);
this.logger.info({