Defined new schema for the config object.
This commit is contained in:
1 parent
07882e045b
commit
8409571d57
4 files changed
+66
-40
No files matched your search
@@ -1,13 +1,12 @@
|
|||||||
|
|
||||||
class NotificationController {
|
class NotificationController {
|
||||||
constructor(notificationService) {
|
constructor(notificationService) {
|
||||||
this.notificationService = notificationService;
|
this.notificationService = notificationService;
|
||||||
this.triggerNotification = this.triggerNotification.bind(this);
|
this.triggerNotification = this.triggerNotification.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async triggerNotification(req, res, next) {
|
async triggerNotification(req, res, next) {
|
||||||
try {
|
try {
|
||||||
const { monitorId, type, config } = req.body;
|
const { monitorId, type, platform, config } = req.body;
|
||||||
|
|
||||||
const networkResponse = {
|
const networkResponse = {
|
||||||
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
|
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
|
||||||
@@ -15,18 +14,24 @@ class NotificationController {
|
|||||||
statusChanged: true,
|
statusChanged: true,
|
||||||
prevStatus: true,
|
prevStatus: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type === "webhook") {
|
if (type === "webhook") {
|
||||||
|
const notification = {
|
||||||
|
type,
|
||||||
|
platform,
|
||||||
|
config
|
||||||
|
};
|
||||||
|
|
||||||
await this.notificationService.sendWebhookNotification(
|
await this.notificationService.sendWebhookNotification(
|
||||||
networkResponse,
|
networkResponse,
|
||||||
config
|
notification
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notification sent successfully"
|
msg: "Notification sent successfully"
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = "NotificationController";
|
error.service = "NotificationController";
|
||||||
error.method = "triggerNotification";
|
error.method = "triggerNotification";
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
const configSchema = mongoose.Schema({
|
||||||
|
webhookUrl: { type: String },
|
||||||
|
botToken: { type: String },
|
||||||
|
chatId: { type: String }
|
||||||
|
}, { _id: false });
|
||||||
|
|
||||||
const NotificationSchema = mongoose.Schema(
|
const NotificationSchema = mongoose.Schema(
|
||||||
{
|
{
|
||||||
monitorId: {
|
monitorId: {
|
||||||
@@ -11,11 +18,9 @@ const NotificationSchema = mongoose.Schema(
|
|||||||
enum: ["email", "sms", "webhook"],
|
enum: ["email", "sms", "webhook"],
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
type: String,
|
type: configSchema,
|
||||||
webhookUrl: String,
|
default: () => ({})
|
||||||
botToken: String,
|
},
|
||||||
chatId: String
|
|
||||||
},
|
|
||||||
address: {
|
address: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -33,23 +33,24 @@ class NotificationService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendWebhookNotification(networkResponse, config) {
|
async sendWebhookNotification(networkResponse, notification) {
|
||||||
const { monitor, status } = networkResponse;
|
const { monitor, status } = networkResponse;
|
||||||
const { type, webhookUrl, botToken, chatId } = config;
|
const { platform } = notification; // Use platform instead of type for webhook formatting
|
||||||
|
const { webhookUrl, botToken, chatId } = notification.config;
|
||||||
let url = webhookUrl;
|
let url = webhookUrl;
|
||||||
|
|
||||||
const message = this.formatNotificationMessage(monitor, status, type, chatId);
|
const message = this.formatNotificationMessage(monitor, status, platform, chatId);
|
||||||
if (message === null) {
|
if (message === null) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Unsupported webhook type: ${type}`,
|
message: `Unsupported webhook platform: ${platform}`,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
method: 'sendWebhookNotification',
|
method: 'sendWebhookNotification',
|
||||||
type
|
platform
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'telegram') {
|
if (platform === 'telegram') {
|
||||||
if (!botToken || !chatId) {
|
if (!botToken || !chatId) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -57,17 +58,17 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await this.networkService.requestWebhook(type, 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 ${type} notification`,
|
message: `Error sending ${platform} notification`,
|
||||||
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,
|
||||||
type,
|
platform,
|
||||||
requestPayload: message
|
requestPayload: message
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
@@ -115,7 +116,7 @@ class NotificationService {
|
|||||||
|
|
||||||
async handleStatusNotifications(networkResponse) {
|
async handleStatusNotifications(networkResponse) {
|
||||||
try {
|
try {
|
||||||
//If status hasn't changed, we're done
|
// If status hasn't changed, we're done
|
||||||
if (networkResponse.statusChanged === false) return false;
|
if (networkResponse.statusChanged === false) return false;
|
||||||
// if prevStatus is undefined, monitor is resuming, we're done
|
// if prevStatus is undefined, monitor is resuming, we're done
|
||||||
if (networkResponse.prevStatus === undefined) return false;
|
if (networkResponse.prevStatus === undefined) return false;
|
||||||
@@ -124,13 +125,12 @@ class NotificationService {
|
|||||||
|
|
||||||
for (const notification of notifications) {
|
for (const notification of notifications) {
|
||||||
if (notification.type === "email") {
|
if (notification.type === "email") {
|
||||||
this.sendEmail(networkResponse, notification.address);
|
await this.sendEmail(networkResponse, notification.address);
|
||||||
} else if (notification.type === "webhook") {
|
} else if (notification.type === "webhook") {
|
||||||
this.sendWebhookNotification(networkResponse, notification.address, notification.type);
|
await this.sendWebhookNotification(networkResponse, notification);
|
||||||
|
}
|
||||||
// Handle other types of notifications here
|
// Handle other types of notifications here
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
|
|||||||
+30
-14
@@ -476,7 +476,7 @@ const imageValidation = joi
|
|||||||
});
|
});
|
||||||
|
|
||||||
const telegramWebhookConfigValidation = joi.object({
|
const telegramWebhookConfigValidation = joi.object({
|
||||||
type: joi.string().valid('telegram').required(),
|
webhookUrl: joi.string().uri().optional(),
|
||||||
botToken: joi.string().required().messages({
|
botToken: joi.string().required().messages({
|
||||||
'string.empty': 'Telegram bot token is required',
|
'string.empty': 'Telegram bot token is required',
|
||||||
'any.required': 'Telegram bot token is required'
|
'any.required': 'Telegram bot token is required'
|
||||||
@@ -488,21 +488,23 @@ const imageValidation = joi
|
|||||||
});
|
});
|
||||||
|
|
||||||
const discordWebhookConfigValidation = joi.object({
|
const discordWebhookConfigValidation = joi.object({
|
||||||
type: joi.string().valid('discord').required(),
|
|
||||||
webhookUrl: joi.string().uri().required().messages({
|
webhookUrl: joi.string().uri().required().messages({
|
||||||
'string.empty': 'Discord webhook URL is required',
|
'string.empty': 'Discord webhook URL is required',
|
||||||
'string.uri': 'Discord webhook URL must be a valid URL',
|
'string.uri': 'Discord webhook URL must be a valid URL',
|
||||||
'any.required': 'Discord webhook URL is required'
|
'any.required': 'Discord webhook URL is required'
|
||||||
})
|
}),
|
||||||
|
botToken: joi.string().optional(),
|
||||||
|
chatId: joi.string().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
const slackWebhookConfigValidation = joi.object({
|
const slackWebhookConfigValidation = joi.object({
|
||||||
type: joi.string().valid('slack').required(),
|
|
||||||
webhookUrl: joi.string().uri().required().messages({
|
webhookUrl: joi.string().uri().required().messages({
|
||||||
'string.empty': 'Slack webhook URL is required',
|
'string.empty': 'Slack webhook URL is required',
|
||||||
'string.uri': 'Slack webhook URL must be a valid URL',
|
'string.uri': 'Slack webhook URL must be a valid URL',
|
||||||
'any.required': 'Slack webhook URL is required'
|
'any.required': 'Slack webhook URL is required'
|
||||||
})
|
}),
|
||||||
|
botToken: joi.string().optional(),
|
||||||
|
chatId: joi.string().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
const triggerNotificationBodyValidation = joi.object({
|
const triggerNotificationBodyValidation = joi.object({
|
||||||
@@ -515,16 +517,30 @@ const imageValidation = joi
|
|||||||
'any.required': 'Notification type is required',
|
'any.required': 'Notification type is required',
|
||||||
'any.only': 'Notification type must be webhook'
|
'any.only': 'Notification type must be webhook'
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
platform: joi.string().valid('telegram', 'discord', 'slack').required().messages({
|
||||||
|
'string.empty': 'Platform type is required',
|
||||||
|
'any.required': 'Platform type is required',
|
||||||
|
'any.only': 'Platform must be telegram, discord, or slack'
|
||||||
|
}),
|
||||||
config: joi.alternatives()
|
config: joi.alternatives()
|
||||||
.conditional('type', {
|
.conditional('platform', [
|
||||||
is: 'webhook',
|
{
|
||||||
then: joi.alternatives().try(
|
is: 'telegram',
|
||||||
telegramWebhookConfigValidation,
|
then: telegramWebhookConfigValidation
|
||||||
discordWebhookConfigValidation,
|
},
|
||||||
slackWebhookConfigValidation
|
{
|
||||||
).required().messages({
|
is: 'discord',
|
||||||
'any.required': 'Webhook configuration is required'
|
then: discordWebhookConfigValidation
|
||||||
})
|
},
|
||||||
|
{
|
||||||
|
is: 'slack',
|
||||||
|
then: slackWebhookConfigValidation
|
||||||
|
}
|
||||||
|
])
|
||||||
|
.required()
|
||||||
|
.messages({
|
||||||
|
'any.required': 'Webhook configuration is required'
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user