Files
Checkmate/server/controllers/notificationController.js
T

147 lines
3.8 KiB
JavaScript
Raw Normal View History

import {
triggerNotificationBodyValidation,
createNotificationBodyValidation,
} from "../validation/joi.js";
2025-04-20 11:29:53 -07:00
import { handleError, handleValidationError } from "./controllerUtils.js";
const SERVICE_NAME = "NotificationController";
const NOTIFICATION_TYPES = {
2025-06-01 11:55:16 -07:00
WEBHOOK: "webhook",
TELEGRAM: "telegram",
2025-04-20 11:29:53 -07:00
};
const PLATFORMS = {
2025-06-01 11:55:16 -07:00
SLACK: "slack",
DISCORD: "discord",
TELEGRAM: "telegram",
2025-04-20 11:29:53 -07:00
};
class NotificationController {
2025-06-09 10:49:52 +08:00
constructor({ notificationService, stringService, statusService, db }) {
2025-06-01 11:55:16 -07:00
this.notificationService = notificationService;
this.stringService = stringService;
this.statusService = statusService;
2025-06-09 10:49:52 +08:00
this.db = db;
2025-06-01 11:55:16 -07:00
}
2025-06-10 12:07:31 +08:00
testNotification = async (req, res, next) => {
2025-06-01 11:55:16 -07:00
try {
2025-06-10 12:07:31 +08:00
const notification = req.body;
2025-06-01 11:55:16 -07:00
2025-06-19 12:10:29 +08:00
const success = await this.notificationService.sendTestNotification(notification);
2025-06-16 10:37:04 +08:00
2025-06-10 13:44:15 +08:00
if (!success) {
return res.error({
msg: "Sending notification failed",
status: 400,
2025-06-01 11:55:16 -07:00
});
}
2025-06-10 13:44:15 +08:00
return res.success({
msg: "Notification sent successfully",
});
2025-06-01 11:55:16 -07:00
} catch (error) {
next(handleError(error, SERVICE_NAME, "testWebhook"));
}
2025-06-10 12:07:31 +08:00
};
createNotification = async (req, res, next) => {
try {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
2025-06-16 10:37:04 +08:00
const body = req.body;
const { _id, teamId } = req.user;
body.userId = _id;
body.teamId = teamId;
const notification = await this.db.createNotification(body);
return res.success({
msg: "Notification created successfully",
data: notification,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createNotification"));
}
};
getNotificationsByTeamId = async (req, res, next) => {
try {
const notifications = await this.db.getNotificationsByTeamId(req.user.teamId);
return res.success({
msg: "Notifications fetched successfully",
data: notifications,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getNotificationsByTeamId"));
}
};
deleteNotification = async (req, res, next) => {
try {
await this.db.deleteNotificationById(req.params.id);
return res.success({
msg: "Notification deleted successfully",
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteNotification"));
}
};
2025-06-10 11:13:19 +08:00
getNotificationById = async (req, res, next) => {
try {
const notification = await this.db.getNotificationById(req.params.id);
return res.success({
msg: "Notification fetched successfully",
data: notification,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getNotificationById"));
}
};
editNotification = async (req, res, next) => {
try {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const notification = await this.db.editNotification(req.params.id, req.body);
return res.success({
msg: "Notification updated successfully",
data: notification,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "editNotification"));
}
};
2025-06-16 12:16:52 +08:00
testAllNotifications = async (req, res, next) => {
try {
const { monitorId } = req.body;
const monitor = await this.db.getMonitorById(monitorId);
const notifications = monitor.notifications;
if (notifications.length === 0) throw new Error("No notifications");
const result = await this.notificationService.testAllNotifications(notifications);
if (!result) throw new Error("Failed to send all notifications");
return res.success({
msg: "All notifications sent successfully",
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "testAllNotifications"));
}
};
2025-04-20 11:29:53 -07:00
}
2025-06-01 11:55:16 -07:00
export default NotificationController;