Use the localization service for user facing strings.
This commit is contained in:
1 parent
e4e3f9cc63
commit
f22b67ad0a
4 files changed
+94
-57
No files matched your search
@@ -9,8 +9,9 @@ import { handleError, handleValidationError } from './controllerUtils.js';
|
||||
const SERVICE_NAME = "NotificationController";
|
||||
|
||||
class NotificationController {
|
||||
constructor(notificationService) {
|
||||
constructor(notificationService, stringService) {
|
||||
this.notificationService = notificationService;
|
||||
this.stringService = stringService;
|
||||
this.triggerNotification = this.triggerNotification.bind(this);
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -187,7 +187,7 @@ const startApp = async () => {
|
||||
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(
|
||||
@@ -274,7 +274,8 @@ const startApp = async () => {
|
||||
);
|
||||
|
||||
const notificationController = new NotificationController(
|
||||
ServiceRegistry.get(NotificationService.SERVICE_NAME)
|
||||
ServiceRegistry.get(NotificationService.SERVICE_NAME),
|
||||
ServiceRegistry.get(StringService.SERVICE_NAME)
|
||||
);
|
||||
|
||||
const distributedUptimeController = new DistributedUptimeController(
|
||||
|
||||
@@ -11,12 +11,13 @@ class NotificationService {
|
||||
* @param {Object} logger - The logger instance for logging activities.
|
||||
* @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.emailService = emailService;
|
||||
this.db = db;
|
||||
this.logger = logger;
|
||||
this.networkService = networkService;
|
||||
this.stringService = stringService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,19 +33,23 @@ class NotificationService {
|
||||
*/
|
||||
|
||||
formatNotificationMessage(monitor, status, platform, chatId) {
|
||||
const messageText = `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`;
|
||||
|
||||
if (platform === 'telegram') {
|
||||
return { chat_id: chatId, text: messageText };
|
||||
}
|
||||
if (platform === 'slack') {
|
||||
return { text: messageText };
|
||||
}
|
||||
if (platform === 'discord') {
|
||||
return { content: messageText };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const messageText = this.stringService.getMonitorStatus(
|
||||
monitor.name,
|
||||
status,
|
||||
monitor.url
|
||||
);
|
||||
|
||||
if (platform === 'telegram') {
|
||||
return { chat_id: chatId, text: messageText };
|
||||
}
|
||||
if (platform === 'slack') {
|
||||
return { text: messageText };
|
||||
}
|
||||
if (platform === 'discord') {
|
||||
return { content: messageText };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a webhook notification to a specified platform.
|
||||
@@ -62,46 +67,46 @@ class NotificationService {
|
||||
*/
|
||||
|
||||
async sendWebhookNotification(networkResponse, notification) {
|
||||
const { monitor, status } = networkResponse;
|
||||
const { platform } = notification;
|
||||
const { webhookUrl, botToken, chatId } = notification.config;
|
||||
let url = webhookUrl;
|
||||
|
||||
const message = this.formatNotificationMessage(monitor, status, platform, chatId);
|
||||
if (message === null) {
|
||||
this.logger.warn({
|
||||
message: `Unsupported webhook platform: ${platform}`,
|
||||
service: this.SERVICE_NAME,
|
||||
method: 'sendWebhookNotification',
|
||||
platform
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (platform === 'telegram') {
|
||||
if (!botToken || !chatId) {
|
||||
return false;
|
||||
}
|
||||
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||
return response.status;
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
message: `Error sending ${platform} notification`,
|
||||
service: this.SERVICE_NAME,
|
||||
method: 'sendWebhookNotification',
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
url,
|
||||
platform,
|
||||
requestPayload: message
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const { monitor, status } = networkResponse;
|
||||
const { platform } = notification;
|
||||
const { webhookUrl, botToken, chatId } = notification.config;
|
||||
let url = webhookUrl;
|
||||
|
||||
const message = this.formatNotificationMessage(monitor, status, platform, chatId);
|
||||
if (message === null) {
|
||||
this.logger.warn({
|
||||
message: this.stringService.getWebhookUnsupportedPlatform(platform),
|
||||
service: this.SERVICE_NAME,
|
||||
method: 'sendWebhookNotification',
|
||||
platform
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (platform === 'telegram') {
|
||||
if (!botToken || !chatId) {
|
||||
return false;
|
||||
}
|
||||
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||
return response.status;
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
message: this.stringService.getWebhookSendError(platform),
|
||||
service: this.SERVICE_NAME,
|
||||
method: 'sendWebhookNotification',
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
url,
|
||||
platform,
|
||||
requestPayload: message
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email notification for hardware infrastructure alerts
|
||||
|
||||
@@ -165,6 +165,36 @@ class StringService {
|
||||
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
|
||||
get unknownError() {
|
||||
return this.translationService.getTranslation('unknownError');
|
||||
|
||||
Reference in new issue
Block a user