mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-13 21:29:46 -06:00
188 lines
4.5 KiB
JavaScript
Executable File
188 lines
4.5 KiB
JavaScript
Executable File
import { createNotificationBodyValidation } from "../validation/joi.js";
|
|
import { asyncHandler } from "../utils/errorUtils.js";
|
|
|
|
const SERVICE_NAME = "NotificationController";
|
|
|
|
class NotificationController {
|
|
constructor({ notificationService, stringService, statusService, db }) {
|
|
this.notificationService = notificationService;
|
|
this.stringService = stringService;
|
|
this.statusService = statusService;
|
|
this.db = db;
|
|
}
|
|
|
|
testNotification = asyncHandler(
|
|
async (req, res, next) => {
|
|
const notification = req.body;
|
|
|
|
const success = await this.notificationService.sendTestNotification(notification);
|
|
|
|
if (!success) {
|
|
return res.error({
|
|
msg: "Sending notification failed",
|
|
status: 400,
|
|
});
|
|
}
|
|
|
|
return res.success({
|
|
msg: "Notification sent successfully",
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"testNotification"
|
|
);
|
|
|
|
createNotification = asyncHandler(
|
|
async (req, res, next) => {
|
|
await createNotificationBodyValidation.validateAsync(req.body, {
|
|
abortEarly: false,
|
|
});
|
|
|
|
const body = req.body;
|
|
|
|
const teamId = req?.user?.teamId;
|
|
if (!teamId) {
|
|
throw new Error("No team ID in request");
|
|
}
|
|
|
|
const userId = req?.user?._id;
|
|
if (!userId) {
|
|
throw new Error("No user ID in request");
|
|
}
|
|
body.userId = userId;
|
|
body.teamId = teamId;
|
|
|
|
const notification = await this.db.createNotification(body);
|
|
return res.success({
|
|
msg: "Notification created successfully",
|
|
data: notification,
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"createNotification"
|
|
);
|
|
|
|
getNotificationsByTeamId = asyncHandler(
|
|
async (req, res, next) => {
|
|
const teamId = req?.user?.teamId;
|
|
if (!teamId) {
|
|
throw new Error("No team ID in request");
|
|
}
|
|
|
|
const notifications = await this.db.getNotificationsByTeamId(teamId);
|
|
|
|
return res.success({
|
|
msg: "Notifications fetched successfully",
|
|
data: notifications,
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"getNotificationsByTeamId"
|
|
);
|
|
|
|
deleteNotification = asyncHandler(
|
|
async (req, res, next) => {
|
|
const teamId = req?.user?.teamId;
|
|
if (!teamId) {
|
|
throw new Error("No team ID in request");
|
|
}
|
|
|
|
const notification = await this.db.getNotificationById(req.params.id);
|
|
if (!notification.teamId.equals(teamId)) {
|
|
const error = new Error("Unauthorized");
|
|
error.status = 403;
|
|
throw error;
|
|
}
|
|
|
|
await this.db.deleteNotificationById(req.params.id);
|
|
return res.success({
|
|
msg: "Notification deleted successfully",
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"deleteNotification"
|
|
);
|
|
|
|
getNotificationById = asyncHandler(
|
|
async (req, res, next) => {
|
|
const notification = await this.db.getNotificationById(req.params.id);
|
|
|
|
const teamId = req?.user?.teamId;
|
|
if (!teamId) {
|
|
throw new Error("No team ID in request");
|
|
}
|
|
|
|
if (!notification.teamId.equals(teamId)) {
|
|
const error = new Error("Unauthorized");
|
|
error.status = 403;
|
|
throw error;
|
|
}
|
|
return res.success({
|
|
msg: "Notification fetched successfully",
|
|
data: notification,
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"getNotificationById"
|
|
);
|
|
|
|
editNotification = asyncHandler(
|
|
async (req, res, next) => {
|
|
await createNotificationBodyValidation.validateAsync(req.body, {
|
|
abortEarly: false,
|
|
});
|
|
|
|
const teamId = req?.user?.teamId;
|
|
if (!teamId) {
|
|
throw new Error("No team ID in request");
|
|
}
|
|
|
|
const notification = await this.db.getNotificationById(req.params.id);
|
|
|
|
if (!notification.teamId.equals(teamId)) {
|
|
const error = new Error("Unauthorized");
|
|
error.status = 403;
|
|
throw error;
|
|
}
|
|
|
|
const editedNotification = await this.db.editNotification(req.params.id, req.body);
|
|
return res.success({
|
|
msg: "Notification updated successfully",
|
|
data: editedNotification,
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"editNotification"
|
|
);
|
|
|
|
testAllNotifications = asyncHandler(
|
|
async (req, res, next) => {
|
|
const monitorId = req.body.monitorId;
|
|
const teamId = req?.user?.teamId;
|
|
if (!teamId) {
|
|
throw new Error("No team ID in request");
|
|
}
|
|
|
|
const monitor = await this.db.getMonitorById(monitorId);
|
|
|
|
if (!monitor.teamId.equals(teamId)) {
|
|
const error = new Error("Unauthorized");
|
|
error.status = 403;
|
|
throw error;
|
|
}
|
|
|
|
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",
|
|
});
|
|
},
|
|
SERVICE_NAME,
|
|
"testAllNotifications"
|
|
);
|
|
}
|
|
|
|
export default NotificationController;
|