From 3f4678f5e677fe37448ed19acf14913065cf5e5e Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 24 Oct 2024 11:36:56 +0800 Subject: [PATCH] Refactor getMonitorCertificate to remove redundant check, move invalid cert handling to FE --- Server/controllers/monitorController.js | 67 +++++++++++-------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 24761089f..ea74a2465 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -3,7 +3,7 @@ import { getMonitorByIdQueryValidation, getMonitorsByTeamIdValidation, createMonitorBodyValidation, - getMonitorURLByQueryValidation, + getMonitorURLByQueryValidation, editMonitorBodyValidation, getMonitorsAndSummaryByTeamIdParamValidation, getMonitorsAndSummaryByTeamIdQueryValidation, @@ -86,21 +86,14 @@ const getMonitorCertificate = async (req, res, next, fetchMonitorCertificate) => const { monitorId } = req.params; const monitor = await req.db.getMonitorById(monitorId); const certificate = await fetchMonitorCertificate(sslChecker, monitor); - if (certificate && certificate.validTo) { - return res.status(200).json({ - success: true, - msg: successMessages.MONITOR_CERTIFICATE, - data: { - certificateDate: new Date(certificate.validTo), - }, - }); - } else { - return res.status(200).json({ - success: true, - msg: successMessages.MONITOR_CERTIFICATE, - data: { certificateDate: "N/A" }, - }); - } + + return res.status(200).json({ + success: true, + msg: successMessages.MONITOR_CERTIFICATE, + data: { + certificateDate: new Date(certificate.validTo), + }, + }); } catch (error) { 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). */ const checkEndpointResolution = async (req, res, next) => { - try { + try { await getMonitorURLByQueryValidation.validateAsync(req.query); } catch (error) { next(handleValidationError(error, SERVICE_NAME)); return; } - try { - let { monitorURL } = req.query; - monitorURL = new URL(monitorURL); - await new Promise((resolve, reject) => { - dns.resolve(monitorURL.hostname, (error) => { - if (error) { - reject(error); - } - resolve(); - }); - }); - return res.status(200).json({ - success: true, - msg: `URL resolved successfully`, - }); - } catch (error) { - next(handleError(error, SERVICE_NAME, "checkEndpointResolution")); - } -} + try { + let { monitorURL } = req.query; + monitorURL = new URL(monitorURL); + await new Promise((resolve, reject) => { + dns.resolve(monitorURL.hostname, (error) => { + if (error) { + reject(error); + } + resolve(); + }); + }); + return res.status(200).json({ + success: true, + msg: `URL resolved successfully`, + }); + } catch (error) { + next(handleError(error, SERVICE_NAME, "checkEndpointResolution")); + } +}; /** * Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications. @@ -517,7 +510,7 @@ export { getMonitorsAndSummaryByTeamId, getMonitorsByTeamId, createMonitor, - checkEndpointResolution, + checkEndpointResolution, deleteMonitor, deleteAllMonitors, editMonitor,