mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-29 13:19:33 -06:00
Refactored notification schema config object.
This commit is contained in:
@@ -23,34 +23,34 @@ class NotificationController {
|
||||
prevStatus: true,
|
||||
};
|
||||
|
||||
if (type === "telegram") {
|
||||
if (!config?.botToken || !config?.chatId) {
|
||||
if (type === "webhook") {
|
||||
if (!config?.type) {
|
||||
return res.status(400).json({
|
||||
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(
|
||||
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" });
|
||||
@@ -64,7 +64,6 @@ class NotificationController {
|
||||
res.status(500).json({ success: false, msg: "Failed to send notification" });
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default NotificationController;
|
||||
@@ -8,14 +8,14 @@ const NotificationSchema = mongoose.Schema(
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: ["email", "sms"],
|
||||
enum: ["email", "sms", "webhook"],
|
||||
},
|
||||
config: {
|
||||
webhookUrl: { type: String }, // For Discord & Slack
|
||||
botToken: { type: String }, // For Telegram
|
||||
chatId: { type: String }, // For Telegram
|
||||
type: String,
|
||||
webhookUrl: String,
|
||||
botToken: String,
|
||||
chatId: String
|
||||
},
|
||||
|
||||
address: {
|
||||
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) {
|
||||
if (!this.cpuAlertThreshold || this.isModified("alertThreshold")) {
|
||||
this.cpuAlertThreshold = this.alertThreshold;
|
||||
|
||||
@@ -33,22 +33,23 @@ class NotificationService {
|
||||
return null;
|
||||
}
|
||||
|
||||
async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) {
|
||||
async sendWebhookNotification(networkResponse, config) {
|
||||
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) {
|
||||
this.logger.warn({
|
||||
message: `Unsupported platform: ${platform}`,
|
||||
message: `Unsupported webhook type: ${type}`,
|
||||
service: this.SERVICE_NAME,
|
||||
method: 'sendWebhookNotification',
|
||||
platform
|
||||
type
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (platform === 'telegram') {
|
||||
if (type === 'telegram') {
|
||||
if (!botToken || !chatId) {
|
||||
return false;
|
||||
}
|
||||
@@ -56,17 +57,17 @@ class NotificationService {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||
const response = await this.networkService.requestWebhook(type, url, message);
|
||||
return response.status;
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
message: `Error sending ${platform} notification`,
|
||||
message: `Error sending ${type} notification`,
|
||||
service: this.SERVICE_NAME,
|
||||
method: 'sendWebhookNotification',
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
url,
|
||||
platform,
|
||||
type,
|
||||
requestPayload: message
|
||||
});
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user