Refactored notification schema config object.
This commit is contained in:
1 parent
d8379f7e88
commit
f46b0768fc
3 files changed
+37
-50
No files matched your search
@@ -23,34 +23,34 @@ class NotificationController {
|
|||||||
prevStatus: true,
|
prevStatus: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type === "telegram") {
|
if (type === "webhook") {
|
||||||
if (!config?.botToken || !config?.chatId) {
|
if (!config?.type) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: "botToken and chatId are required for Telegram notifications"
|
msg: "webhook type is required in config"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.type === "telegram") {
|
||||||
|
if (!config.botToken || !config.chatId) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
msg: "botToken and chatId are required for Telegram notifications"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (["discord", "slack"].includes(config.type)) {
|
||||||
|
if (!config.webhookUrl) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
msg: `webhookUrl is required for ${config.type} notifications`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await this.notificationService.sendWebhookNotification(
|
await this.notificationService.sendWebhookNotification(
|
||||||
networkResponse, null, type, config.botToken, config.chatId
|
networkResponse,
|
||||||
|
config
|
||||||
);
|
);
|
||||||
} else if (type === "discord" || type === "slack") {
|
|
||||||
if (!config?.webhookUrl) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: `webhookUrl is required for ${type} notifications`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await this.notificationService.sendWebhookNotification(
|
|
||||||
networkResponse, config.webhookUrl, type
|
|
||||||
);
|
|
||||||
} else if (type === "email") {
|
|
||||||
if (!config?.address) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: "address is required for email notifications"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await this.notificationService.sendEmail(networkResponse, config.address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ success: true, msg: "Notification sent successfully" });
|
res.json({ success: true, msg: "Notification sent successfully" });
|
||||||
@@ -64,7 +64,6 @@ class NotificationController {
|
|||||||
res.status(500).json({ success: false, msg: "Failed to send notification" });
|
res.status(500).json({ success: false, msg: "Failed to send notification" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NotificationController;
|
export default NotificationController;
|
||||||
@@ -8,14 +8,14 @@ const NotificationSchema = mongoose.Schema(
|
|||||||
},
|
},
|
||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
enum: ["email", "sms"],
|
enum: ["email", "sms", "webhook"],
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
webhookUrl: { type: String }, // For Discord & Slack
|
type: String,
|
||||||
botToken: { type: String }, // For Telegram
|
webhookUrl: String,
|
||||||
chatId: { type: String }, // For Telegram
|
botToken: String,
|
||||||
|
chatId: String
|
||||||
},
|
},
|
||||||
|
|
||||||
address: {
|
address: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
@@ -56,19 +56,6 @@ const NotificationSchema = mongoose.Schema(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
NotificationSchema.pre("save", function (next) {
|
|
||||||
if (this.type === "telegram" && (!this.config.botToken || !this.config.chatId)) {
|
|
||||||
return next(new Error("botToken and chatId are required for Telegram notifications"));
|
|
||||||
}
|
|
||||||
if ((this.type === "discord" || this.type === "slack") && !this.config.webhookUrl) {
|
|
||||||
return next(new Error(`webhookUrl is required for ${this.type} notifications`));
|
|
||||||
}
|
|
||||||
if (this.type === "email" && !this.config.address) {
|
|
||||||
return next(new Error("address is required for email notifications"));
|
|
||||||
}
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
NotificationSchema.pre("save", function (next) {
|
NotificationSchema.pre("save", function (next) {
|
||||||
if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) {
|
if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) {
|
||||||
this.cpuAlertThreshold = this.alertThreshold;
|
this.cpuAlertThreshold = this.alertThreshold;
|
||||||
|
|||||||
@@ -33,22 +33,23 @@ class NotificationService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) {
|
async sendWebhookNotification(networkResponse, config) {
|
||||||
const { monitor, status } = networkResponse;
|
const { monitor, status } = networkResponse;
|
||||||
let url = address;
|
const { type, webhookUrl, botToken, chatId } = config;
|
||||||
|
let url = webhookUrl;
|
||||||
|
|
||||||
const message = this.formatNotificationMessage(monitor, status, platform, chatId);
|
const message = this.formatNotificationMessage(monitor, status, type, chatId);
|
||||||
if (!message) {
|
if (!message) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Unsupported platform: ${platform}`,
|
message: `Unsupported webhook type: ${type}`,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
method: 'sendWebhookNotification',
|
method: 'sendWebhookNotification',
|
||||||
platform
|
type
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (platform === 'telegram') {
|
if (type === 'telegram') {
|
||||||
if (!botToken || !chatId) {
|
if (!botToken || !chatId) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -56,17 +57,17 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
const response = await this.networkService.requestWebhook(type, url, message);
|
||||||
return response.status;
|
return response.status;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: `Error sending ${platform} notification`,
|
message: `Error sending ${type} 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,
|
||||||
platform,
|
type,
|
||||||
requestPayload: message
|
requestPayload: message
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in new issue
Block a user