Skip to content

Commit d8379f7

Browse files
committed
Configured a config object in the notification schema.
1 parent 01e73fe commit d8379f7

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Server/controllers/notificationController.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,53 @@ class NotificationController {
66
}
77

88
async triggerNotification(req, res) {
9-
const { monitorId, type, webhookUrl, botToken, chatId } = req.body;
10-
9+
const { monitorId, type, config } = req.body;
10+
1111
if (!monitorId || !type) {
1212
return res.status(400).json({
1313
success: false,
1414
msg: "monitorId and type are required"
1515
});
1616
}
17-
17+
1818
try {
1919
const networkResponse = {
2020
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
2121
status: false,
2222
statusChanged: true,
2323
prevStatus: true,
2424
};
25-
25+
2626
if (type === "telegram") {
27-
if (!botToken || !chatId) {
27+
if (!config?.botToken || !config?.chatId) {
2828
return res.status(400).json({
2929
success: false,
3030
msg: "botToken and chatId are required for Telegram notifications"
3131
});
3232
}
33-
await this.notificationService.sendWebhookNotification(networkResponse, null, type, botToken, chatId);
33+
await this.notificationService.sendWebhookNotification(
34+
networkResponse, null, type, config.botToken, config.chatId
35+
);
3436
} else if (type === "discord" || type === "slack") {
35-
if (!webhookUrl) {
37+
if (!config?.webhookUrl) {
3638
return res.status(400).json({
3739
success: false,
3840
msg: `webhookUrl is required for ${type} notifications`
3941
});
4042
}
41-
await this.notificationService.sendWebhookNotification(networkResponse, webhookUrl, type);
43+
await this.notificationService.sendWebhookNotification(
44+
networkResponse, config.webhookUrl, type
45+
);
4246
} else if (type === "email") {
43-
if (!req.body.address) {
47+
if (!config?.address) {
4448
return res.status(400).json({
4549
success: false,
4650
msg: "address is required for email notifications"
4751
});
4852
}
49-
await this.notificationService.sendEmail(networkResponse, req.body.address);
53+
await this.notificationService.sendEmail(networkResponse, config.address);
5054
}
51-
55+
5256
res.json({ success: true, msg: "Notification sent successfully" });
5357
} catch (error) {
5458
logger.error({
@@ -60,6 +64,7 @@ class NotificationController {
6064
res.status(500).json({ success: false, msg: "Failed to send notification" });
6165
}
6266
}
67+
6368
}
6469

6570
export default NotificationController;

Server/db/models/Notification.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ const NotificationSchema = mongoose.Schema(
88
},
99
type: {
1010
type: String,
11-
enum: ["email", "sms", "discord", "slack", "telegram"],
11+
enum: ["email", "sms"],
1212
},
13-
webhookUrl: {
14-
type: String,
15-
},
16-
botToken: {
17-
type: String,
18-
},
19-
chatId: {
20-
type: String,
21-
},
13+
config: {
14+
webhookUrl: { type: String }, // For Discord & Slack
15+
botToken: { type: String }, // For Telegram
16+
chatId: { type: String }, // For Telegram
17+
},
18+
2219
address: {
2320
type: String,
2421
},
@@ -59,6 +56,19 @@ const NotificationSchema = mongoose.Schema(
5956
}
6057
);
6158

59+
NotificationSchema.pre("save", function (next) {
60+
if (this.type === "telegram" && (!this.config.botToken || !this.config.chatId)) {
61+
return next(new Error("botToken and chatId are required for Telegram notifications"));
62+
}
63+
if ((this.type === "discord" || this.type === "slack") && !this.config.webhookUrl) {
64+
return next(new Error(`webhookUrl is required for ${this.type} notifications`));
65+
}
66+
if (this.type === "email" && !this.config.address) {
67+
return next(new Error("address is required for email notifications"));
68+
}
69+
next();
70+
});
71+
6272
NotificationSchema.pre("save", function (next) {
6373
if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) {
6474
this.cpuAlertThreshold = this.alertThreshold;

0 commit comments

Comments
 (0)