Refactor getMonitorCertificate to remove redundant check, move invalid cert handling to FE

This commit is contained in:
Alex Holliday committed 2024-10-24 11:36:56 +08:00
1 parent fb0ead89b7
commit 3f4678f5e6
1 file changed
+30 -37
+30 -37
View File
@@ -3,7 +3,7 @@ import {
getMonitorByIdQueryValidation, getMonitorByIdQueryValidation,
getMonitorsByTeamIdValidation, getMonitorsByTeamIdValidation,
createMonitorBodyValidation, createMonitorBodyValidation,
getMonitorURLByQueryValidation, getMonitorURLByQueryValidation,
editMonitorBodyValidation, editMonitorBodyValidation,
getMonitorsAndSummaryByTeamIdParamValidation, getMonitorsAndSummaryByTeamIdParamValidation,
getMonitorsAndSummaryByTeamIdQueryValidation, getMonitorsAndSummaryByTeamIdQueryValidation,
@@ -86,21 +86,14 @@ const getMonitorCertificate = async (req, res, next, fetchMonitorCertificate) =>
const { monitorId } = req.params; const { monitorId } = req.params;
const monitor = await req.db.getMonitorById(monitorId); const monitor = await req.db.getMonitorById(monitorId);
const certificate = await fetchMonitorCertificate(sslChecker, monitor); const certificate = await fetchMonitorCertificate(sslChecker, monitor);
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),
}, },
}); });
} else {
return res.status(200).json({
success: true,
msg: successMessages.MONITOR_CERTIFICATE,
data: { certificateDate: "N/A" },
});
}
} catch (error) { } catch (error) {
next(handleError(error, SERVICE_NAME, "getMonitorCertificate")); next(handleError(error, SERVICE_NAME, "getMonitorCertificate"));
} }
@@ -270,32 +263,32 @@ const createMonitor = 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 checkEndpointResolution = async (req, res, next) => { const checkEndpointResolution = async (req, res, next) => {
try { try {
await getMonitorURLByQueryValidation.validateAsync(req.query); await getMonitorURLByQueryValidation.validateAsync(req.query);
} catch (error) { } catch (error) {
next(handleValidationError(error, SERVICE_NAME)); next(handleValidationError(error, SERVICE_NAME));
return; return;
} }
try { try {
let { monitorURL } = req.query; let { monitorURL } = req.query;
monitorURL = new URL(monitorURL); monitorURL = new URL(monitorURL);
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
dns.resolve(monitorURL.hostname, (error) => { dns.resolve(monitorURL.hostname, (error) => {
if (error) { if (error) {
reject(error); reject(error);
} }
resolve(); resolve();
}); });
}); });
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
msg: `URL resolved successfully`, msg: `URL resolved successfully`,
}); });
} catch (error) { } catch (error) {
next(handleError(error, SERVICE_NAME, "checkEndpointResolution")); next(handleError(error, 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.
@@ -517,7 +510,7 @@ export {
getMonitorsAndSummaryByTeamId, getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId, getMonitorsByTeamId,
createMonitor, createMonitor,
checkEndpointResolution, checkEndpointResolution,
deleteMonitor, deleteMonitor,
deleteAllMonitors, deleteAllMonitors,
editMonitor, editMonitor,