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
+36 -21

No files matched your search

+20 -10
View File
@@ -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,
},
@@ -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;