Files
Checkmate/server/controllers/notificationController.js
T

175 lines
4.6 KiB
JavaScript
Raw Normal View History

import { createNotificationBodyValidation } from "../validation/joi.js";
2025-07-24 15:06:46 -07:00
import BaseController from "./baseController.js";
2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "NotificationController";
2025-07-24 15:06:46 -07:00
class NotificationController extends BaseController {
2025-07-25 10:16:29 -07:00
constructor(commonDependencies, { notificationService, statusService }) {
super(commonDependencies);
2025-06-01 11:55:16 -07:00
this.notificationService = notificationService;
this.statusService = statusService;
}
2025-07-24 15:06:46 -07:00
testNotification = this.asyncHandler(
async (req, res) => {
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) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createServerError("Sending notification failed");
2025-06-01 11:55:16 -07:00
}
2025-06-10 13:44:15 +08:00
return res.success({
msg: "Notification sent successfully",
});
},
SERVICE_NAME,
"testNotification"
);
2025-07-24 15:06:46 -07:00
createNotification = this.asyncHandler(
async (req, res) => {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
2025-06-16 10:37:04 +08:00
const body = req.body;
2025-07-21 14:11:09 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 14:11:09 -07:00
}
const userId = req?.user?._id;
if (!userId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("User ID is required");
2025-07-21 14:11:09 -07:00
}
body.userId = userId;
2025-06-16 10:37:04 +08:00
body.teamId = teamId;
2025-07-21 14:11:09 -07:00
2025-06-16 10:37:04 +08:00
const notification = await this.db.createNotification(body);
return res.success({
msg: "Notification created successfully",
data: notification,
});
},
SERVICE_NAME,
"createNotification"
);
2025-07-24 15:06:46 -07:00
getNotificationsByTeamId = this.asyncHandler(
async (req, res) => {
2025-07-21 14:11:09 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 14:11:09 -07:00
}
const notifications = await this.db.getNotificationsByTeamId(teamId);
return res.success({
msg: "Notifications fetched successfully",
data: notifications,
});
},
SERVICE_NAME,
"getNotificationsByTeamId"
);
2025-07-24 15:06:46 -07:00
deleteNotification = this.asyncHandler(
async (req, res) => {
2025-07-21 14:11:09 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 14:11:09 -07:00
}
const notification = await this.db.getNotificationById(req.params.id);
if (!notification.teamId.equals(teamId)) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createAuthorizationError();
2025-07-21 14:11:09 -07:00
}
await this.db.deleteNotificationById(req.params.id);
return res.success({
msg: "Notification deleted successfully",
});
},
SERVICE_NAME,
"deleteNotification"
);
2025-06-10 11:13:19 +08:00
2025-07-24 15:06:46 -07:00
getNotificationById = this.asyncHandler(
async (req, res) => {
2025-06-10 11:13:19 +08:00
const notification = await this.db.getNotificationById(req.params.id);
2025-07-21 14:11:09 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 14:11:09 -07:00
}
if (!notification.teamId.equals(teamId)) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createAuthorizationError();
2025-07-21 14:11:09 -07:00
}
2025-06-10 11:13:19 +08:00
return res.success({
msg: "Notification fetched successfully",
data: notification,
});
},
SERVICE_NAME,
"getNotificationById"
);
2025-06-10 11:13:19 +08:00
2025-07-24 15:06:46 -07:00
editNotification = this.asyncHandler(
async (req, res) => {
2025-06-10 11:13:19 +08:00
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
2025-07-21 14:11:09 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 14:11:09 -07:00
}
const notification = await this.db.getNotificationById(req.params.id);
if (!notification.teamId.equals(teamId)) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createAuthorizationError();
2025-07-21 14:11:09 -07:00
}
const editedNotification = await this.db.editNotification(req.params.id, req.body);
2025-06-10 11:13:19 +08:00
return res.success({
msg: "Notification updated successfully",
2025-07-21 14:11:09 -07:00
data: editedNotification,
2025-06-10 11:13:19 +08:00
});
},
SERVICE_NAME,
"editNotification"
);
2025-06-16 12:16:52 +08:00
2025-07-24 15:06:46 -07:00
testAllNotifications = this.asyncHandler(
async (req, res) => {
2025-07-21 14:11:09 -07:00
const monitorId = req.body.monitorId;
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 14:11:09 -07:00
}
2025-06-16 12:16:52 +08:00
const monitor = await this.db.getMonitorById(monitorId);
2025-07-21 14:11:09 -07:00
if (!monitor.teamId.equals(teamId)) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createAuthorizationError();
2025-07-21 14:11:09 -07:00
}
2025-06-16 12:16:52 +08:00
const notifications = monitor.notifications;
2025-07-24 15:06:46 -07:00
if (notifications.length === 0) throw this.errorService.createBadRequestError("No notifications");
2025-06-16 12:16:52 +08:00
const result = await this.notificationService.testAllNotifications(notifications);
2025-07-24 15:06:46 -07:00
if (!result) throw this.errorService.createServerError("Failed to send all notifications");
2025-06-16 12:16:52 +08:00
return res.success({
msg: "All notifications sent successfully",
});
},
SERVICE_NAME,
"testAllNotifications"
);
2025-04-20 11:29:53 -07:00
}
2025-06-01 11:55:16 -07:00
export default NotificationController;