Configured a config object in the notification schema.

This commit is contained in:
Skorpios committed 2025-02-09 12:48:22 -08:00
1 parent 01e73fe5b8
commit d8379f7e88
2 files changed
+32 -17

No files matched your search

+12 -7
View File
@@ -6,7 +6,7 @@ class NotificationController {
} }
async triggerNotification(req, res) { async triggerNotification(req, res) {
const { monitorId, type, webhookUrl, botToken, chatId } = req.body; const { monitorId, type, config } = req.body;
if (!monitorId || !type) { if (!monitorId || !type) {
return res.status(400).json({ return res.status(400).json({
@@ -24,29 +24,33 @@ class NotificationController {
}; };
if (type === "telegram") { if (type === "telegram") {
if (!botToken || !chatId) { if (!config?.botToken || !config?.chatId) {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
msg: "botToken and chatId are required for Telegram notifications" msg: "botToken and chatId are required for Telegram notifications"
}); });
} }
await this.notificationService.sendWebhookNotification(networkResponse, null, type, botToken, chatId); await this.notificationService.sendWebhookNotification(
networkResponse, null, type, config.botToken, config.chatId
);
} else if (type === "discord" || type === "slack") { } else if (type === "discord" || type === "slack") {
if (!webhookUrl) { if (!config?.webhookUrl) {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
msg: `webhookUrl is required for ${type} notifications` msg: `webhookUrl is required for ${type} notifications`
}); });
} }
await this.notificationService.sendWebhookNotification(networkResponse, webhookUrl, type); await this.notificationService.sendWebhookNotification(
networkResponse, config.webhookUrl, type
);
} else if (type === "email") { } else if (type === "email") {
if (!req.body.address) { if (!config?.address) {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
msg: "address is required for email notifications" msg: "address is required for email notifications"
}); });
} }
await this.notificationService.sendEmail(networkResponse, req.body.address); await this.notificationService.sendEmail(networkResponse, config.address);
} }
res.json({ success: true, msg: "Notification sent successfully" }); res.json({ success: true, msg: "Notification sent successfully" });
@@ -60,6 +64,7 @@ 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;
+20 -10
View File
@@ -8,17 +8,14 @@ const NotificationSchema = mongoose.Schema(
}, },
type: { type: {
type: String, type: String,
enum: ["email", "sms", "discord", "slack", "telegram"], enum: ["email", "sms"],
}, },
webhookUrl: { config: {
type: String, webhookUrl: { type: String }, // For Discord & Slack
}, botToken: { type: String }, // For Telegram
botToken: { chatId: { type: String }, // For Telegram
type: String, },
},
chatId: {
type: String,
},
address: { address: {
type: String, type: String,
}, },
@@ -59,6 +56,19 @@ 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;