Use new fetchMonitorCertificate method, prettier formatting
This commit is contained in:
1 parent
5f6e0631c6
commit
81d3b8dcb5
1 file changed
+302
-307
@@ -1,25 +1,28 @@
|
|||||||
import {
|
import {
|
||||||
getMonitorByIdParamValidation,
|
getMonitorByIdParamValidation,
|
||||||
getMonitorByIdQueryValidation,
|
getMonitorByIdQueryValidation,
|
||||||
getMonitorsByTeamIdValidation,
|
getMonitorsByTeamIdValidation,
|
||||||
createMonitorBodyValidation,
|
createMonitorBodyValidation,
|
||||||
editMonitorBodyValidation,
|
editMonitorBodyValidation,
|
||||||
getMonitorsAndSummaryByTeamIdParamValidation,
|
getMonitorsAndSummaryByTeamIdParamValidation,
|
||||||
getMonitorsAndSummaryByTeamIdQueryValidation,
|
getMonitorsAndSummaryByTeamIdQueryValidation,
|
||||||
getMonitorsByTeamIdQueryValidation,
|
getMonitorsByTeamIdQueryValidation,
|
||||||
pauseMonitorParamValidation,
|
pauseMonitorParamValidation,
|
||||||
getMonitorStatsByIdParamValidation,
|
getMonitorStatsByIdParamValidation,
|
||||||
getMonitorStatsByIdQueryValidation,
|
getMonitorStatsByIdQueryValidation,
|
||||||
getCertificateParamValidation,
|
getCertificateParamValidation,
|
||||||
} from "../validation/joi.js";
|
} from "../validation/joi.js";
|
||||||
|
|
||||||
import sslChecker from "ssl-checker";
|
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
import { errorMessages, successMessages } from "../utils/messages.js";
|
import { errorMessages, successMessages } from "../utils/messages.js";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { getTokenFromHeaders } from "../utils/utils.js";
|
import { getTokenFromHeaders } from "../utils/utils.js";
|
||||||
import logger from "../utils/logger.js";
|
import logger from "../utils/logger.js";
|
||||||
import { handleError, handleValidationError } from "./controllerUtils.js";
|
import {
|
||||||
|
handleError,
|
||||||
|
handleValidationError,
|
||||||
|
fetchMonitorCertificate,
|
||||||
|
} from "./controllerUtils.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all monitors
|
* Returns all monitors
|
||||||
@@ -31,16 +34,16 @@ import { handleError, handleValidationError } from "./controllerUtils.js";
|
|||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const getAllMonitors = async (req, res, next) => {
|
const getAllMonitors = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const monitors = await req.db.getAllMonitors();
|
const monitors = await req.db.getAllMonitors();
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_GET_ALL,
|
msg: successMessages.MONITOR_GET_ALL,
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "getAllMonitors"));
|
next(handleError(error, SERVICE_NAME, "getAllMonitors"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,56 +56,55 @@ const getAllMonitors = async (req, res, next) => {
|
|||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const getMonitorStatsById = async (req, res, next) => {
|
const getMonitorStatsById = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMonitorStatsByIdParamValidation.validateAsync(req.params);
|
await getMonitorStatsByIdParamValidation.validateAsync(req.params);
|
||||||
await getMonitorStatsByIdQueryValidation.validateAsync(req.query);
|
await getMonitorStatsByIdQueryValidation.validateAsync(req.query);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitorStats = await req.db.getMonitorStatsById(req);
|
const monitorStats = await req.db.getMonitorStatsById(req);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_STATS_BY_ID,
|
msg: successMessages.MONITOR_STATS_BY_ID,
|
||||||
data: monitorStats,
|
data: monitorStats,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "getMonitorStatsById"));
|
next(handleError(error, SERVICE_NAME, "getMonitorStatsById"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMonitorCertificate = async (req, res, next) => {
|
const getMonitorCertificate = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getCertificateParamValidation.validateAsync(req.params);
|
await getCertificateParamValidation.validateAsync(req.params);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { monitorId } = req.params;
|
const { monitorId } = req.params;
|
||||||
const monitor = await req.db.getMonitorById(monitorId);
|
const monitor = await req.db.getMonitorById(monitorId);
|
||||||
const monitorUrl = new URL(monitor.url);
|
const certificate = await fetchMonitorCertificate(monitor);
|
||||||
const certificate = await sslChecker(monitorUrl.hostname);
|
if (certificate && certificate.validTo) {
|
||||||
if (certificate && certificate.validTo) {
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: successMessages.MONITOR_CERTIFICATE,
|
||||||
msg: successMessages.MONITOR_CERTIFICATE,
|
data: {
|
||||||
data: {
|
certificateDate: new Date(certificate.validTo),
|
||||||
certificateDate: new Date(certificate.validTo).toLocaleDateString(),
|
},
|
||||||
},
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: successMessages.MONITOR_CERTIFICATE,
|
||||||
msg: successMessages.MONITOR_CERTIFICATE,
|
data: { certificateDate: "N/A" },
|
||||||
data: { certificateDate: "N/A" },
|
});
|
||||||
});
|
}
|
||||||
}
|
} catch (error) {
|
||||||
} catch (error) {
|
next(handleError(error, SERVICE_NAME, "getMonitorCertificate"));
|
||||||
next(handleError(error, SERVICE_NAME, "getMonitorCertificate"));
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,30 +119,30 @@ const getMonitorCertificate = async (req, res, next) => {
|
|||||||
* @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).
|
||||||
*/
|
*/
|
||||||
const getMonitorById = async (req, res, next) => {
|
const getMonitorById = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMonitorByIdParamValidation.validateAsync(req.params);
|
await getMonitorByIdParamValidation.validateAsync(req.params);
|
||||||
await getMonitorByIdQueryValidation.validateAsync(req.query);
|
await getMonitorByIdQueryValidation.validateAsync(req.query);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await req.db.getMonitorById(req.params.monitorId);
|
const monitor = await req.db.getMonitorById(req.params.monitorId);
|
||||||
if (!monitor) {
|
if (!monitor) {
|
||||||
const error = new Error(errorMessages.MONITOR_GET_BY_ID);
|
const error = new Error(errorMessages.MONITOR_GET_BY_ID);
|
||||||
error.status = 404;
|
error.status = 404;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "getMonitorById"));
|
next(handleError(error, SERVICE_NAME, "getMonitorById"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,31 +159,26 @@ const getMonitorById = async (req, res, next) => {
|
|||||||
* @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).
|
||||||
*/
|
*/
|
||||||
const getMonitorsAndSummaryByTeamId = async (req, res, next) => {
|
const getMonitorsAndSummaryByTeamId = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMonitorsAndSummaryByTeamIdParamValidation.validateAsync(
|
await getMonitorsAndSummaryByTeamIdParamValidation.validateAsync(req.params);
|
||||||
req.params
|
await getMonitorsAndSummaryByTeamIdQueryValidation.validateAsync(req.query);
|
||||||
);
|
} catch (error) {
|
||||||
await getMonitorsAndSummaryByTeamIdQueryValidation.validateAsync(req.query);
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
} catch (error) {
|
return;
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { teamId } = req.params;
|
const { teamId } = req.params;
|
||||||
const { type } = req.query;
|
const { type } = req.query;
|
||||||
const monitorsSummary = await req.db.getMonitorsAndSummaryByTeamId(
|
const monitorsSummary = await req.db.getMonitorsAndSummaryByTeamId(teamId, type);
|
||||||
teamId,
|
return res.status(200).json({
|
||||||
type
|
success: true,
|
||||||
);
|
msg: successMessages.MONITOR_GET_BY_USER_ID(teamId),
|
||||||
return res.status(200).json({
|
data: monitorsSummary,
|
||||||
success: true,
|
});
|
||||||
msg: successMessages.MONITOR_GET_BY_USER_ID(teamId),
|
} catch (error) {
|
||||||
data: monitorsSummary,
|
next(handleError(error, SERVICE_NAME, "getMonitorsAndSummaryByTeamId"));
|
||||||
});
|
}
|
||||||
} catch (error) {
|
|
||||||
next(handleError(error, SERVICE_NAME, "getMonitorsAndSummaryByTeamId"));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -197,26 +194,26 @@ const getMonitorsAndSummaryByTeamId = async (req, res, next) => {
|
|||||||
* @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).
|
||||||
*/
|
*/
|
||||||
const getMonitorsByTeamId = async (req, res, next) => {
|
const getMonitorsByTeamId = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMonitorsByTeamIdValidation.validateAsync(req.params);
|
await getMonitorsByTeamIdValidation.validateAsync(req.params);
|
||||||
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
|
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const teamId = req.params.teamId;
|
const teamId = req.params.teamId;
|
||||||
const monitors = await req.db.getMonitorsByTeamId(req, res);
|
const monitors = await req.db.getMonitorsByTeamId(req, res);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_GET_BY_USER_ID(teamId),
|
msg: successMessages.MONITOR_GET_BY_USER_ID(teamId),
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "getMonitorsByTeamId"));
|
next(handleError(error, SERVICE_NAME, "getMonitorsByTeamId"));
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -231,36 +228,36 @@ const getMonitorsByTeamId = async (req, res, next) => {
|
|||||||
* @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).
|
||||||
*/
|
*/
|
||||||
const createMonitor = async (req, res, next) => {
|
const createMonitor = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await createMonitorBodyValidation.validateAsync(req.body);
|
await createMonitorBodyValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const notifications = req.body.notifications;
|
const notifications = req.body.notifications;
|
||||||
const monitor = await req.db.createMonitor(req, res);
|
const monitor = await req.db.createMonitor(req, res);
|
||||||
|
|
||||||
if (notifications && notifications.length !== 0) {
|
if (notifications && notifications.length !== 0) {
|
||||||
monitor.notifications = await Promise.all(
|
monitor.notifications = await Promise.all(
|
||||||
notifications.map(async (notification) => {
|
notifications.map(async (notification) => {
|
||||||
notification.monitorId = monitor._id;
|
notification.monitorId = monitor._id;
|
||||||
await req.db.createNotification(notification);
|
await req.db.createNotification(notification);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
await monitor.save();
|
await monitor.save();
|
||||||
}
|
}
|
||||||
// Add monitor to job queue
|
// Add monitor to job queue
|
||||||
req.jobQueue.addJob(monitor._id, monitor);
|
req.jobQueue.addJob(monitor._id, monitor);
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_CREATE,
|
msg: successMessages.MONITOR_CREATE,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "createMonitor"));
|
next(handleError(error, SERVICE_NAME, "createMonitor"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -275,37 +272,35 @@ const createMonitor = async (req, res, next) => {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
const deleteMonitor = async (req, res, next) => {
|
const deleteMonitor = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMonitorByIdParamValidation.validateAsync(req.params);
|
await getMonitorByIdParamValidation.validateAsync(req.params);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await req.db.deleteMonitor(req, res, next);
|
const monitor = await req.db.deleteMonitor(req, res, next);
|
||||||
// Delete associated checks,alerts,and notifications
|
// Delete associated checks,alerts,and notifications
|
||||||
try {
|
try {
|
||||||
await req.jobQueue.deleteJob(monitor);
|
await req.jobQueue.deleteJob(monitor);
|
||||||
await req.db.deleteChecks(monitor._id);
|
await req.db.deleteChecks(monitor._id);
|
||||||
await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
|
await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
|
||||||
await req.db.deleteNotificationsByMonitorId(monitor._id);
|
await req.db.deleteNotificationsByMonitorId(monitor._id);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
||||||
{
|
{
|
||||||
method: "deleteMonitor",
|
method: "deleteMonitor",
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
error: error.message,
|
error: error.message,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return res
|
return res.status(200).json({ success: true, msg: successMessages.MONITOR_DELETE });
|
||||||
.status(200)
|
} catch (error) {
|
||||||
.json({ success: true, msg: successMessages.MONITOR_DELETE });
|
next(handleError(error, SERVICE_NAME, "deleteMonitor"));
|
||||||
} catch (error) {
|
}
|
||||||
next(handleError(error, SERVICE_NAME, "deleteMonitor"));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -320,34 +315,34 @@ const deleteMonitor = async (req, res, next) => {
|
|||||||
* @throws {Error} If there is an error during the deletion process.
|
* @throws {Error} If there is an error during the deletion process.
|
||||||
*/
|
*/
|
||||||
const deleteAllMonitors = async (req, res, next) => {
|
const deleteAllMonitors = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const token = getTokenFromHeaders(req.headers);
|
const token = getTokenFromHeaders(req.headers);
|
||||||
const { jwtSecret } = req.settingsService.getSettings();
|
const { jwtSecret } = req.settingsService.getSettings();
|
||||||
const { teamId } = jwt.verify(token, jwtSecret);
|
const { teamId } = jwt.verify(token, jwtSecret);
|
||||||
const { monitors, deletedCount } = await req.db.deleteAllMonitors(teamId);
|
const { monitors, deletedCount } = await req.db.deleteAllMonitors(teamId);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
monitors.map(async (monitor) => {
|
monitors.map(async (monitor) => {
|
||||||
try {
|
try {
|
||||||
await req.jobQueue.deleteJob(monitor);
|
await req.jobQueue.deleteJob(monitor);
|
||||||
await req.db.deleteChecks(monitor._id);
|
await req.db.deleteChecks(monitor._id);
|
||||||
await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
|
await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
|
||||||
await req.db.deleteNotificationsByMonitorId(monitor._id);
|
await req.db.deleteNotificationsByMonitorId(monitor._id);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
`Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
||||||
{
|
{
|
||||||
method: "deleteAllMonitors",
|
method: "deleteAllMonitors",
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return res
|
return res
|
||||||
.status(200)
|
.status(200)
|
||||||
.json({ success: true, msg: `Deleted ${deletedCount} monitors` });
|
.json({ success: true, msg: `Deleted ${deletedCount} monitors` });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "deleteAllMonitors"));
|
next(handleError(error, SERVICE_NAME, "deleteAllMonitors"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -364,46 +359,46 @@ const deleteAllMonitors = async (req, res, next) => {
|
|||||||
* @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).
|
||||||
*/
|
*/
|
||||||
const editMonitor = async (req, res, next) => {
|
const editMonitor = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getMonitorByIdParamValidation.validateAsync(req.params);
|
await getMonitorByIdParamValidation.validateAsync(req.params);
|
||||||
await editMonitorBodyValidation.validateAsync(req.body);
|
await editMonitorBodyValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { monitorId } = req.params;
|
const { monitorId } = req.params;
|
||||||
const monitorBeforeEdit = await req.db.getMonitorById(monitorId);
|
const monitorBeforeEdit = await req.db.getMonitorById(monitorId);
|
||||||
|
|
||||||
// Get notifications from the request body
|
// Get notifications from the request body
|
||||||
const notifications = req.body.notifications;
|
const notifications = req.body.notifications;
|
||||||
|
|
||||||
const editedMonitor = await req.db.editMonitor(monitorId, req.body);
|
const editedMonitor = await req.db.editMonitor(monitorId, req.body);
|
||||||
|
|
||||||
await req.db.deleteNotificationsByMonitorId(editedMonitor._id);
|
await req.db.deleteNotificationsByMonitorId(editedMonitor._id);
|
||||||
|
|
||||||
if (notifications && notifications.length !== 0) {
|
if (notifications && notifications.length !== 0) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
notifications.map(async (notification) => {
|
notifications.map(async (notification) => {
|
||||||
notification.monitorId = editedMonitor._id;
|
notification.monitorId = editedMonitor._id;
|
||||||
await req.db.createNotification(notification);
|
await req.db.createNotification(notification);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the old job(editedMonitor has the same ID as the old monitor)
|
// Delete the old job(editedMonitor has the same ID as the old monitor)
|
||||||
await req.jobQueue.deleteJob(monitorBeforeEdit);
|
await req.jobQueue.deleteJob(monitorBeforeEdit);
|
||||||
// Add the new job back to the queue
|
// Add the new job back to the queue
|
||||||
await req.jobQueue.addJob(editedMonitor._id, editedMonitor);
|
await req.jobQueue.addJob(editedMonitor._id, editedMonitor);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_EDIT,
|
msg: successMessages.MONITOR_EDIT,
|
||||||
data: editedMonitor,
|
data: editedMonitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "editMonitor"));
|
next(handleError(error, SERVICE_NAME, "editMonitor"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -418,32 +413,32 @@ const editMonitor = async (req, res, next) => {
|
|||||||
* @throws {Error} If there is an error during the process.
|
* @throws {Error} If there is an error during the process.
|
||||||
*/
|
*/
|
||||||
const pauseMonitor = async (req, res, next) => {
|
const pauseMonitor = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await pauseMonitorParamValidation.validateAsync(req.params);
|
await pauseMonitorParamValidation.validateAsync(req.params);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await req.db.getMonitorById(req.params.monitorId);
|
const monitor = await req.db.getMonitorById(req.params.monitorId);
|
||||||
if (monitor.isActive) {
|
if (monitor.isActive) {
|
||||||
await req.jobQueue.deleteJob(monitor);
|
await req.jobQueue.deleteJob(monitor);
|
||||||
} else {
|
} else {
|
||||||
await req.jobQueue.addJob(monitor._id, monitor);
|
await req.jobQueue.addJob(monitor._id, monitor);
|
||||||
}
|
}
|
||||||
monitor.isActive = !monitor.isActive;
|
monitor.isActive = !monitor.isActive;
|
||||||
monitor.status = undefined;
|
monitor.status = undefined;
|
||||||
monitor.save();
|
monitor.save();
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: monitor.isActive
|
msg: monitor.isActive
|
||||||
? successMessages.MONITOR_RESUME
|
? successMessages.MONITOR_RESUME
|
||||||
: successMessages.MONITOR_PAUSE,
|
: successMessages.MONITOR_PAUSE,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "pauseMonitor"));
|
next(handleError(error, SERVICE_NAME, "pauseMonitor"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -458,36 +453,36 @@ const pauseMonitor = async (req, res, next) => {
|
|||||||
* @throws {Error} If there is an error during the process.
|
* @throws {Error} If there is an error during the process.
|
||||||
*/
|
*/
|
||||||
const addDemoMonitors = async (req, res, next) => {
|
const addDemoMonitors = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const token = getTokenFromHeaders(req.headers);
|
const token = getTokenFromHeaders(req.headers);
|
||||||
const { jwtSecret } = req.settingsService.getSettings();
|
const { jwtSecret } = req.settingsService.getSettings();
|
||||||
const { _id, teamId } = jwt.verify(token, jwtSecret);
|
const { _id, teamId } = jwt.verify(token, jwtSecret);
|
||||||
const demoMonitors = await req.db.addDemoMonitors(_id, teamId);
|
const demoMonitors = await req.db.addDemoMonitors(_id, teamId);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
demoMonitors.map((monitor) => req.jobQueue.addJob(monitor._id, monitor))
|
demoMonitors.map((monitor) => req.jobQueue.addJob(monitor._id, monitor))
|
||||||
);
|
);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_DEMO_ADDED,
|
msg: successMessages.MONITOR_DEMO_ADDED,
|
||||||
data: demoMonitors.length,
|
data: demoMonitors.length,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "addDemoMonitors"));
|
next(handleError(error, SERVICE_NAME, "addDemoMonitors"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorCertificate,
|
getMonitorCertificate,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
getMonitorsAndSummaryByTeamId,
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
deleteAllMonitors,
|
deleteAllMonitors,
|
||||||
editMonitor,
|
editMonitor,
|
||||||
pauseMonitor,
|
pauseMonitor,
|
||||||
addDemoMonitors,
|
addDemoMonitors,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user