This commit is contained in:
Alex Holliday committed 2026-03-04 17:32:50 +00:00
1 parent 1deb22bf63
commit afc27c804d
5 files changed
+50 -33

No files matched your search

@@ -7,7 +7,13 @@ const __dirname = path.dirname(__filename);
const SERVICE_NAME = "EmailService"; const SERVICE_NAME = "EmailService";
class EmailService { export interface IEmailService {
init(): void;
buildEmail(template: string, context: Record<string, unknown>): Promise<string | undefined>;
sendEmail(to: string, subject: string, html: string, transportConfig?: EmailTransportConfig): Promise<string | false | undefined>;
}
class EmailService implements IEmailService {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private settingsService: any; private settingsService: any;
@@ -1,15 +1,15 @@
const SERVICE_NAME = "EmailProvider"; const SERVICE_NAME = "EmailProvider";
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { INotificationProvider } from "@/service/index.js";
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
import { buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js"; import { buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import type { ILogger } from "@/utils/logger.js";
import { IEmailService } from "@/service/infrastructure/emailService.js";
export class EmailProvider implements INotificationProvider { export class EmailProvider implements INotificationProvider {
private emailService: any; private emailService: IEmailService;
private logger: any; private logger: ILogger;
constructor(emailService: any, logger: any) { constructor(emailService: IEmailService, logger: ILogger) {
this.emailService = emailService; this.emailService = emailService;
this.logger = logger; this.logger = logger;
} }
@@ -18,6 +18,24 @@ export class EmailProvider implements INotificationProvider {
const subject = "Test notification"; const subject = "Test notification";
const html = await buildTestEmail(this.emailService); const html = await buildTestEmail(this.emailService);
if (!notification.address) {
this.logger.warn({
message: "Missing address",
service: SERVICE_NAME,
method: "sendTestAlert",
});
return false;
}
if (!html) {
this.logger.warn({
message: "Failed to build test email content",
service: SERVICE_NAME,
method: "sendTestAlert",
});
return false;
}
const messageId = await this.emailService.sendEmail(notification.address, subject, html); const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) { if (!messageId) {
this.logger.warn({ this.logger.warn({
@@ -38,6 +56,15 @@ export class EmailProvider implements INotificationProvider {
const subject = this.buildSubject(message); const subject = this.buildSubject(message);
const html = await this.buildEmailFromMessage(message); const html = await this.buildEmailFromMessage(message);
if (!html) {
this.logger.warn({
message: "Failed to build email content",
service: SERVICE_NAME,
method: "sendMessage",
});
return false;
}
const messageId = await this.emailService.sendEmail(notification.address, subject, html); const messageId = await this.emailService.sendEmail(notification.address, subject, html);
if (!messageId) { if (!messageId) {
this.logger.warn({ this.logger.warn({
@@ -65,7 +92,7 @@ export class EmailProvider implements INotificationProvider {
} }
} }
private async buildEmailFromMessage(message: NotificationMessage): Promise<string> { private async buildEmailFromMessage(message: NotificationMessage): Promise<string | undefined> {
const context = { const context = {
title: message.content.title, title: message.content.title,
summary: message.content.summary, summary: message.content.summary,
@@ -88,16 +115,6 @@ export class EmailProvider implements INotificationProvider {
const html = 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; return html;
} }
@@ -1,14 +1,15 @@
const SERVICE_NAME = "MatrixProvider"; const SERVICE_NAME = "MatrixProvider";
import got from "got"; import got from "got";
import type { INotificationProvider } from "@/service/index.js"; import type { INotificationProvider } from "@/service/index.js";
import type { Notification } from "@/types/index.js"; import type { AlertMatrixPayload, Notification } from "@/types/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import { ILogger } from "@/utils/logger.js";
export class MatrixProvider implements INotificationProvider { export class MatrixProvider implements INotificationProvider {
private logger: any; private logger: ILogger;
constructor(logger: any) { constructor(logger: ILogger) {
this.logger = logger; this.logger = logger;
} }
@@ -42,9 +43,6 @@ export class MatrixProvider implements INotificationProvider {
} }
}; };
/**
* New unified message format - builds Matrix HTML message from NotificationMessage
*/
sendMessage = async (notification: Notification, message: NotificationMessage): Promise<boolean> => { sendMessage = async (notification: Notification, message: NotificationMessage): Promise<boolean> => {
const { homeserverUrl, accessToken, roomId } = notification; const { homeserverUrl, accessToken, roomId } = notification;
@@ -87,7 +85,7 @@ export class MatrixProvider implements INotificationProvider {
* Returns both plain text and HTML formatted versions * Returns both plain text and HTML formatted versions
* Matrix supports HTML subset for rich formatting * Matrix supports HTML subset for rich formatting
*/ */
private buildMatrixMessage(message: NotificationMessage): { plainText: string; htmlText: string } { private buildMatrixMessage(message: NotificationMessage): AlertMatrixPayload {
const plainLines: string[] = []; const plainLines: string[] = [];
const htmlLines: string[] = []; const htmlLines: string[] = [];
@@ -1,6 +1,5 @@
import EmailService from "@/service/infrastructure/emailService.js"; import { IEmailService } from "@/service/infrastructure/emailService.js";
// Test notification helpers - used by all providers for test alerts export const buildTestEmail = async (emailService: IEmailService) => {
export const buildTestEmail = async (emailService: EmailService) => {
const context = { testName: "Monitoring System" }; const context = { testName: "Monitoring System" };
const html = await emailService.buildEmail("testEmailTemplate", context); const html = await emailService.buildEmail("testEmailTemplate", context);
return html; return html;
+2 -5
View File
@@ -11,11 +11,8 @@ export interface AlertPagerDutyPayload {
} }
export interface AlertMatrixPayload { export interface AlertMatrixPayload {
friendlyName?: string; plainText: string;
homeserverUrl: string; htmlText: string;
accessToken: string;
roomId: string;
monitorName: string;
} }
export interface Alert { export interface Alert {