Skip to content

Commit

Permalink
Configured a config object in the notification schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skorpios604 committed Feb 9, 2025
1 parent 01e73fe commit d8379f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
27 changes: 16 additions & 11 deletions Server/controllers/notificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,53 @@ class NotificationController {
}

async triggerNotification(req, res) {
const { monitorId, type, webhookUrl, botToken, chatId } = req.body;

const { monitorId, type, config } = req.body;
if (!monitorId || !type) {
return res.status(400).json({
success: false,
msg: "monitorId and type are required"
});
}

try {
const networkResponse = {
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
status: false,
statusChanged: true,
prevStatus: true,
};

if (type === "telegram") {
if (!botToken || !chatId) {
if (!config?.botToken || !config?.chatId) {
return res.status(400).json({
success: false,
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") {
if (!webhookUrl) {
if (!config?.webhookUrl) {
return res.status(400).json({
success: false,
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") {
if (!req.body.address) {
if (!config?.address) {
return res.status(400).json({
success: false,
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" });
} catch (error) {
logger.error({
Expand All @@ -60,6 +64,7 @@ class NotificationController {
res.status(500).json({ success: false, msg: "Failed to send notification" });
}
}

}

export default NotificationController;
30 changes: 20 additions & 10 deletions Server/db/models/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ const NotificationSchema = mongoose.Schema(
},
type: {
type: String,
enum: ["email", "sms", "discord", "slack", "telegram"],
enum: ["email", "sms"],
},
webhookUrl: {
type: String,
},
botToken: {
type: String,
},
chatId: {
type: String,
},
config: {
webhookUrl: { type: String }, // For Discord & Slack
botToken: { type: String }, // For Telegram
chatId: { type: String }, // For Telegram
},

address: {
type: String,
},
Expand Down Expand Up @@ -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) {
if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) {
this.cpuAlertThreshold = this.alertThreshold;
Expand Down

0 comments on commit d8379f7

Please sign in to comment.