Use the localization service for user facing strings.

This commit is contained in:
Skorpios committed 2025-02-17 09:12:11 -08:00
1 parent e4e3f9cc63
commit f22b67ad0a
4 files changed
+90 -53

No files matched your search

+2 -1
View File
@@ -9,8 +9,9 @@ import { handleError, handleValidationError } from './controllerUtils.js';
const SERVICE_NAME = "NotificationController"; const SERVICE_NAME = "NotificationController";
class NotificationController { class NotificationController {
constructor(notificationService) { constructor(notificationService, stringService) {
this.notificationService = notificationService; this.notificationService = notificationService;
this.stringService = stringService;
this.triggerNotification = this.triggerNotification.bind(this); this.triggerNotification = this.triggerNotification.bind(this);
} }
+3 -2
View File
@@ -187,7 +187,7 @@ const startApp = async () => {
logger logger
); );
const statusService = new StatusService(db, logger); const statusService = new StatusService(db, logger);
const notificationService = new NotificationService(emailService, db, logger, networkService); const notificationService = new NotificationService(emailService, db, logger, networkService, stringService);
const jobQueue = new JobQueue( const jobQueue = new JobQueue(
@@ -274,7 +274,8 @@ const startApp = async () => {
); );
const notificationController = new NotificationController( const notificationController = new NotificationController(
ServiceRegistry.get(NotificationService.SERVICE_NAME) ServiceRegistry.get(NotificationService.SERVICE_NAME),
ServiceRegistry.get(StringService.SERVICE_NAME)
); );
const distributedUptimeController = new DistributedUptimeController( const distributedUptimeController = new DistributedUptimeController(
+55 -50
View File
@@ -11,12 +11,13 @@ class NotificationService {
* @param {Object} logger - The logger instance for logging activities. * @param {Object} logger - The logger instance for logging activities.
* @param {Object} networkService - The network service for sending webhook notifications. * @param {Object} networkService - The network service for sending webhook notifications.
*/ */
constructor(emailService, db, logger, networkService) { constructor(emailService, db, logger, networkService, stringService) {
this.SERVICE_NAME = SERVICE_NAME; this.SERVICE_NAME = SERVICE_NAME;
this.emailService = emailService; this.emailService = emailService;
this.db = db; this.db = db;
this.logger = logger; this.logger = logger;
this.networkService = networkService; this.networkService = networkService;
this.stringService = stringService;
} }
/** /**
@@ -32,19 +33,23 @@ class NotificationService {
*/ */
formatNotificationMessage(monitor, status, platform, chatId) { formatNotificationMessage(monitor, status, platform, chatId) {
const messageText = `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`; const messageText = this.stringService.getMonitorStatus(
monitor.name,
status,
monitor.url
);
if (platform === 'telegram') { if (platform === 'telegram') {
return { chat_id: chatId, text: messageText }; return { chat_id: chatId, text: messageText };
} }
if (platform === 'slack') { if (platform === 'slack') {
return { text: messageText }; return { text: messageText };
} }
if (platform === 'discord') { if (platform === 'discord') {
return { content: messageText }; return { content: messageText };
} }
return null; return null;
} }
/** /**
* Sends a webhook notification to a specified platform. * Sends a webhook notification to a specified platform.
@@ -62,46 +67,46 @@ class NotificationService {
*/ */
async sendWebhookNotification(networkResponse, notification) { async sendWebhookNotification(networkResponse, notification) {
const { monitor, status } = networkResponse; const { monitor, status } = networkResponse;
const { platform } = notification; const { platform } = notification;
const { webhookUrl, botToken, chatId } = notification.config; const { webhookUrl, botToken, chatId } = notification.config;
let url = webhookUrl; let url = webhookUrl;
const message = this.formatNotificationMessage(monitor, status, platform, chatId); const message = this.formatNotificationMessage(monitor, status, platform, chatId);
if (message === null) { if (message === null) {
this.logger.warn({ this.logger.warn({
message: `Unsupported webhook platform: ${platform}`, message: this.stringService.getWebhookUnsupportedPlatform(platform),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: 'sendWebhookNotification', method: 'sendWebhookNotification',
platform platform
}); });
return false; return false;
} }
if (platform === 'telegram') { if (platform === 'telegram') {
if (!botToken || !chatId) { if (!botToken || !chatId) {
return false; return false;
} }
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
} }
try { try {
const response = await this.networkService.requestWebhook(platform, url, message); const response = await this.networkService.requestWebhook(platform, url, message);
return response.status; return response.status;
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
message: `Error sending ${platform} notification`, message: this.stringService.getWebhookSendError(platform),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: 'sendWebhookNotification', method: 'sendWebhookNotification',
error: error.message, error: error.message,
stack: error.stack, stack: error.stack,
url, url,
platform, platform,
requestPayload: message requestPayload: message
}); });
return false; return false;
} }
} }
/** /**
* Sends an email notification for hardware infrastructure alerts * Sends an email notification for hardware infrastructure alerts
+30
View File
@@ -165,6 +165,36 @@ class StringService {
return this.translationService.getTranslation('maintenanceWindowEdit'); return this.translationService.getTranslation('maintenanceWindowEdit');
} }
// Webhook Messages
get webhookUnsupportedPlatform() {
return this.translationService.getTranslation('webhookUnsupportedPlatform');
}
get webhookSendError() {
return this.translationService.getTranslation('webhookSendError');
}
get webhookSendSuccess() {
return this.translationService.getTranslation('webhookSendSuccess');
}
getWebhookUnsupportedPlatform(platform) {
return this.translationService.getTranslation('webhookUnsupportedPlatform')
.replace('{platform}', platform);
}
getWebhookSendError(platform) {
return this.translationService.getTranslation('webhookSendError')
.replace('{platform}', platform);
}
getMonitorStatus(name, status, url) {
return this.translationService.getTranslation('monitorStatus')
.replace('{name}', name)
.replace('{status}', status ? "up" : "down")
.replace('{url}', url);
}
// Error Messages // Error Messages
get unknownError() { get unknownError() {
return this.translationService.getTranslation('unknownError'); return this.translationService.getTranslation('unknownError');