Files
Checkmate/Server/controllers/notificationController.js
T

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-02-16 20:21:44 -08:00
import {
triggerNotificationBodyValidation,
telegramWebhookConfigValidation,
discordWebhookConfigValidation,
slackWebhookConfigValidation
} from '../validation/joi.js';
import { handleError, handleValidationError } from './controllerUtils.js';
const SERVICE_NAME = "NotificationController";
2025-02-08 17:07:14 -08:00
class NotificationController {
constructor(notificationService) {
this.notificationService = notificationService;
2025-02-12 21:22:44 -08:00
this.triggerNotification = this.triggerNotification.bind(this);
2025-02-08 17:07:14 -08:00
}
2025-02-16 19:32:52 -08:00
async triggerNotification(req, res, next) {
2025-02-16 20:21:44 -08:00
try {
await triggerNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
stripUnknown: true
});
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
2025-02-08 17:07:14 -08:00
try {
2025-02-16 19:32:52 -08:00
const { monitorId, type, platform, config } = req.body;
2025-02-16 20:21:44 -08:00
2025-02-08 17:07:14 -08:00
const networkResponse = {
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
status: false,
statusChanged: true,
prevStatus: true,
};
2025-02-16 20:21:44 -08:00
if (type === "webhook") {
2025-02-16 19:32:52 -08:00
const notification = {
type,
2025-02-16 20:21:44 -08:00
platform,
2025-02-16 19:32:52 -08:00
config
};
await this.notificationService.sendWebhookNotification(
networkResponse,
2025-02-16 19:32:52 -08:00
notification
);
2025-02-08 17:07:14 -08:00
}
2025-02-16 20:21:44 -08:00
return res.success({
msg: "Notification sent successfully"
});
2025-02-16 20:21:44 -08:00
2025-02-08 17:07:14 -08:00
} catch (error) {
2025-02-16 20:21:44 -08:00
next(handleError(error, SERVICE_NAME, "triggerNotification"));
2025-02-08 17:07:14 -08:00
}
}
}
export default NotificationController;