add error handling util, update middleware and controllers

This commit is contained in:
Alex Holliday committed 2025-07-15 16:33:35 -07:00
1 parent c54feb9306
commit 38bb915144
15 files changed
+683 -852

No files matched your search

+44 -68
View File
@@ -1,22 +1,8 @@
import {
triggerNotificationBodyValidation,
createNotificationBodyValidation,
} from "../validation/joi.js";
import { handleError, handleValidationError } from "./controllerUtils.js";
import { createNotificationBodyValidation } from "../validation/joi.js";
import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "NotificationController";
const NOTIFICATION_TYPES = {
WEBHOOK: "webhook",
TELEGRAM: "telegram",
};
const PLATFORMS = {
SLACK: "slack",
DISCORD: "discord",
TELEGRAM: "telegram",
};
class NotificationController {
constructor({ notificationService, stringService, statusService, db }) {
this.notificationService = notificationService;
@@ -25,8 +11,8 @@ class NotificationController {
this.db = db;
}
testNotification = async (req, res, next) => {
try {
testNotification = asyncHandler(
async (req, res, next) => {
const notification = req.body;
const success = await this.notificationService.sendTestNotification(notification);
@@ -41,22 +27,17 @@ class NotificationController {
return res.success({
msg: "Notification sent successfully",
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "testWebhook"));
}
};
},
SERVICE_NAME,
"testNotification"
);
createNotification = async (req, res, next) => {
try {
createNotification = asyncHandler(
async (req, res, next) => {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const body = req.body;
const { _id, teamId } = req.user;
body.userId = _id;
@@ -66,69 +47,64 @@ class NotificationController {
msg: "Notification created successfully",
data: notification,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createNotification"));
}
};
},
SERVICE_NAME,
"createNotification"
);
getNotificationsByTeamId = async (req, res, next) => {
try {
getNotificationsByTeamId = asyncHandler(
async (req, res, next) => {
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"));
}
};
},
SERVICE_NAME,
"getNotificationsByTeamId"
);
deleteNotification = async (req, res, next) => {
try {
deleteNotification = asyncHandler(
async (req, res, next) => {
await this.db.deleteNotificationById(req.params.id);
return res.success({
msg: "Notification deleted successfully",
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteNotification"));
}
};
},
SERVICE_NAME,
"deleteNotification"
);
getNotificationById = async (req, res, next) => {
try {
getNotificationById = asyncHandler(
async (req, res, next) => {
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"));
}
};
},
SERVICE_NAME,
"getNotificationById"
);
editNotification = async (req, res, next) => {
try {
editNotification = asyncHandler(
async (req, res, next) => {
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"));
}
};
},
SERVICE_NAME,
"editNotification"
);
testAllNotifications = async (req, res, next) => {
try {
testAllNotifications = asyncHandler(
async (req, res, next) => {
const { monitorId } = req.body;
const monitor = await this.db.getMonitorById(monitorId);
const notifications = monitor.notifications;
@@ -138,10 +114,10 @@ class NotificationController {
return res.success({
msg: "All notifications sent successfully",
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "testAllNotifications"));
}
};
},
SERVICE_NAME,
"testAllNotifications"
);
}
export default NotificationController;