Skip to content

Commit

Permalink
Refactored notification schema config object.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skorpios604 committed Feb 9, 2025
1 parent d8379f7 commit f46b076
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 49 deletions.
43 changes: 21 additions & 22 deletions Server/controllers/notificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
}
await this.notificationService.sendWebhookNotification(
networkResponse, null, type, config.botToken, config.chatId
);
} else if (type === "discord" || type === "slack") {
if (!config?.webhookUrl) {
return res.status(400).json({
success: false,
msg: `webhookUrl is required for ${type} notifications`
});

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, config.webhookUrl, type
networkResponse,
config
);
} 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" });
Expand All @@ -64,7 +64,6 @@ class NotificationController {
res.status(500).json({ success: false, msg: "Failed to send notification" });
}
}

}

export default NotificationController;
23 changes: 5 additions & 18 deletions Server/db/models/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions Server/service/notificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,41 @@ 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;
}
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
}

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;
Expand Down

0 comments on commit f46b076

Please sign in to comment.