add a provider registry to notificaiton service, replace positional args and switch statements
This commit is contained in:
1 parent
bf255a0178
commit
5d3dc03c89
1 file changed
+50
-96
@@ -9,6 +9,10 @@ import type { MonitorActionDecision } from "@/worker/worker.helper.js";
|
|||||||
import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
|
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
|
||||||
|
import type { NotificationChannel } from "@/domain/notifications/notification.type.js";
|
||||||
|
import { no } from "zod/v4/locales";
|
||||||
|
|
||||||
|
export type NotificationProviderRegistry = Record<NotificationChannel, INotificationProvider>;
|
||||||
|
|
||||||
export interface INotificationsService {
|
export interface INotificationsService {
|
||||||
createNotification: (notificationData: Partial<Notification>, userId: string, teamId: string) => Promise<Notification>;
|
createNotification: (notificationData: Partial<Notification>, userId: string, teamId: string) => Promise<Notification>;
|
||||||
@@ -29,52 +33,29 @@ export class NotificationsService implements INotificationsService {
|
|||||||
|
|
||||||
private notificationsRepository: INotificationsRepository;
|
private notificationsRepository: INotificationsRepository;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private webhookProvider: INotificationProvider;
|
private providers: NotificationProviderRegistry;
|
||||||
private emailProvider: INotificationProvider;
|
|
||||||
private slackProvider: INotificationProvider;
|
|
||||||
private discordProvider: INotificationProvider;
|
|
||||||
private pagerDutyProvider: INotificationProvider;
|
|
||||||
private matrixProvider: INotificationProvider;
|
|
||||||
private teamsProvider: INotificationProvider;
|
|
||||||
private telegramProvider: INotificationProvider;
|
|
||||||
private pushoverProvider: INotificationProvider;
|
|
||||||
private twilioProvider: INotificationProvider;
|
|
||||||
private ntfyProvider: INotificationProvider;
|
|
||||||
private logger: ILogger;
|
|
||||||
private settingsService: ISettingsService;
|
private settingsService: ISettingsService;
|
||||||
|
private logger: ILogger;
|
||||||
private notificationMessageBuilder: INotificationMessageBuilder;
|
private notificationMessageBuilder: INotificationMessageBuilder;
|
||||||
|
|
||||||
constructor(
|
constructor({
|
||||||
notificationsRepository: INotificationsRepository,
|
notificationsRepository,
|
||||||
monitorsRepository: IMonitorsRepository,
|
monitorsRepository,
|
||||||
webhookProvider: INotificationProvider,
|
providers,
|
||||||
emailProvider: INotificationProvider,
|
settingsService,
|
||||||
slackProvider: INotificationProvider,
|
logger,
|
||||||
discordProvider: INotificationProvider,
|
notificationMessageBuilder,
|
||||||
pagerDutyProvider: INotificationProvider,
|
}: {
|
||||||
matrixProvider: INotificationProvider,
|
notificationsRepository: INotificationsRepository;
|
||||||
teamsProvider: INotificationProvider,
|
monitorsRepository: IMonitorsRepository;
|
||||||
telegramProvider: INotificationProvider,
|
providers: NotificationProviderRegistry;
|
||||||
pushoverProvider: INotificationProvider,
|
settingsService: ISettingsService;
|
||||||
twilioProvider: INotificationProvider,
|
logger: ILogger;
|
||||||
ntfyProvider: INotificationProvider,
|
notificationMessageBuilder: INotificationMessageBuilder;
|
||||||
settingsService: ISettingsService,
|
}) {
|
||||||
logger: ILogger,
|
|
||||||
notificationMessageBuilder: INotificationMessageBuilder
|
|
||||||
) {
|
|
||||||
this.notificationsRepository = notificationsRepository;
|
this.notificationsRepository = notificationsRepository;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
this.webhookProvider = webhookProvider;
|
this.providers = providers;
|
||||||
this.emailProvider = emailProvider;
|
|
||||||
this.slackProvider = slackProvider;
|
|
||||||
this.discordProvider = discordProvider;
|
|
||||||
this.pagerDutyProvider = pagerDutyProvider;
|
|
||||||
this.matrixProvider = matrixProvider;
|
|
||||||
this.teamsProvider = teamsProvider;
|
|
||||||
this.telegramProvider = telegramProvider;
|
|
||||||
this.pushoverProvider = pushoverProvider;
|
|
||||||
this.twilioProvider = twilioProvider;
|
|
||||||
this.ntfyProvider = ntfyProvider;
|
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.notificationMessageBuilder = notificationMessageBuilder;
|
this.notificationMessageBuilder = notificationMessageBuilder;
|
||||||
@@ -97,37 +78,16 @@ export class NotificationsService implements INotificationsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Route to provider based on notification type
|
// Route to provider based on notification type
|
||||||
switch (notification.type) {
|
const provider = this.providers[notification.type];
|
||||||
case "webhook":
|
if (!provider) {
|
||||||
return await this.webhookProvider.sendMessage!(notification, notificationMessage);
|
this.logger.warn({
|
||||||
case "slack":
|
message: `Unknown notification type: ${notification.type}`,
|
||||||
return await this.slackProvider.sendMessage!(notification, notificationMessage);
|
service: SERVICE_NAME,
|
||||||
case "matrix":
|
method: "send",
|
||||||
return await this.matrixProvider.sendMessage!(notification, notificationMessage);
|
});
|
||||||
case "pager_duty":
|
return false;
|
||||||
return await this.pagerDutyProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "discord":
|
|
||||||
return await this.discordProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "email":
|
|
||||||
return await this.emailProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "teams":
|
|
||||||
return await this.teamsProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "telegram":
|
|
||||||
return await this.telegramProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "pushover":
|
|
||||||
return await this.pushoverProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "twilio":
|
|
||||||
return await this.twilioProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
case "ntfy":
|
|
||||||
return await this.ntfyProvider.sendMessage!(notification, notificationMessage);
|
|
||||||
default:
|
|
||||||
this.logger.warn({
|
|
||||||
message: `Unknown notification type: ${notification.type}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "send",
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return await provider.sendMessage(notification, notificationMessage);
|
||||||
};
|
};
|
||||||
|
|
||||||
private sendNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => {
|
private sendNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => {
|
||||||
@@ -165,32 +125,26 @@ export class NotificationsService implements INotificationsService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
sendTestNotification = async (notification: Partial<Notification>) => {
|
sendTestNotification = async (notification: Partial<Notification>) => {
|
||||||
switch (notification.type) {
|
const type = notification.type;
|
||||||
case "email":
|
if (!type) {
|
||||||
return await this.emailProvider.sendTestAlert(notification);
|
this.logger.warn({
|
||||||
case "slack":
|
message: "Notification type not provided",
|
||||||
return await this.slackProvider.sendTestAlert(notification);
|
service: SERVICE_NAME,
|
||||||
case "discord":
|
method: "sendTestNotification",
|
||||||
return await this.discordProvider.sendTestAlert(notification);
|
});
|
||||||
case "pager_duty":
|
return false;
|
||||||
return await this.pagerDutyProvider.sendTestAlert(notification);
|
|
||||||
case "matrix":
|
|
||||||
return await this.matrixProvider.sendTestAlert(notification);
|
|
||||||
case "webhook":
|
|
||||||
return await this.webhookProvider.sendTestAlert(notification);
|
|
||||||
case "teams":
|
|
||||||
return await this.teamsProvider.sendTestAlert(notification);
|
|
||||||
case "telegram":
|
|
||||||
return await this.telegramProvider.sendTestAlert(notification);
|
|
||||||
case "pushover":
|
|
||||||
return await this.pushoverProvider.sendTestAlert(notification);
|
|
||||||
case "twilio":
|
|
||||||
return await this.twilioProvider.sendTestAlert(notification);
|
|
||||||
case "ntfy":
|
|
||||||
return await this.ntfyProvider.sendTestAlert(notification);
|
|
||||||
default:
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
return await provider.sendTestAlert(notification);
|
||||||
};
|
};
|
||||||
|
|
||||||
testAllNotifications = async (notificationIds: string[]) => {
|
testAllNotifications = async (notificationIds: string[]) => {
|
||||||
|
|||||||
Reference in new issue
Block a user