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

+14 -20
View File
@@ -1,5 +1,5 @@
import { createAnnouncementValidation } from "../validation/joi.js"; import { createAnnouncementValidation } from "../validation/joi.js";
import { handleError } from "./controllerUtils.js"; import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "announcementController"; const SERVICE_NAME = "announcementController";
@@ -28,16 +28,10 @@ class AnnouncementController {
* *
* @returns {Promise<void>} A promise that resolves once the response is sent. * @returns {Promise<void>} A promise that resolves once the response is sent.
*/ */
createAnnouncement = async (req, res, next) => { createAnnouncement = asyncHandler(
try { async (req, res, next) => {
await createAnnouncementValidation.validateAsync(req.body); await createAnnouncementValidation.validateAsync(req.body);
} catch (error) { const { title, message } = req.body;
return next(handleError(error, SERVICE_NAME)); // Handle Joi validation errors
}
const { title, message } = req.body;
try {
const announcementData = { const announcementData = {
title: title.trim(), title: title.trim(),
message: message.trim(), message: message.trim(),
@@ -49,10 +43,10 @@ class AnnouncementController {
msg: this.stringService.createAnnouncement, msg: this.stringService.createAnnouncement,
data: newAnnouncement, data: newAnnouncement,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "createAnnouncement")); SERVICE_NAME,
} "createAnnouncement"
}; );
/** /**
* Handles retrieving announcements with pagination. * Handles retrieving announcements with pagination.
@@ -63,17 +57,17 @@ class AnnouncementController {
* - `msg`: A message about the success of the request. * - `msg`: A message about the success of the request.
* @param {Function} next - The next middleware function in the stack for error handling. * @param {Function} next - The next middleware function in the stack for error handling.
*/ */
getAnnouncement = async (req, res, next) => { getAnnouncement = asyncHandler(
try { async (req, res, next) => {
const allAnnouncements = await this.db.getAnnouncements(); const allAnnouncements = await this.db.getAnnouncements();
return res.success({ return res.success({
msg: this.stringService.getAnnouncement, msg: this.stringService.getAnnouncement,
data: allAnnouncements, data: allAnnouncements,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getAnnouncement")); SERVICE_NAME,
} "getAnnouncement"
}; );
} }
export default AnnouncementController; export default AnnouncementController;
+69 -124
View File
@@ -10,7 +10,8 @@ import {
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js"; import { getTokenFromHeaders } from "../utils/utils.js";
import crypto from "crypto"; import crypto from "crypto";
import { handleValidationError, handleError } from "./controllerUtils.js"; import { asyncHandler, createAuthError, createError } from "../utils/errorUtils.js";
const SERVICE_NAME = "authController"; const SERVICE_NAME = "authController";
class AuthController { class AuthController {
@@ -32,15 +33,10 @@ class AuthController {
* @throws {Error} * @throws {Error}
*/ */
issueToken = (payload, appSettings) => { issueToken = (payload, appSettings) => {
try { const tokenTTL = appSettings?.jwtTTL ?? "2h";
const tokenTTL = appSettings?.jwtTTL ?? "2h"; const tokenSecret = appSettings?.jwtSecret;
const tokenSecret = appSettings?.jwtSecret; const payloadData = payload;
const payloadData = payload; return jwt.sign(payloadData, tokenSecret, { expiresIn: tokenTTL });
return jwt.sign(payloadData, tokenSecret, { expiresIn: tokenTTL });
} catch (error) {
throw handleError(error, SERVICE_NAME, "issueToken");
}
}; };
/** /**
@@ -55,19 +51,14 @@ class AuthController {
* @returns {Object} The response object with a success status, a message indicating the creation of the user, the created user data, and a JWT token. * @returns {Object} The response object with a success status, a message indicating the creation of the user, the created user data, and a JWT token.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
registerUser = async (req, res, next) => { registerUser = asyncHandler(
try { async (req, res, next) => {
if (req.body?.email) { if (req.body?.email) {
req.body.email = req.body.email?.toLowerCase(); req.body.email = req.body.email?.toLowerCase();
} }
await registrationBodyValidation.validateAsync(req.body); await registrationBodyValidation.validateAsync(req.body);
} catch (error) {
const validationError = handleValidationError(error, SERVICE_NAME); // Create a new user
next(validationError);
return;
}
// Create a new user
try {
const user = req.body; const user = req.body;
// If superAdmin exists, a token should be attached to all further register requests // If superAdmin exists, a token should be attached to all further register requests
const superAdminExists = await this.db.checkSuperadmin(req, res); const superAdminExists = await this.db.checkSuperadmin(req, res);
@@ -123,10 +114,10 @@ class AuthController {
msg: this.stringService.authCreateUser, msg: this.stringService.authCreateUser,
data: { user: newUser, token: token }, data: { user: newUser, token: token },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "registerController")); SERVICE_NAME,
} "registerUser"
}; );
/** /**
* Logs in a user by validating the user's credentials and issuing a JWT token. * Logs in a user by validating the user's credentials and issuing a JWT token.
@@ -140,18 +131,13 @@ class AuthController {
* @returns {Object} The response object with a success status, a message indicating the login of the user, the user data (without password and avatar image), and a JWT token. * @returns {Object} The response object with a success status, a message indicating the login of the user, the user data (without password and avatar image), and a JWT token.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422) or the password is incorrect. * @throws {Error} If there is an error during the process, especially if there is a validation error (422) or the password is incorrect.
*/ */
loginUser = async (req, res, next) => { loginUser = asyncHandler(
try { async (req, res, next) => {
if (req.body?.email) { if (req.body?.email) {
req.body.email = req.body.email?.toLowerCase(); req.body.email = req.body.email?.toLowerCase();
} }
await loginValidation.validateAsync(req.body); await loginValidation.validateAsync(req.body);
} catch (error) {
const validationError = handleValidationError(error, SERVICE_NAME);
next(validationError);
return;
}
try {
const { email, password } = req.body; const { email, password } = req.body;
// Check if user exists // Check if user exists
@@ -160,10 +146,7 @@ class AuthController {
// Compare password // Compare password
const match = await user.comparePassword(password); const match = await user.comparePassword(password);
if (match !== true) { if (match !== true) {
const error = new Error(this.stringService.authIncorrectPassword); throw createAuthError(this.stringService.authIncorrectPassword);
error.status = 401;
next(error);
return;
} }
// Remove password from user object. Should this be abstracted to DB layer? // Remove password from user object. Should this be abstracted to DB layer?
@@ -184,11 +167,10 @@ class AuthController {
token: token, token: token,
}, },
}); });
} catch (error) { },
error.status = 401; SERVICE_NAME,
next(handleError(error, SERVICE_NAME, "loginUser")); "loginUser"
} );
};
/** /**
* Edits a user's information. If the user wants to change their password, the current password is checked before updating to the new password. * Edits a user's information. If the user wants to change their password, the current password is checked before updating to the new password.
@@ -204,26 +186,16 @@ class AuthController {
* @returns {Object} The response object with a success status, a message indicating the update of the user, and the updated user data. * @returns {Object} The response object with a success status, a message indicating the update of the user, and the updated user data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422), the user is unauthorized (401), or the password is incorrect (403). * @throws {Error} If there is an error during the process, especially if there is a validation error (422), the user is unauthorized (401), or the password is incorrect (403).
*/ */
editUser = async (req, res, next) => { editUser = asyncHandler(
try { async (req, res, next) => {
await editUserParamValidation.validateAsync(req.params); await editUserParamValidation.validateAsync(req.params);
await editUserBodyValidation.validateAsync(req.body); await editUserBodyValidation.validateAsync(req.body);
} catch (error) {
const validationError = handleValidationError(error, SERVICE_NAME);
next(validationError);
return;
}
// TODO is this neccessary any longer? Verify ownership middleware should handle this // TODO is this neccessary any longer? Verify ownership middleware should handle this
if (req.params.userId !== req.user._id.toString()) { if (req.params.userId !== req.user._id.toString()) {
const error = new Error(this.stringService.unauthorized); throw createAuthError(this.stringService.unauthorized);
error.status = 401; }
error.service = SERVICE_NAME;
next(error);
return;
}
try {
// Change Password check // Change Password check
if (req.body.password && req.body.newPassword) { if (req.body.password && req.body.newPassword) {
// Get token from headers // Get token from headers
@@ -240,10 +212,7 @@ class AuthController {
// If not a match, throw a 403 // If not a match, throw a 403
// 403 instead of 401 to avoid triggering axios interceptor // 403 instead of 401 to avoid triggering axios interceptor
if (!match) { if (!match) {
const error = new Error(this.stringService.authIncorrectPassword); throw createError(this.stringService.authIncorrectPassword, 403);
error.status = 403;
next(error);
return;
} }
// If a match, update the password // If a match, update the password
req.body.password = req.body.newPassword; req.body.password = req.body.newPassword;
@@ -254,10 +223,10 @@ class AuthController {
msg: this.stringService.authUpdateUser, msg: this.stringService.authUpdateUser,
data: updatedUser, data: updatedUser,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "userEditController")); SERVICE_NAME,
} "editUser"
}; );
/** /**
* Checks if a superadmin account exists in the database. * Checks if a superadmin account exists in the database.
@@ -268,18 +237,17 @@ class AuthController {
* @returns {Object} The response object with a success status, a message indicating the existence of a superadmin, and a boolean indicating the existence of a superadmin. * @returns {Object} The response object with a success status, a message indicating the existence of a superadmin, and a boolean indicating the existence of a superadmin.
* @throws {Error} If there is an error during the process. * @throws {Error} If there is an error during the process.
*/ */
checkSuperadminExists = async (req, res, next) => { checkSuperadminExists = asyncHandler(
try { async (req, res, next) => {
const superAdminExists = await this.db.checkSuperadmin(req, res); const superAdminExists = await this.db.checkSuperadmin(req, res);
return res.success({ return res.success({
msg: this.stringService.authAdminExists, msg: this.stringService.authAdminExists,
data: superAdminExists, data: superAdminExists,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "checkSuperadminController")); SERVICE_NAME,
} "checkSuperadminExists"
}; );
/** /**
* Requests a recovery token for a user. The user's email is validated and a recovery token is created and sent via email. * Requests a recovery token for a user. The user's email is validated and a recovery token is created and sent via email.
* @async * @async
@@ -291,16 +259,9 @@ class AuthController {
* @returns {Object} The response object with a success status, a message indicating the creation of the recovery token, and the message ID of the sent email. * @returns {Object} The response object with a success status, a message indicating the creation of the recovery token, and the message ID of the sent email.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
requestRecovery = async (req, res, next) => { requestRecovery = asyncHandler(
try { async (req, res, next) => {
await recoveryValidation.validateAsync(req.body); await recoveryValidation.validateAsync(req.body);
} catch (error) {
const validationError = handleValidationError(error, SERVICE_NAME);
next(validationError);
return;
}
try {
const { email } = req.body; const { email } = req.body;
const user = await this.db.getUserByEmail(email); const user = await this.db.getUserByEmail(email);
const recoveryToken = await this.db.requestRecoveryToken(req, res); const recoveryToken = await this.db.requestRecoveryToken(req, res);
@@ -323,10 +284,10 @@ class AuthController {
msg: this.stringService.authCreateRecoveryToken, msg: this.stringService.authCreateRecoveryToken,
data: msgId, data: msgId,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "recoveryRequestController")); SERVICE_NAME,
} "requestRecovery"
}; );
/** /**
* Validates a recovery token. The recovery token is validated and if valid, a success message is returned. * Validates a recovery token. The recovery token is validated and if valid, a success message is returned.
* @async * @async
@@ -338,25 +299,17 @@ class AuthController {
* @returns {Object} The response object with a success status and a message indicating the validation of the recovery token. * @returns {Object} The response object with a success status and a message indicating the validation of the recovery token.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
validateRecovery = async (req, res, next) => { validateRecovery = asyncHandler(
try { async (req, res, next) => {
await recoveryTokenValidation.validateAsync(req.body); await recoveryTokenValidation.validateAsync(req.body);
} catch (error) {
const validationError = handleValidationError(error, SERVICE_NAME);
next(validationError);
return;
}
try {
await this.db.validateRecoveryToken(req, res); await this.db.validateRecoveryToken(req, res);
return res.success({ return res.success({
msg: this.stringService.authVerifyRecoveryToken, msg: this.stringService.authVerifyRecoveryToken,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController")); SERVICE_NAME,
} "validateRecovery"
}; );
/** /**
* Resets a user's password. The new password is validated and if valid, the user's password is updated in the database and a new JWT token is issued. * Resets a user's password. The new password is validated and if valid, the user's password is updated in the database and a new JWT token is issued.
@@ -370,27 +323,20 @@ class AuthController {
* @returns {Object} The response object with a success status, a message indicating the reset of the password, the updated user data (without password and avatar image), and a new JWT token. * @returns {Object} The response object with a success status, a message indicating the reset of the password, the updated user data (without password and avatar image), and a new JWT token.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
resetPassword = async (req, res, next) => { resetPassword = asyncHandler(
try { async (req, res, next) => {
await newPasswordValidation.validateAsync(req.body); await newPasswordValidation.validateAsync(req.body);
} catch (error) {
const validationError = handleValidationError(error, SERVICE_NAME);
next(validationError);
return;
}
try {
const user = await this.db.resetPassword(req, res); const user = await this.db.resetPassword(req, res);
const appSettings = await this.settingsService.getSettings(); const appSettings = await this.settingsService.getSettings();
const token = this.issueToken(user._doc, appSettings); const token = this.issueToken(user._doc, appSettings);
return res.success({ return res.success({
msg: this.stringService.authResetPassword, msg: this.stringService.authResetPassword,
data: { user, token }, data: { user, token },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "resetPasswordController")); SERVICE_NAME,
} "resetPassword"
}; );
/** /**
* Deletes a user and all associated monitors, checks, and alerts. * Deletes a user and all associated monitors, checks, and alerts.
@@ -401,8 +347,8 @@ class AuthController {
* @returns {Object} The response object with success status and message. * @returns {Object} The response object with success status and message.
* @throws {Error} If user validation fails or user is not found in the database. * @throws {Error} If user validation fails or user is not found in the database.
*/ */
deleteUser = async (req, res, next) => { deleteUser = asyncHandler(
try { async (req, res, next) => {
const token = getTokenFromHeaders(req.headers); const token = getTokenFromHeaders(req.headers);
const decodedToken = jwt.decode(token); const decodedToken = jwt.decode(token);
const { email } = decodedToken; const { email } = decodedToken;
@@ -430,23 +376,22 @@ class AuthController {
return res.success({ return res.success({
msg: this.stringService.authDeleteUser, msg: this.stringService.authDeleteUser,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteUserController")); SERVICE_NAME,
} "deleteUser"
}; );
getAllUsers = async (req, res, next) => { getAllUsers = asyncHandler(
try { async (req, res, next) => {
const allUsers = await this.db.getAllUsers(req, res); const allUsers = await this.db.getAllUsers(req, res);
return res.success({ return res.success({
msg: this.stringService.authGetAllUsers, msg: this.stringService.authGetAllUsers,
data: allUsers, data: allUsers,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getAllUsersController")); SERVICE_NAME,
} "getAllUsers"
}; );
} }
export default AuthController; export default AuthController;
+57 -99
View File
@@ -12,9 +12,7 @@ import {
ackAllChecksParamValidation, ackAllChecksParamValidation,
ackAllChecksBodyValidation, ackAllChecksBodyValidation,
} from "../validation/joi.js"; } from "../validation/joi.js";
import jwt from "jsonwebtoken"; import { asyncHandler } from "../utils/errorUtils.js";
import { getTokenFromHeaders } from "../utils/utils.js";
import { handleValidationError, handleError } from "./controllerUtils.js";
const SERVICE_NAME = "checkController"; const SERVICE_NAME = "checkController";
@@ -25,16 +23,11 @@ class CheckController {
this.stringService = stringService; this.stringService = stringService;
} }
createCheck = async (req, res, next) => { createCheck = asyncHandler(
try { async (req, res, next) => {
await createCheckParamValidation.validateAsync(req.params); await createCheckParamValidation.validateAsync(req.params);
await createCheckBodyValidation.validateAsync(req.body); await createCheckBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const checkData = { ...req.body }; const checkData = { ...req.body };
const check = await this.db.createCheck(checkData); const check = await this.db.createCheck(checkData);
@@ -42,21 +35,16 @@ class CheckController {
msg: this.stringService.checkCreate, msg: this.stringService.checkCreate,
data: check, data: check,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "createCheck")); SERVICE_NAME,
} "createCheck"
}; );
getChecksByMonitor = async (req, res, next) => { getChecksByMonitor = asyncHandler(
try { async (req, res, next) => {
await getChecksParamValidation.validateAsync(req.params); await getChecksParamValidation.validateAsync(req.params);
await getChecksQueryValidation.validateAsync(req.query); await getChecksQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId } = req.params; const { monitorId } = req.params;
let { type, sortOrder, dateRange, filter, ack, page, rowsPerPage, status } = let { type, sortOrder, dateRange, filter, ack, page, rowsPerPage, status } =
req.query; req.query;
@@ -76,20 +64,16 @@ class CheckController {
msg: this.stringService.checkGet, msg: this.stringService.checkGet,
data: result, data: result,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getChecks")); SERVICE_NAME,
} "getChecksByMonitor"
}; );
getChecksByTeam = async (req, res, next) => { getChecksByTeam = asyncHandler(
try { async (req, res, next) => {
await getTeamChecksParamValidation.validateAsync(req.params); await getTeamChecksParamValidation.validateAsync(req.params);
await getTeamChecksQueryValidation.validateAsync(req.query); await getTeamChecksQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
let { sortOrder, dateRange, filter, ack, page, rowsPerPage } = req.query; let { sortOrder, dateRange, filter, ack, page, rowsPerPage } = req.query;
const { teamId } = req.user; const { teamId } = req.user;
@@ -106,33 +90,28 @@ class CheckController {
msg: this.stringService.checkGet, msg: this.stringService.checkGet,
data: checkData, data: checkData,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getTeamChecks")); SERVICE_NAME,
} "getChecksByTeam"
}; );
getChecksSummaryByTeamId = async (req, res, next) => { getChecksSummaryByTeamId = asyncHandler(
try { async (req, res, next) => {
const { teamId } = req.user; const { teamId } = req.user;
const summary = await this.db.getChecksSummaryByTeamId({ teamId }); const summary = await this.db.getChecksSummaryByTeamId({ teamId });
return res.success({ return res.success({
msg: this.stringService.checkGetSummary, msg: this.stringService.checkGetSummary,
data: summary, data: summary,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getChecksSummaryByTeamId")); SERVICE_NAME,
} "getChecksSummaryByTeamId"
}; );
ackCheck = async (req, res, next) => { ackCheck = asyncHandler(
try { async (req, res, next) => {
await ackCheckBodyValidation.validateAsync(req.body); await ackCheckBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { checkId } = req.params; const { checkId } = req.params;
const { ack } = req.body; const { ack } = req.body;
const { teamId } = req.user; const { teamId } = req.user;
@@ -143,21 +122,16 @@ class CheckController {
msg: this.stringService.checkUpdateStatus, msg: this.stringService.checkUpdateStatus,
data: updatedCheck, data: updatedCheck,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "ackCheck")); SERVICE_NAME,
} "ackCheck"
}; );
ackAllChecks = async (req, res, next) => { ackAllChecks = asyncHandler(
try { async (req, res, next) => {
await ackAllChecksParamValidation.validateAsync(req.params); await ackAllChecksParamValidation.validateAsync(req.params);
await ackAllChecksBodyValidation.validateAsync(req.body); await ackAllChecksBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId, path } = req.params; const { monitorId, path } = req.params;
const { ack } = req.body; const { ack } = req.body;
const { teamId } = req.user; const { teamId } = req.user;
@@ -168,40 +142,30 @@ class CheckController {
msg: this.stringService.checkUpdateStatus, msg: this.stringService.checkUpdateStatus,
data: updatedChecks, data: updatedChecks,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "ackAllChecks")); SERVICE_NAME,
} "ackAllChecks"
}; );
deleteChecks = async (req, res, next) => { deleteChecks = asyncHandler(
try { async (req, res, next) => {
await deleteChecksParamValidation.validateAsync(req.params); await deleteChecksParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const deletedCount = await this.db.deleteChecks(req.params.monitorId); const deletedCount = await this.db.deleteChecks(req.params.monitorId);
return res.success({ return res.success({
msg: this.stringService.checkDelete, msg: this.stringService.checkDelete,
data: { deletedCount }, data: { deletedCount },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteChecks")); SERVICE_NAME,
} "deleteChecks"
}; );
deleteChecksByTeamId = async (req, res, next) => { deleteChecksByTeamId = asyncHandler(
try { async (req, res, next) => {
await deleteChecksByTeamIdParamValidation.validateAsync(req.params); await deleteChecksByTeamIdParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { teamId } = req.user; const { teamId } = req.user;
const deletedCount = await this.db.deleteChecksByTeamId(teamId); const deletedCount = await this.db.deleteChecksByTeamId(teamId);
@@ -209,23 +173,17 @@ class CheckController {
msg: this.stringService.checkDelete, msg: this.stringService.checkDelete,
data: { deletedCount }, data: { deletedCount },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteChecksByTeamId")); SERVICE_NAME,
} "deleteChecksByTeamId"
}; );
updateChecksTTL = async (req, res, next) => { updateChecksTTL = asyncHandler(
const SECONDS_PER_DAY = 86400; async (req, res, next) => {
const SECONDS_PER_DAY = 86400;
try {
await updateChecksTTLBodyValidation.validateAsync(req.body); await updateChecksTTLBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
// Get user's teamId
const { teamId } = req.user; const { teamId } = req.user;
const ttl = parseInt(req.body.ttl, 10) * SECONDS_PER_DAY; const ttl = parseInt(req.body.ttl, 10) * SECONDS_PER_DAY;
await this.db.updateChecksTTL(teamId, ttl); await this.db.updateChecksTTL(teamId, ttl);
@@ -233,9 +191,9 @@ class CheckController {
return res.success({ return res.success({
msg: this.stringService.checkUpdateTTL, msg: this.stringService.checkUpdateTTL,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "updateTTL")); SERVICE_NAME,
} "updateChecksTtl"
}; );
} }
export default CheckController; export default CheckController;
+3 -15
View File
@@ -1,16 +1,4 @@
const handleValidationError = (error, serviceName) => { import { createServerError } from "../utils/errorUtils.js";
error.status = 422;
error.service = serviceName;
error.message = error.details?.[0]?.message || error.message || "Validation Error";
return error;
};
const handleError = (error, serviceName, method, status = 500) => {
error.status === undefined ? (error.status = status) : null;
error.service === undefined ? (error.service = serviceName) : null;
error.method === undefined ? (error.method = method) : null;
return error;
};
const fetchMonitorCertificate = async (sslChecker, monitor) => { const fetchMonitorCertificate = async (sslChecker, monitor) => {
const monitorUrl = new URL(monitor.url); const monitorUrl = new URL(monitor.url);
@@ -18,9 +6,9 @@ const fetchMonitorCertificate = async (sslChecker, monitor) => {
const cert = await sslChecker(hostname); const cert = await sslChecker(hostname);
// Throw an error if no cert or if cert.validTo is not present // Throw an error if no cert or if cert.validTo is not present
if (cert?.validTo === null || cert?.validTo === undefined) { if (cert?.validTo === null || cert?.validTo === undefined) {
throw new Error("Certificate not found"); throw createServerError("Certificate not found");
} }
return cert; return cert;
}; };
export { handleValidationError, handleError, fetchMonitorCertificate }; export { fetchMonitorCertificate };
+25 -28
View File
@@ -1,6 +1,6 @@
import { handleError } from "./controllerUtils.js";
import v8 from "v8"; import v8 from "v8";
import os from "os"; import os from "os";
import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "diagnosticController"; const SERVICE_NAME = "diagnosticController";
@@ -17,20 +17,20 @@ class DiagnosticController {
this.getDbStats = this.getDbStats.bind(this); this.getDbStats = this.getDbStats.bind(this);
} }
async getMonitorsByTeamIdExecutionStats(req, res, next) { getMonitorsByTeamIdExecutionStats = asyncHandler(
try { async (req, res, next) => {
const data = await this.db.getMonitorsByTeamIdExecutionStats(req); const data = await this.db.getMonitorsByTeamIdExecutionStats(req);
return res.success({ return res.success({
msg: "OK", msg: "OK",
data, data,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMonitorsByTeamIdExecutionStats")); SERVICE_NAME,
} "getMonitorsByTeamIdExecutionStats"
} );
async getDbStats(req, res, next) { getDbStats = asyncHandler(
try { async (req, res, next) => {
const { methodName, args = [] } = req.body; const { methodName, args = [] } = req.body;
if (!methodName || !this.db[methodName]) { if (!methodName || !this.db[methodName]) {
return res.error({ return res.error({
@@ -48,13 +48,13 @@ class DiagnosticController {
msg: "OK", msg: "OK",
data: stats, data: stats,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getDbStats")); SERVICE_NAME,
} "getDbStats"
} );
async getCPUUsage() { getCPUUsage = asyncHandler(
try { async (req, res, next) => {
const startUsage = process.cpuUsage(); const startUsage = process.cpuUsage();
const timingPeriod = 1000; // measured in ms const timingPeriod = 1000; // measured in ms
await new Promise((resolve) => setTimeout(resolve, timingPeriod)); await new Promise((resolve) => setTimeout(resolve, timingPeriod));
@@ -65,16 +65,13 @@ class DiagnosticController {
usagePercentage: ((endUsage.user + endUsage.system) / 1000 / timingPeriod) * 100, usagePercentage: ((endUsage.user + endUsage.system) / 1000 / timingPeriod) * 100,
}; };
return cpuUsage; return cpuUsage;
} catch (error) { },
return { SERVICE_NAME,
userUsageMs: 0, "getCPUUsage"
systemUsageMs: 0, );
};
}
}
getSystemStats = async (req, res, next) => { getSystemStats = asyncHandler(
try { async (req, res, next) => {
// Memory Usage // Memory Usage
const totalMemory = os.totalmem(); const totalMemory = os.totalmem();
const freeMemory = os.freemem(); const freeMemory = os.freemem();
@@ -129,9 +126,9 @@ class DiagnosticController {
msg: "OK", msg: "OK",
data: diagnostics, data: diagnostics,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMemoryUsage")); SERVICE_NAME,
} "getSystemStats"
}; );
} }
export default DiagnosticController; export default DiagnosticController;
+35 -62
View File
@@ -3,10 +3,9 @@ import {
inviteBodyValidation, inviteBodyValidation,
inviteVerificationBodyValidation, inviteVerificationBodyValidation,
} from "../validation/joi.js"; } from "../validation/joi.js";
import logger from "../utils/logger.js";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import { handleError, handleValidationError } from "./controllerUtils.js";
import { getTokenFromHeaders } from "../utils/utils.js"; import { getTokenFromHeaders } from "../utils/utils.js";
import { asyncHandler, createServerError } from "../utils/errorUtils.js";
const SERVICE_NAME = "inviteController"; const SERVICE_NAME = "inviteController";
@@ -31,99 +30,73 @@ class InviteController {
* @returns {Object} The response object with a success status, a message indicating the sending of the invitation, and the invitation token. * @returns {Object} The response object with a success status, a message indicating the sending of the invitation, and the invitation token.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
getInviteToken = async (req, res, next) => { getInviteToken = asyncHandler(
try { async (req, res, next) => {
// Only admins can invite // Only admins can invite
const token = getTokenFromHeaders(req.headers); const token = getTokenFromHeaders(req.headers);
const { role, teamId } = jwt.decode(token); const { role, teamId } = jwt.decode(token);
req.body.teamId = teamId; req.body.teamId = teamId;
try { await inviteRoleValidation.validateAsync({ roles: role });
await inviteRoleValidation.validateAsync({ roles: role }); await inviteBodyValidation.validateAsync(req.body);
await inviteBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
const inviteToken = await this.db.requestInviteToken({ ...req.body }); const inviteToken = await this.db.requestInviteToken({ ...req.body });
return res.success({ return res.success({
msg: this.stringService.inviteIssued, msg: this.stringService.inviteIssued,
data: inviteToken, data: inviteToken,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "inviteController")); SERVICE_NAME,
} "getInviteToken"
}; );
sendInviteEmail = async (req, res, next) => { sendInviteEmail = asyncHandler(
try { async (req, res, next) => {
// Only admins can invite // Only admins can invite
const token = getTokenFromHeaders(req.headers); const token = getTokenFromHeaders(req.headers);
const { role, firstname, teamId } = jwt.decode(token); const { role, firstname, teamId } = jwt.decode(token);
req.body.teamId = teamId; req.body.teamId = teamId;
try { await inviteRoleValidation.validateAsync({ roles: role });
await inviteRoleValidation.validateAsync({ roles: role }); await inviteBodyValidation.validateAsync(req.body);
await inviteBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
const inviteToken = await this.db.requestInviteToken({ ...req.body }); const inviteToken = await this.db.requestInviteToken({ ...req.body });
const { clientHost } = this.settingsService.getSettings(); const { clientHost } = this.settingsService.getSettings();
try { const html = await this.emailService.buildEmail("employeeActivationTemplate", {
const html = await this.emailService.buildEmail("employeeActivationTemplate", { name: firstname,
name: firstname, link: `${clientHost}/register/${inviteToken.token}`,
link: `${clientHost}/register/${inviteToken.token}`, });
}); const result = await this.emailService.sendEmail(
const result = await this.emailService.sendEmail( req.body.email,
req.body.email, "Welcome to Uptime Monitor",
"Welcome to Uptime Monitor", html
html );
if (!result) {
throw createServerError(
"Failed to send invite e-mail... Please verify your settings."
); );
if (!result) {
return res.error({
msg: "Failed to send invite e-mail... Please verify your settings.",
});
}
} catch (error) {
logger.warn({
message: error.message,
service: SERVICE_NAME,
method: "sendInviteEmail",
stack: error.stack,
});
} }
return res.success({ return res.success({
msg: this.stringService.inviteIssued, msg: this.stringService.inviteIssued,
data: inviteToken, data: inviteToken,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "inviteController")); SERVICE_NAME,
} "sendInviteEmail"
}; );
inviteVerifyController = async (req, res, next) => { inviteVerifyController = asyncHandler(
try { async (req, res, next) => {
await inviteVerificationBodyValidation.validateAsync(req.body); await inviteVerificationBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const invite = await this.db.getInviteToken(req.body.token); const invite = await this.db.getInviteToken(req.body.token);
return res.success({ return res.success({
msg: this.stringService.inviteVerified, msg: this.stringService.inviteVerified,
data: invite, data: invite,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "inviteVerifyController")); SERVICE_NAME,
} "inviteVerifyController"
}; );
} }
export default InviteController; export default InviteController;
+8 -9
View File
@@ -1,23 +1,22 @@
import { handleError } from "./controllerUtils.js"; import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "JobQueueController"; const SERVICE_NAME = "LogController";
class LogController { class LogController {
constructor(logger) { constructor(logger) {
this.logger = logger; this.logger = logger;
} }
getLogs = async (req, res, next) => { getLogs = asyncHandler(
try { async (req, res, next) => {
const logs = await this.logger.getLogs(); const logs = await this.logger.getLogs();
res.success({ res.success({
msg: "Logs fetched successfully", msg: "Logs fetched successfully",
data: logs, data: logs,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getLogs")); SERVICE_NAME,
return; "getLogs"
} );
};
} }
export default LogController; export default LogController;
@@ -7,7 +7,7 @@ import {
getMaintenanceWindowsByTeamIdQueryValidation, getMaintenanceWindowsByTeamIdQueryValidation,
deleteMaintenanceWindowByIdParamValidation, deleteMaintenanceWindowByIdParamValidation,
} from "../validation/joi.js"; } from "../validation/joi.js";
import { handleValidationError, handleError } from "./controllerUtils.js"; import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "maintenanceWindowController"; const SERVICE_NAME = "maintenanceWindowController";
@@ -18,14 +18,10 @@ class MaintenanceWindowController {
this.stringService = stringService; this.stringService = stringService;
} }
createMaintenanceWindows = async (req, res, next) => { createMaintenanceWindows = asyncHandler(
try { async (req, res, next) => {
await createMaintenanceWindowBodyValidation.validateAsync(req.body); await createMaintenanceWindowBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { teamId } = req.user; const { teamId } = req.user;
const monitorIds = req.body.monitors; const monitorIds = req.body.monitors;
const dbTransactions = monitorIds.map((monitorId) => { const dbTransactions = monitorIds.map((monitorId) => {
@@ -44,39 +40,28 @@ class MaintenanceWindowController {
return res.success({ return res.success({
msg: this.stringService.maintenanceWindowCreate, msg: this.stringService.maintenanceWindowCreate,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "createMaintenanceWindow")); SERVICE_NAME,
} "createMaintenanceWindows"
}; );
getMaintenanceWindowById = async (req, res, next) => { getMaintenanceWindowById = asyncHandler(
try { async (req, res, next) => {
await getMaintenanceWindowByIdParamValidation.validateAsync(req.params); await getMaintenanceWindowByIdParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id); const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id);
return res.success({ return res.success({
msg: this.stringService.maintenanceWindowGetById, msg: this.stringService.maintenanceWindowGetById,
data: maintenanceWindow, data: maintenanceWindow,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMaintenanceWindowById")); SERVICE_NAME,
} "getMaintenanceWindowById"
}; );
getMaintenanceWindowsByTeamId = async (req, res, next) => { getMaintenanceWindowsByTeamId = asyncHandler(
try { async (req, res, next) => {
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query); await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { teamId } = req.user; const { teamId } = req.user;
const maintenanceWindows = await this.db.getMaintenanceWindowsByTeamId( const maintenanceWindows = await this.db.getMaintenanceWindowsByTeamId(
teamId, teamId,
@@ -87,20 +72,15 @@ class MaintenanceWindowController {
msg: this.stringService.maintenanceWindowGetByTeam, msg: this.stringService.maintenanceWindowGetByTeam,
data: maintenanceWindows, data: maintenanceWindows,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMaintenanceWindowsByUserId")); SERVICE_NAME,
} "getMaintenanceWindowsByTeamId"
}; );
getMaintenanceWindowsByMonitorId = async (req, res, next) => { getMaintenanceWindowsByMonitorId = asyncHandler(
try { async (req, res, next) => {
await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(req.params); await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId( const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId(
req.params.monitorId req.params.monitorId
); );
@@ -109,37 +89,27 @@ class MaintenanceWindowController {
msg: this.stringService.maintenanceWindowGetByUser, msg: this.stringService.maintenanceWindowGetByUser,
data: maintenanceWindows, data: maintenanceWindows,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMaintenanceWindowsByMonitorId")); SERVICE_NAME,
} "getMaintenanceWindowsByMonitorId"
}; );
deleteMaintenanceWindow = async (req, res, next) => { deleteMaintenanceWindow = asyncHandler(
try { async (req, res, next) => {
await deleteMaintenanceWindowByIdParamValidation.validateAsync(req.params); await deleteMaintenanceWindowByIdParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
await this.db.deleteMaintenanceWindowById(req.params.id); await this.db.deleteMaintenanceWindowById(req.params.id);
return res.success({ return res.success({
msg: this.stringService.maintenanceWindowDelete, msg: this.stringService.maintenanceWindowDelete,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteMaintenanceWindow")); SERVICE_NAME,
} "deleteMaintenanceWindow"
}; );
editMaintenanceWindow = async (req, res, next) => { editMaintenanceWindow = asyncHandler(
try { async (req, res, next) => {
await editMaintenanceWindowByIdParamValidation.validateAsync(req.params); await editMaintenanceWindowByIdParamValidation.validateAsync(req.params);
await editMaintenanceByIdWindowBodyValidation.validateAsync(req.body); await editMaintenanceByIdWindowBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const editedMaintenanceWindow = await this.db.editMaintenanceWindowById( const editedMaintenanceWindow = await this.db.editMaintenanceWindowById(
req.params.id, req.params.id,
req.body req.body
@@ -148,10 +118,10 @@ class MaintenanceWindowController {
msg: this.stringService.maintenanceWindowEdit, msg: this.stringService.maintenanceWindowEdit,
data: editedMaintenanceWindow, data: editedMaintenanceWindow,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "editMaintenanceWindow")); SERVICE_NAME,
} "editMaintenanceWindow"
}; );
} }
export default MaintenanceWindowController; export default MaintenanceWindowController;
+161 -228
View File
@@ -16,12 +16,12 @@ import {
} from "../validation/joi.js"; } from "../validation/joi.js";
import sslChecker from "ssl-checker"; import sslChecker from "ssl-checker";
import logger from "../utils/logger.js"; import logger from "../utils/logger.js";
import { handleError, handleValidationError } from "./controllerUtils.js";
import axios from "axios"; import axios from "axios";
import seedDb from "../db/mongo/utils/seedDb.js"; import seedDb from "../db/mongo/utils/seedDb.js";
const SERVICE_NAME = "monitorController";
import pkg from "papaparse"; import pkg from "papaparse";
import { asyncHandler, createServerError } from "../utils/errorUtils.js";
const SERVICE_NAME = "monitorController";
class MonitorController { class MonitorController {
constructor(db, settingsService, jobQueue, stringService, emailService) { constructor(db, settingsService, jobQueue, stringService, emailService) {
this.db = db; this.db = db;
@@ -40,17 +40,17 @@ class MonitorController {
* @returns {Promise<Express.Response>} * @returns {Promise<Express.Response>}
* @throws {Error} * @throws {Error}
*/ */
getAllMonitors = async (req, res, next) => { getAllMonitors = asyncHandler(
try { async (req, res, next) => {
const monitors = await this.db.getAllMonitors(); const monitors = await this.db.getAllMonitors();
return res.success({ return res.success({
msg: this.stringService.monitorGetAll, msg: this.stringService.monitorGetAll,
data: monitors, data: monitors,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getAllMonitors")); SERVICE_NAME,
} "getAllMonitors"
}; );
/** /**
* Returns all monitors with uptime stats for 1,7,30, and 90 days * Returns all monitors with uptime stats for 1,7,30, and 90 days
@@ -61,20 +61,20 @@ class MonitorController {
* @returns {Promise<Express.Response>} * @returns {Promise<Express.Response>}
* @throws {Error} * @throws {Error}
*/ */
getAllMonitorsWithUptimeStats = async (req, res, next) => { getAllMonitorsWithUptimeStats = asyncHandler(
try { async (req, res, next) => {
const monitors = await this.db.getAllMonitorsWithUptimeStats(); const monitors = await this.db.getAllMonitorsWithUptimeStats();
return res.success({ return res.success({
msg: this.stringService.monitorGetAll, msg: this.stringService.monitorGetAll,
data: monitors, data: monitors,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getAllMonitorsWithUptimeStats")); SERVICE_NAME,
} "getAllMonitorsWithUptimeStats"
}; );
getUptimeDetailsById = async (req, res, next) => { getUptimeDetailsById = asyncHandler(
try { async (req, res, next) => {
const { monitorId } = req.params; const { monitorId } = req.params;
const { dateRange, normalize } = req.query; const { dateRange, normalize } = req.query;
@@ -87,10 +87,10 @@ class MonitorController {
msg: this.stringService.monitorGetByIdSuccess, msg: this.stringService.monitorGetByIdSuccess,
data, data,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMonitorDetailsById")); SERVICE_NAME,
} "getUptimeDetailsById"
}; );
/** /**
* Returns monitor stats for monitor with matching ID * Returns monitor stats for monitor with matching ID
@@ -101,16 +101,11 @@ class MonitorController {
* @returns {Promise<Express.Response>} * @returns {Promise<Express.Response>}
* @throws {Error} * @throws {Error}
*/ */
getMonitorStatsById = async (req, res, next) => { getMonitorStatsById = asyncHandler(
try { async (req, res, next) => {
await getMonitorStatsByIdParamValidation.validateAsync(req.params); await getMonitorStatsByIdParamValidation.validateAsync(req.params);
await getMonitorStatsByIdQueryValidation.validateAsync(req.query); await getMonitorStatsByIdQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
let { limit, sortOrder, dateRange, numToDisplay, normalize } = req.query; let { limit, sortOrder, dateRange, numToDisplay, normalize } = req.query;
const { monitorId } = req.params; const { monitorId } = req.params;
@@ -126,10 +121,10 @@ class MonitorController {
msg: this.stringService.monitorStatsById, msg: this.stringService.monitorStatsById,
data: monitorStats, data: monitorStats,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMonitorStatsById")); SERVICE_NAME,
} "getMonitorStatsById"
}; );
/** /**
* Get hardware details for a specific monitor by ID * Get hardware details for a specific monitor by ID
@@ -140,15 +135,11 @@ class MonitorController {
* @returns {Promise<Express.Response>} * @returns {Promise<Express.Response>}
* @throws {Error} - Throws error if monitor not found or other database errors * @throws {Error} - Throws error if monitor not found or other database errors
*/ */
getHardwareDetailsById = async (req, res, next) => { getHardwareDetailsById = asyncHandler(
try { async (req, res, next) => {
await getHardwareDetailsByIdParamValidation.validateAsync(req.params); await getHardwareDetailsByIdParamValidation.validateAsync(req.params);
await getHardwareDetailsByIdQueryValidation.validateAsync(req.query); await getHardwareDetailsByIdQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId } = req.params; const { monitorId } = req.params;
const { dateRange } = req.query; const { dateRange } = req.query;
const monitor = await this.db.getHardwareDetailsById({ monitorId, dateRange }); const monitor = await this.db.getHardwareDetailsById({ monitorId, dateRange });
@@ -156,19 +147,15 @@ class MonitorController {
msg: this.stringService.monitorGetByIdSuccess, msg: this.stringService.monitorGetByIdSuccess,
data: monitor, data: monitor,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getHardwareDetailsById")); SERVICE_NAME,
} "getHardwareDetailsById"
}; );
getMonitorCertificate = async (req, res, next, fetchMonitorCertificate) => { getMonitorCertificate = asyncHandler(
try { async (req, res, next, fetchMonitorCertificate) => {
await getCertificateParamValidation.validateAsync(req.params); await getCertificateParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
}
try {
const { monitorId } = req.params; const { monitorId } = req.params;
const monitor = await this.db.getMonitorById(monitorId); const monitor = await this.db.getMonitorById(monitorId);
const certificate = await fetchMonitorCertificate(sslChecker, monitor); const certificate = await fetchMonitorCertificate(sslChecker, monitor);
@@ -179,10 +166,10 @@ class MonitorController {
certificateDate: new Date(certificate.validTo), certificateDate: new Date(certificate.validTo),
}, },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMonitorCertificate")); SERVICE_NAME,
} "getMonitorCertificate"
}; );
/** /**
* Retrieves a monitor by its ID. * Retrieves a monitor by its ID.
@@ -195,25 +182,20 @@ class MonitorController {
* @returns {Object} The response object with a success status, a message, and the retrieved monitor data. * @returns {Object} The response object with a success status, a message, and the retrieved monitor data.
* @throws {Error} If there is an error during the process, especially if the monitor is not found (404) or if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if the monitor is not found (404) or if there is a validation error (422).
*/ */
getMonitorById = async (req, res, next) => { getMonitorById = asyncHandler(
try { async (req, res, next) => {
await getMonitorByIdParamValidation.validateAsync(req.params); await getMonitorByIdParamValidation.validateAsync(req.params);
await getMonitorByIdQueryValidation.validateAsync(req.query); await getMonitorByIdQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const monitor = await this.db.getMonitorById(req.params.monitorId); const monitor = await this.db.getMonitorById(req.params.monitorId);
return res.success({ return res.success({
msg: this.stringService.monitorGetByIdSuccess, msg: this.stringService.monitorGetByIdSuccess,
data: monitor, data: monitor,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMonitorById")); SERVICE_NAME,
} "getMonitorById"
}; );
/** /**
* Creates a new monitor and adds it to the job queue. * Creates a new monitor and adds it to the job queue.
@@ -226,15 +208,10 @@ class MonitorController {
* @returns {Object} The response object with a success status, a message indicating the creation of the monitor, and the created monitor data. * @returns {Object} The response object with a success status, a message indicating the creation of the monitor, and the created monitor data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
createMonitor = async (req, res, next) => { createMonitor = asyncHandler(
try { async (req, res, next) => {
await createMonitorBodyValidation.validateAsync(req.body); await createMonitorBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { _id, teamId } = req.user; const { _id, teamId } = req.user;
const monitor = await this.db.createMonitor({ const monitor = await this.db.createMonitor({
body: req.body, body: req.body,
@@ -248,10 +225,10 @@ class MonitorController {
msg: this.stringService.monitorCreate, msg: this.stringService.monitorCreate,
data: monitor, data: monitor,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "createMonitor")); SERVICE_NAME,
} "createMonitor"
}; );
/** /**
* Creates bulk monitors and adds them to the job queue after parsing CSV. * Creates bulk monitors and adds them to the job queue after parsing CSV.
@@ -263,8 +240,8 @@ class MonitorController {
* @returns {Object} The response object with a success status and message. * @returns {Object} The response object with a success status and message.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
createBulkMonitors = async (req, res, next) => { createBulkMonitors = asyncHandler(
try { async (req, res, next) => {
const { parse } = pkg; const { parse } = pkg;
// validate the file // validate the file
@@ -310,51 +287,43 @@ class MonitorController {
return value; return value;
}, },
complete: async ({ data, errors }) => { complete: async ({ data, errors }) => {
try { if (errors.length > 0) {
if (errors.length > 0) { throw createServerError("Error parsing CSV");
throw new Error("Error parsing CSV");
}
if (!data || data.length === 0) {
throw new Error("CSV file contains no data rows");
}
const enrichedData = data.map((monitor) => ({
userId: _id,
teamId,
...monitor,
description: monitor.description || monitor.name || monitor.url,
name: monitor.name || monitor.url,
type: monitor.type || "http",
}));
await createMonitorsBodyValidation.validateAsync(enrichedData);
try {
const monitors = await this.db.createBulkMonitors(enrichedData);
await Promise.all(
monitors.map(async (monitor, index) => {
this.jobQueue.addJob(monitor._id, monitor);
})
);
return res.success({
msg: this.stringService.bulkMonitorsCreate,
data: monitors,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createBulkMonitors"));
}
} catch (error) {
next(handleError(error, SERVICE_NAME, "createBulkMonitors"));
} }
if (!data || data.length === 0) {
throw createServerError("CSV file contains no data rows");
}
const enrichedData = data.map((monitor) => ({
userId: _id,
teamId,
...monitor,
description: monitor.description || monitor.name || monitor.url,
name: monitor.name || monitor.url,
type: monitor.type || "http",
}));
await createMonitorsBodyValidation.validateAsync(enrichedData);
const monitors = await this.db.createBulkMonitors(enrichedData);
await Promise.all(
monitors.map(async (monitor, index) => {
this.jobQueue.addJob(monitor._id, monitor);
})
);
return res.success({
msg: this.stringService.bulkMonitorsCreate,
data: monitors,
});
}, },
}); });
} catch (error) { },
return next(handleError(error, SERVICE_NAME, "createBulkMonitors")); SERVICE_NAME,
} "createBulkMonitors"
}; );
/** /**
* Checks if the endpoint can be resolved * Checks if the endpoint can be resolved
* @async * @async
@@ -365,15 +334,9 @@ class MonitorController {
* @returns {Object} The response object with a success status, a message, and the resolution result. * @returns {Object} The response object with a success status, a message, and the resolution result.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
checkEndpointResolution = async (req, res, next) => { checkEndpointResolution = asyncHandler(
try { async (req, res, next) => {
await getMonitorURLByQueryValidation.validateAsync(req.query); await getMonitorURLByQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorURL } = req.query; const { monitorURL } = req.query;
const parsedUrl = new URL(monitorURL); const parsedUrl = new URL(monitorURL);
const response = await axios.get(parsedUrl, { const response = await axios.get(parsedUrl, {
@@ -384,10 +347,10 @@ class MonitorController {
status: response.status, status: response.status,
msg: response.statusText, msg: response.statusText,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "checkEndpointResolution")); SERVICE_NAME,
} "checkEndpointResolution"
}; );
/** /**
* Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications. * Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications.
@@ -400,24 +363,18 @@ class MonitorController {
* @returns {Object} The response object with a success status and a message indicating the deletion of the monitor. * @returns {Object} The response object with a success status and a message indicating the deletion of the monitor.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422) or an error in deleting associated records. * @throws {Error} If there is an error during the process, especially if there is a validation error (422) or an error in deleting associated records.
*/ */
deleteMonitor = async (req, res, next) => { deleteMonitor = asyncHandler(
try { async (req, res, next) => {
await getMonitorByIdParamValidation.validateAsync(req.params); await getMonitorByIdParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const monitorId = req.params.monitorId; const monitorId = req.params.monitorId;
const monitor = await this.db.deleteMonitor({ monitorId }); const monitor = await this.db.deleteMonitor({ monitorId });
await this.jobQueue.deleteJob(monitor); await this.jobQueue.deleteJob(monitor);
await this.db.deleteStatusPagesByMonitorId(monitor._id); await this.db.deleteStatusPagesByMonitorId(monitor._id);
return res.success({ msg: this.stringService.monitorDelete }); return res.success({ msg: this.stringService.monitorDelete });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteMonitor")); SERVICE_NAME,
} "deleteMonitor"
}; );
/** /**
* Deletes all monitors associated with a team. * Deletes all monitors associated with a team.
@@ -430,8 +387,8 @@ class MonitorController {
* @returns {Object} The response object with a success status and a message indicating the number of deleted monitors. * @returns {Object} The response object with a success status and a message indicating the number of deleted monitors.
* @throws {Error} If there is an error during the deletion process. * @throws {Error} If there is an error during the deletion process.
*/ */
deleteAllMonitors = async (req, res, next) => { deleteAllMonitors = asyncHandler(
try { async (req, res, next) => {
const { teamId } = req.user; const { teamId } = req.user;
const { monitors, deletedCount } = await this.db.deleteAllMonitors(teamId); const { monitors, deletedCount } = await this.db.deleteAllMonitors(teamId);
await Promise.all( await Promise.all(
@@ -442,7 +399,7 @@ class MonitorController {
await this.db.deletePageSpeedChecksByMonitorId(monitor._id); await this.db.deletePageSpeedChecksByMonitorId(monitor._id);
await this.db.deleteNotificationsByMonitorId(monitor._id); await this.db.deleteNotificationsByMonitorId(monitor._id);
} catch (error) { } catch (error) {
logger.error({ logger.warn({
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`, message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
service: SERVICE_NAME, service: SERVICE_NAME,
method: "deleteAllMonitors", method: "deleteAllMonitors",
@@ -452,10 +409,10 @@ class MonitorController {
}) })
); );
return res.success({ msg: `Deleted ${deletedCount} monitors` }); return res.success({ msg: `Deleted ${deletedCount} monitors` });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteAllMonitors")); SERVICE_NAME,
} "deleteAllMonitors"
}; );
/** /**
* Edits a monitor by its ID, updates its notifications, and updates its job in the job queue. * Edits a monitor by its ID, updates its notifications, and updates its job in the job queue.
@@ -470,16 +427,10 @@ class MonitorController {
* @returns {Object} The response object with a success status, a message indicating the editing of the monitor, and the edited monitor data. * @returns {Object} The response object with a success status, a message indicating the editing of the monitor, and the edited monitor data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422). * @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/ */
editMonitor = async (req, res, next) => { editMonitor = asyncHandler(
try { async (req, res, next) => {
await getMonitorByIdParamValidation.validateAsync(req.params); await getMonitorByIdParamValidation.validateAsync(req.params);
await editMonitorBodyValidation.validateAsync(req.body); await editMonitorBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId } = req.params; const { monitorId } = req.params;
const editedMonitor = await this.db.editMonitor(monitorId, req.body); const editedMonitor = await this.db.editMonitor(monitorId, req.body);
@@ -490,10 +441,10 @@ class MonitorController {
msg: this.stringService.monitorEdit, msg: this.stringService.monitorEdit,
data: editedMonitor, data: editedMonitor,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "editMonitor")); SERVICE_NAME,
} "editMonitor"
}; );
/** /**
* Pauses or resumes a monitor based on its current state. * Pauses or resumes a monitor based on its current state.
@@ -506,14 +457,10 @@ class MonitorController {
* @returns {Object} The response object with a success status, a message indicating the new state of the monitor, and the updated monitor data. * @returns {Object} The response object with a success status, a message indicating the new state of the monitor, and the updated monitor data.
* @throws {Error} If there is an error during the process. * @throws {Error} If there is an error during the process.
*/ */
pauseMonitor = async (req, res, next) => { pauseMonitor = asyncHandler(
try { async (req, res, next) => {
await pauseMonitorParamValidation.validateAsync(req.params); await pauseMonitorParamValidation.validateAsync(req.params);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
}
try {
const monitorId = req.params.monitorId; const monitorId = req.params.monitorId;
const monitor = await this.db.pauseMonitor({ monitorId }); const monitor = await this.db.pauseMonitor({ monitorId });
monitor.isActive === true monitor.isActive === true
@@ -526,10 +473,10 @@ class MonitorController {
: this.stringService.monitorPause, : this.stringService.monitorPause,
data: monitor, data: monitor,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "pauseMonitor")); SERVICE_NAME,
} "pauseMonitor"
}; );
/** /**
* Adds demo monitors for a team. * Adds demo monitors for a team.
@@ -542,8 +489,8 @@ class MonitorController {
* @returns {Object} The response object with a success status, a message indicating the addition of demo monitors, and the number of demo monitors added. * @returns {Object} The response object with a success status, a message indicating the addition of demo monitors, and the number of demo monitors added.
* @throws {Error} If there is an error during the process. * @throws {Error} If there is an error during the process.
*/ */
addDemoMonitors = async (req, res, next) => { addDemoMonitors = asyncHandler(
try { async (req, res, next) => {
const { _id, teamId } = req.user; const { _id, teamId } = req.user;
const demoMonitors = await this.db.addDemoMonitors(_id, teamId); const demoMonitors = await this.db.addDemoMonitors(_id, teamId);
await Promise.all( await Promise.all(
@@ -554,10 +501,10 @@ class MonitorController {
msg: this.stringService.monitorDemoAdded, msg: this.stringService.monitorDemoAdded,
data: demoMonitors.length, data: demoMonitors.length,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "addDemoMonitors")); SERVICE_NAME,
} "addDemoMonitors"
}; );
/** /**
* Sends a test email to verify email delivery functionality. * Sends a test email to verify email delivery functionality.
@@ -570,8 +517,8 @@ class MonitorController {
* @returns {Object} The response object with a success status and the email delivery message ID. * @returns {Object} The response object with a success status and the email delivery message ID.
* @throws {Error} If there is an error while sending the test email. * @throws {Error} If there is an error while sending the test email.
*/ */
sendTestEmail = async (req, res, next) => { sendTestEmail = asyncHandler(
try { async (req, res, next) => {
const { to } = req.body; const { to } = req.body;
if (!to || typeof to !== "string") { if (!to || typeof to !== "string") {
throw new Error(this.stringService.errorForValidEmailAddress); throw new Error(this.stringService.errorForValidEmailAddress);
@@ -584,29 +531,23 @@ class MonitorController {
const messageId = await this.emailService.sendEmail(to, subject, html); const messageId = await this.emailService.sendEmail(to, subject, html);
if (!messageId) { if (!messageId) {
return res.error({ throw createServerError("Failed to send test email.");
msg: "Failed to send test email.",
});
} }
return res.success({ return res.success({
msg: this.stringService.sendTestEmail, msg: this.stringService.sendTestEmail,
data: { messageId }, data: { messageId },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "sendTestEmail")); SERVICE_NAME,
} "sendTestEmail"
}; );
getMonitorsByTeamId = async (req, res, next) => { getMonitorsByTeamId = asyncHandler(
try { async (req, res, next) => {
await getMonitorsByTeamIdParamValidation.validateAsync(req.params); await getMonitorsByTeamIdParamValidation.validateAsync(req.params);
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query); await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
}
try {
let { limit, type, page, rowsPerPage, filter, field, order } = req.query; let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
const teamId = req.user.teamId; const teamId = req.user.teamId;
@@ -624,20 +565,16 @@ class MonitorController {
msg: this.stringService.monitorGetByTeamId, msg: this.stringService.monitorGetByTeamId,
data: monitors, data: monitors,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMonitorsByTeamId")); SERVICE_NAME,
} "getMonitorsByTeamId"
}; );
getMonitorsAndSummaryByTeamId = async (req, res, next) => { getMonitorsAndSummaryByTeamId = asyncHandler(
try { async (req, res, next) => {
await getMonitorsByTeamIdParamValidation.validateAsync(req.params); await getMonitorsByTeamIdParamValidation.validateAsync(req.params);
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query); await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
} catch (error) {
return next(handleValidationError(error, SERVICE_NAME));
}
try {
const { explain } = req; const { explain } = req;
const { type } = req.query; const { type } = req.query;
const { teamId } = req.user; const { teamId } = req.user;
@@ -651,20 +588,16 @@ class MonitorController {
msg: "OK", // TODO msg: "OK", // TODO
data: result, data: result,
}); });
} catch (error) { },
return next(handleError(error, SERVICE_NAME, "getMonitorsAndSummaryByTeamId")); SERVICE_NAME,
} "getMonitorsAndSummaryByTeamId"
}; );
getMonitorsWithChecksByTeamId = async (req, res, next) => { getMonitorsWithChecksByTeamId = asyncHandler(
try { async (req, res, next) => {
await getMonitorsByTeamIdParamValidation.validateAsync(req.params); await getMonitorsByTeamIdParamValidation.validateAsync(req.params);
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query); await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
} catch (error) {
return next(handleValidationError(error, SERVICE_NAME));
}
try {
const { explain } = req; const { explain } = req;
let { limit, type, page, rowsPerPage, filter, field, order } = req.query; let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
const { teamId } = req.user; const { teamId } = req.user;
@@ -684,23 +617,23 @@ class MonitorController {
msg: "OK", msg: "OK",
data: result, data: result,
}); });
} catch (error) { },
return next(handleError(error, SERVICE_NAME, "getMonitorsWithChecksByTeamId")); SERVICE_NAME,
} "getMonitorsWithChecksByTeamId"
}; );
seedDb = async (req, res, next) => { seedDb = asyncHandler(
try { async (req, res, next) => {
const { _id, teamId } = req.user; const { _id, teamId } = req.user;
await seedDb(_id, teamId); await seedDb(_id, teamId);
res.success({ msg: "Database seeded" }); res.success({ msg: "Database seeded" });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "seedDb")); SERVICE_NAME,
} "seedDb"
}; );
exportMonitorsToCSV = async (req, res, next) => { exportMonitorsToCSV = asyncHandler(
try { async (req, res, next) => {
const { teamId } = req.user; const { teamId } = req.user;
const monitors = await this.db.getMonitorsByTeamId({ teamId }); const monitors = await this.db.getMonitorsByTeamId({ teamId });
@@ -730,10 +663,10 @@ class MonitorController {
"Content-Disposition": "attachment; filename=monitors.csv", "Content-Disposition": "attachment; filename=monitors.csv",
}, },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "exportMonitorsToCSV")); SERVICE_NAME,
} "exportMonitorsToCSV"
}; );
} }
export default MonitorController; export default MonitorController;
+44 -68
View File
@@ -1,22 +1,8 @@
import { import { createNotificationBodyValidation } from "../validation/joi.js";
triggerNotificationBodyValidation, import { asyncHandler } from "../utils/errorUtils.js";
createNotificationBodyValidation,
} from "../validation/joi.js";
import { handleError, handleValidationError } from "./controllerUtils.js";
const SERVICE_NAME = "NotificationController"; const SERVICE_NAME = "NotificationController";
const NOTIFICATION_TYPES = {
WEBHOOK: "webhook",
TELEGRAM: "telegram",
};
const PLATFORMS = {
SLACK: "slack",
DISCORD: "discord",
TELEGRAM: "telegram",
};
class NotificationController { class NotificationController {
constructor({ notificationService, stringService, statusService, db }) { constructor({ notificationService, stringService, statusService, db }) {
this.notificationService = notificationService; this.notificationService = notificationService;
@@ -25,8 +11,8 @@ class NotificationController {
this.db = db; this.db = db;
} }
testNotification = async (req, res, next) => { testNotification = asyncHandler(
try { async (req, res, next) => {
const notification = req.body; const notification = req.body;
const success = await this.notificationService.sendTestNotification(notification); const success = await this.notificationService.sendTestNotification(notification);
@@ -41,22 +27,17 @@ class NotificationController {
return res.success({ return res.success({
msg: "Notification sent successfully", msg: "Notification sent successfully",
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "testWebhook")); SERVICE_NAME,
} "testNotification"
}; );
createNotification = async (req, res, next) => { createNotification = asyncHandler(
try { async (req, res, next) => {
await createNotificationBodyValidation.validateAsync(req.body, { await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false, abortEarly: false,
}); });
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const body = req.body; const body = req.body;
const { _id, teamId } = req.user; const { _id, teamId } = req.user;
body.userId = _id; body.userId = _id;
@@ -66,69 +47,64 @@ class NotificationController {
msg: "Notification created successfully", msg: "Notification created successfully",
data: notification, data: notification,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "createNotification")); SERVICE_NAME,
} "createNotification"
}; );
getNotificationsByTeamId = async (req, res, next) => { getNotificationsByTeamId = asyncHandler(
try { async (req, res, next) => {
const notifications = await this.db.getNotificationsByTeamId(req.user.teamId); const notifications = await this.db.getNotificationsByTeamId(req.user.teamId);
return res.success({ return res.success({
msg: "Notifications fetched successfully", msg: "Notifications fetched successfully",
data: notifications, data: notifications,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getNotificationsByTeamId")); SERVICE_NAME,
} "getNotificationsByTeamId"
}; );
deleteNotification = async (req, res, next) => { deleteNotification = asyncHandler(
try { async (req, res, next) => {
await this.db.deleteNotificationById(req.params.id); await this.db.deleteNotificationById(req.params.id);
return res.success({ return res.success({
msg: "Notification deleted successfully", msg: "Notification deleted successfully",
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteNotification")); SERVICE_NAME,
} "deleteNotification"
}; );
getNotificationById = async (req, res, next) => { getNotificationById = asyncHandler(
try { async (req, res, next) => {
const notification = await this.db.getNotificationById(req.params.id); const notification = await this.db.getNotificationById(req.params.id);
return res.success({ return res.success({
msg: "Notification fetched successfully", msg: "Notification fetched successfully",
data: notification, data: notification,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getNotificationById")); SERVICE_NAME,
} "getNotificationById"
}; );
editNotification = async (req, res, next) => { editNotification = asyncHandler(
try { async (req, res, next) => {
await createNotificationBodyValidation.validateAsync(req.body, { await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false, abortEarly: false,
}); });
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const notification = await this.db.editNotification(req.params.id, req.body); const notification = await this.db.editNotification(req.params.id, req.body);
return res.success({ return res.success({
msg: "Notification updated successfully", msg: "Notification updated successfully",
data: notification, data: notification,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "editNotification")); SERVICE_NAME,
} "editNotification"
}; );
testAllNotifications = async (req, res, next) => { testAllNotifications = asyncHandler(
try { async (req, res, next) => {
const { monitorId } = req.body; const { monitorId } = req.body;
const monitor = await this.db.getMonitorById(monitorId); const monitor = await this.db.getMonitorById(monitorId);
const notifications = monitor.notifications; const notifications = monitor.notifications;
@@ -138,10 +114,10 @@ class NotificationController {
return res.success({ return res.success({
msg: "All notifications sent successfully", msg: "All notifications sent successfully",
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "testAllNotifications")); SERVICE_NAME,
} "testAllNotifications"
}; );
} }
export default NotificationController; export default NotificationController;
+37 -43
View File
@@ -1,4 +1,4 @@
import { handleError } from "./controllerUtils.js"; import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "JobQueueController"; const SERVICE_NAME = "JobQueueController";
@@ -8,82 +8,76 @@ class JobQueueController {
this.stringService = stringService; this.stringService = stringService;
} }
getMetrics = async (req, res, next) => { getMetrics = asyncHandler(
try { async (req, res, next) => {
const metrics = await this.jobQueue.getMetrics(); const metrics = await this.jobQueue.getMetrics();
res.success({ res.success({
msg: this.stringService.queueGetMetrics, msg: this.stringService.queueGetMetrics,
data: metrics, data: metrics,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getMetrics")); SERVICE_NAME,
return; "getMetrics"
} );
};
getJobs = async (req, res, next) => { getJobs = asyncHandler(
try { async (req, res, next) => {
const jobs = await this.jobQueue.getJobs(); const jobs = await this.jobQueue.getJobs();
return res.success({ return res.success({
msg: this.stringService.queueGetJobs, msg: this.stringService.queueGetJobs,
data: jobs, data: jobs,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getJobs")); SERVICE_NAME,
return; "getJobs"
} );
};
getAllMetrics = async (req, res, next) => { getAllMetrics = asyncHandler(
try { async (req, res, next) => {
const jobs = await this.jobQueue.getJobs(); const jobs = await this.jobQueue.getJobs();
const metrics = await this.jobQueue.getMetrics(); const metrics = await this.jobQueue.getMetrics();
return res.success({ return res.success({
msg: this.stringService.queueGetAllMetrics, msg: this.stringService.queueGetAllMetrics,
data: { jobs, metrics }, data: { jobs, metrics },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getAllMetrics")); SERVICE_NAME,
return; "getAllMetrics"
} );
};
addJob = async (req, res, next) => { addJob = asyncHandler(
try { async (req, res, next) => {
await this.jobQueue.addJob(Math.random().toString(36).substring(7)); await this.jobQueue.addJob(Math.random().toString(36).substring(7));
return res.success({ return res.success({
msg: this.stringService.queueAddJob, msg: this.stringService.queueAddJob,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "addJob")); SERVICE_NAME,
return; "addJob"
} );
};
flushQueue = async (req, res, next) => { flushQueue = asyncHandler(
try { async (req, res, next) => {
const result = await this.jobQueue.flushQueues(); const result = await this.jobQueue.flushQueues();
return res.success({ return res.success({
msg: this.stringService.jobQueueFlush, msg: this.stringService.jobQueueFlush,
data: result, data: result,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "flushQueue")); SERVICE_NAME,
return; "flushQueue"
} );
};
checkQueueHealth = async (req, res, next) => { checkQueueHealth = asyncHandler(
try { async (req, res, next) => {
const stuckQueues = await this.jobQueue.checkQueueHealth(); const stuckQueues = await this.jobQueue.checkQueueHealth();
return res.success({ return res.success({
msg: this.stringService.queueHealthCheck, msg: this.stringService.queueHealthCheck,
data: stuckQueues, data: stuckQueues,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "checkQueueHealth")); SERVICE_NAME,
return; "checkQueueHealth"
} );
};
} }
export default JobQueueController; export default JobQueueController;
+33 -36
View File
@@ -1,6 +1,7 @@
import { updateAppSettingsBodyValidation } from "../validation/joi.js"; import { updateAppSettingsBodyValidation } from "../validation/joi.js";
import { handleValidationError, handleError } from "./controllerUtils.js";
import { sendTestEmailBodyValidation } from "../validation/joi.js"; import { sendTestEmailBodyValidation } from "../validation/joi.js";
import { asyncHandler, createServerError } from "../utils/errorUtils.js";
const SERVICE_NAME = "SettingsController"; const SERVICE_NAME = "SettingsController";
class SettingsController { class SettingsController {
@@ -32,45 +33,44 @@ class SettingsController {
return returnSettings; return returnSettings;
}; };
getAppSettings = async (req, res, next) => { getAppSettings = asyncHandler(
const dbSettings = await this.settingsService.getDBSettings(); async (req, res, next) => {
const dbSettings = await this.settingsService.getDBSettings();
const returnSettings = this.buildAppSettings(dbSettings); const returnSettings = this.buildAppSettings(dbSettings);
return res.success({ return res.success({
msg: this.stringService.getAppSettings, msg: this.stringService.getAppSettings,
data: returnSettings, data: returnSettings,
}); });
}; },
SERVICE_NAME,
"getAppSettings"
);
updateAppSettings = async (req, res, next) => { updateAppSettings = asyncHandler(
try { async (req, res, next) => {
await updateAppSettingsBodyValidation.validateAsync(req.body); await updateAppSettingsBodyValidation.validateAsync(req.body);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const updatedSettings = await this.db.updateAppSettings(req.body); const updatedSettings = await this.db.updateAppSettings(req.body);
const returnSettings = this.buildAppSettings(updatedSettings); const returnSettings = this.buildAppSettings(updatedSettings);
return res.success({ return res.success({
msg: this.stringService.updateAppSettings, msg: this.stringService.updateAppSettings,
data: returnSettings, data: returnSettings,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "updateAppSettings")); SERVICE_NAME,
} "updateAppSettings"
}; );
sendTestEmail = async (req, res, next) => { sendTestEmail = asyncHandler(
try { async (req, res, next) => {
await sendTestEmailBodyValidation.validateAsync(req.body); try {
} catch (error) { await sendTestEmailBodyValidation.validateAsync(req.body);
next(handleValidationError(error, SERVICE_NAME)); } catch (error) {
return; next(handleValidationError(error, SERVICE_NAME));
} return;
}
try {
const { const {
to, to,
systemEmailHost, systemEmailHost,
@@ -107,20 +107,17 @@ class SettingsController {
}); });
if (!messageId) { if (!messageId) {
return res.error({ throw createServerError("Failed to send test email.");
msg: "Failed to send test email.",
});
} }
return res.success({ return res.success({
msg: this.stringService.sendTestEmail, msg: this.stringService.sendTestEmail,
data: { messageId }, data: { messageId },
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME)); SERVICE_NAME,
return; "sendTestEmail"
} );
};
} }
export default SettingsController; export default SettingsController;
+37 -52
View File
@@ -1,10 +1,10 @@
import { handleError, handleValidationError } from "./controllerUtils.js";
import { import {
createStatusPageBodyValidation, createStatusPageBodyValidation,
getStatusPageParamValidation, getStatusPageParamValidation,
getStatusPageQueryValidation, getStatusPageQueryValidation,
imageValidation, imageValidation,
} from "../validation/joi.js"; } from "../validation/joi.js";
import { asyncHandler } from "../utils/errorUtils.js";
const SERVICE_NAME = "statusPageController"; const SERVICE_NAME = "statusPageController";
@@ -14,16 +14,11 @@ class StatusPageController {
this.stringService = stringService; this.stringService = stringService;
} }
createStatusPage = async (req, res, next) => { createStatusPage = asyncHandler(
try { async (req, res, next) => {
await createStatusPageBodyValidation.validateAsync(req.body); await createStatusPageBodyValidation.validateAsync(req.body);
await imageValidation.validateAsync(req.file); await imageValidation.validateAsync(req.file);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { _id, teamId } = req.user; const { _id, teamId } = req.user;
const statusPage = await this.db.createStatusPage({ const statusPage = await this.db.createStatusPage({
statusPageData: req.body, statusPageData: req.body,
@@ -35,21 +30,16 @@ class StatusPageController {
msg: this.stringService.statusPageCreate, msg: this.stringService.statusPageCreate,
data: statusPage, data: statusPage,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "createStatusPage")); SERVICE_NAME,
} "createStatusPage"
}; );
updateStatusPage = async (req, res, next) => { updateStatusPage = asyncHandler(
try { async (req, res, next) => {
await createStatusPageBodyValidation.validateAsync(req.body); await createStatusPageBodyValidation.validateAsync(req.body);
await imageValidation.validateAsync(req.file); await imageValidation.validateAsync(req.file);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.updateStatusPage(req.body, req.file); const statusPage = await this.db.updateStatusPage(req.body, req.file);
if (statusPage === null) { if (statusPage === null) {
const error = new Error(this.stringService.statusPageNotFound); const error = new Error(this.stringService.statusPageNotFound);
@@ -60,45 +50,40 @@ class StatusPageController {
msg: this.stringService.statusPageUpdate, msg: this.stringService.statusPageUpdate,
data: statusPage, data: statusPage,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "updateStatusPage")); SERVICE_NAME,
} "updateStatusPage"
}; );
getStatusPage = async (req, res, next) => { getStatusPage = asyncHandler(
try { async (req, res, next) => {
const statusPage = await this.db.getStatusPage(); const statusPage = await this.db.getStatusPage();
return res.success({ return res.success({
msg: this.stringService.statusPageByUrl, msg: this.stringService.statusPageByUrl,
data: statusPage, data: statusPage,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getStatusPage")); SERVICE_NAME,
} "getStatusPage"
}; );
getStatusPageByUrl = async (req, res, next) => { getStatusPageByUrl = asyncHandler(
try { async (req, res, next) => {
await getStatusPageParamValidation.validateAsync(req.params); await getStatusPageParamValidation.validateAsync(req.params);
await getStatusPageQueryValidation.validateAsync(req.query); await getStatusPageQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.getStatusPageByUrl(req.params.url, req.query.type); const statusPage = await this.db.getStatusPageByUrl(req.params.url, req.query.type);
return res.success({ return res.success({
msg: this.stringService.statusPageByUrl, msg: this.stringService.statusPageByUrl,
data: statusPage, data: statusPage,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getStatusPageByUrl")); SERVICE_NAME,
} "getStatusPageByUrl"
}; );
getStatusPagesByTeamId = async (req, res, next) => { getStatusPagesByTeamId = asyncHandler(
try { async (req, res, next) => {
const teamId = req.user.teamId; const teamId = req.user.teamId;
const statusPages = await this.db.getStatusPagesByTeamId(teamId); const statusPages = await this.db.getStatusPagesByTeamId(teamId);
@@ -106,21 +91,21 @@ class StatusPageController {
msg: this.stringService.statusPageByTeamId, msg: this.stringService.statusPageByTeamId,
data: statusPages, data: statusPages,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "getStatusPageByTeamId")); SERVICE_NAME,
} "getStatusPagesByTeamId"
}; );
deleteStatusPage = async (req, res, next) => { deleteStatusPage = asyncHandler(
try { async (req, res, next) => {
await this.db.deleteStatusPage(req.params.url); await this.db.deleteStatusPage(req.params.url);
return res.success({ return res.success({
msg: this.stringService.statusPageDelete, msg: this.stringService.statusPageDelete,
}); });
} catch (error) { },
next(handleError(error, SERVICE_NAME, "deleteStatusPage")); SERVICE_NAME,
} "deleteStatusPage"
}; );
} }
export default StatusPageController; export default StatusPageController;
+1
View File
@@ -3,6 +3,7 @@ import ServiceRegistry from "../service/serviceRegistry.js";
import StringService from "../service/stringService.js"; import StringService from "../service/stringService.js";
const handleErrors = (error, req, res, next) => { const handleErrors = (error, req, res, next) => {
console.log("ERROR", error);
const status = error.status || 500; const status = error.status || 500;
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME); const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
const message = error.message || stringService.authIncorrectPassword; const message = error.message || stringService.authIncorrectPassword;
+121
View File
@@ -0,0 +1,121 @@
class AppError extends Error {
constructor(message, status = 500, service = null, method = null, details = null) {
super(message);
this.status = status;
this.service = service;
this.method = method;
this.details = details;
Error.captureStackTrace(this, this.constructor);
}
}
export const createError = (
message,
status = 500,
service = null,
method = null,
details = null
) => {
return new AppError(message, status, service, method, details);
};
export const createValidationError = (
message,
details = null,
service = null,
method = null
) => {
return createError(message, 422, service, method, details);
};
export const createAuthError = (
message,
details = null,
service = null,
method = null
) => {
return createError(message, 401, service, method, details);
};
export const createForbiddenError = (
message,
details = null,
service = null,
method = null
) => {
return createError(message, 403, service, method, details);
};
export const createNotFoundError = (
message,
details = null,
service = null,
method = null
) => {
return createError(message, 404, service, method, details);
};
export const createConflictError = (
message,
details = null,
service = null,
method = null
) => {
return createError(message, 409, service, method, details);
};
export const createServerError = (
message,
details = null,
service = null,
method = null
) => {
return createError(message, 500, service, method, details);
};
export const asyncHandler = (fn, serviceName, methodName) => {
return async (req, res, next) => {
try {
await fn(req, res, next);
} catch (error) {
// Handle validation errors
if (error.isJoi || error.name === "ValidationError") {
const validationError = createValidationError(
error.message,
error.details,
serviceName,
methodName
);
return next(validationError);
}
if (error instanceof AppError) {
error.service = error.service || serviceName;
error.method = error.method || methodName;
return next(error);
}
if (error.code === "23505") {
const appError = createConflictError("Resource already exists", {
originalError: error.message,
code: error.code,
});
appError.service = serviceName;
appError.method = methodName;
return next(appError);
}
// For unknown errors, create a server error
const appError = createServerError(
error.message || "An unexpected error occurred",
{ originalError: error.message, stack: error.stack }
);
appError.service = serviceName;
appError.method = methodName;
appError.stack = error.stack; // Preserve original stack
return next(appError);
}
};
};