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
+44 -7

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(
+9 -4
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,7 +33,11 @@ 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 };
@@ -70,7 +75,7 @@ class NotificationService {
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
@@ -90,7 +95,7 @@ class NotificationService {
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,
+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');