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

44 lines
1.8 KiB
TypeScript
Raw Normal View History

2026-01-19 18:58:17 +00:00
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
2026-01-19 21:39:02 +00:00
import { buildHardwareAlerts, buildHardwareEmail, buildEmail, buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.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 21:58:48 +00:00
constructor(emailService: any, private logger: any) {
2026-01-19 19:30:46 +00:00
this.emailService = emailService;
}
private buildHardwareEmail = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const html = buildHardwareEmail(this.emailService, monitor, alertsToSend);
return html;
};
2026-01-19 18:58:17 +00:00
async sendAlert(notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): Promise<boolean> {
2026-01-19 19:30:46 +00:00
const subject = `Monitor ${monitor.name} infrastructure alerts`;
let html;
if (monitor.type === "hardware") {
html = this.buildHardwareEmail(monitor, monitorStatusResponse);
} else {
html = await buildEmail(this.emailService, monitor);
}
if (!notification.address) {
return false;
}
2026-01-19 21:58:48 +00:00
this.logger?.debug?.("Sending Email alert", { address: notification.address });
2026-01-19 19:30:46 +00:00
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) return false;
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 21:58:48 +00:00
this.logger?.debug?.("Sending Email test alert", { address: notification.address });
2026-01-19 21:39:02 +00:00
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) return false;
return true;
2026-01-19 18:58:17 +00:00
}
}