diff --git a/Client/src/App.jsx b/Client/src/App.jsx index 62c95c3db..cc9de1304 100644 --- a/Client/src/App.jsx +++ b/Client/src/App.jsx @@ -110,7 +110,7 @@ function App() { element={} /> } /> { }; const CreateMaintenance = () => { + const { maintenanceWindowId } = useParams(); const navigate = useNavigate(); const theme = useTheme(); const { user, authToken } = useSelector((state) => state.auth); const [monitors, setMonitors] = useState([]); const [search, setSearch] = useState(""); + const [isLoading, setIsLoading] = useState(false); const [form, setForm] = useState({ repeat: "none", startDate: dayjs(), @@ -93,13 +96,48 @@ const CreateMaintenance = () => { useEffect(() => { const fetchMonitors = async () => { - const response = await networkService.getMonitorsByTeamId({ - authToken: authToken, - teamId: user.teamId, - limit: -1, - types: ["http", "ping", "pagespeed"], - }); - setMonitors(response.data.data.monitors); + setIsLoading(true); + try { + const response = await networkService.getMonitorsByTeamId({ + authToken: authToken, + teamId: user.teamId, + limit: -1, + types: ["http", "ping", "pagespeed"], + }); + setMonitors(response.data.data.monitors); + + if (maintenanceWindowId === undefined) { + return; + } + + const maintenanceWindow = await networkService.getMaintenanceWindowById( + { + authToken: authToken, + maintenanceWindowId: maintenanceWindowId, + } + ); + logger.info(maintenanceWindow); + // const { name, start, end, repeat, monitors } = + // maintenanceWindow.data.data.maintenanceWindow; + // const startDate = dayjs(start); + // const startTime = dayjs(start); + // const duration = dayjs(end).diff(dayjs(start), "seconds"); + // const durationUnit = "seconds"; + // setForm({ + // name, + // startDate, + // startTime, + // duration, + // durationUnit, + // repeat, + // monitors, + // }); + } catch (error) { + createToast({ body: "Failed to fetch data" }); + logger.error("Failed to fetch monitors", error); + } finally { + setIsLoading(false); + } }; fetchMonitors(); }, [authToken, user]); @@ -190,7 +228,6 @@ const CreateMaintenance = () => { repeat, }; - console.log(submit); if (repeat === 0) { submit.expiry = end; } diff --git a/Client/src/Utils/Logger.js b/Client/src/Utils/Logger.js index 79b2ed4f9..ec50712aa 100644 --- a/Client/src/Utils/Logger.js +++ b/Client/src/Utils/Logger.js @@ -14,25 +14,34 @@ class Logger { const NO_OP = () => {}; if (logLevel === "none") { + this.info = NO_OP; this.error = NO_OP; this.warn = NO_OP; this.log = NO_OP; return; } - this.error = console.error.bind(console); - if (logLevel === "error") { + this.info = NO_OP; this.warn = NO_OP; this.log = NO_OP; return; } - this.warn = console.warn.bind(console); if (logLevel === "warn") { + this.info = NO_OP; this.log = NO_OP; return; } + + if (logLevel === "info") { + this.log = NO_OP; + return; + } + + this.error = console.error.bind(console); + this.warn = console.warn.bind(console); + this.info = console.info.bind(console); this.log = console.log.bind(console); } diff --git a/Client/src/Utils/NetworkService.js b/Client/src/Utils/NetworkService.js index 991b71ea9..91c838070 100644 --- a/Client/src/Utils/NetworkService.js +++ b/Client/src/Utils/NetworkService.js @@ -716,6 +716,33 @@ class NetworkService { } ); } + + /** + * ************************************ + * Get maintenance window by id + * ************************************ + * + * @async + * @param {Object} config - The configuration object. + * @param {string} config.authToken - The authorization token to be used in the request header. + * @param {string} [config.maintenanceWindowId] - The id of the maintenance window to delete. + * @returns {Promise} The response from the axios POST request. + * + */ + + async getMaintenanceWindowById(config) { + const { authToken, maintenanceWindowId } = config; + return this.axiosInstance.get( + `/maintenance-window/${maintenanceWindowId}`, + { + headers: { + Authorization: `Bearer ${authToken}`, + "Content-Type": "application/json", + }, + } + ); + } + /** * ************************************ * Get maintenance windows by teamId @@ -735,7 +762,6 @@ class NetworkService { async getMaintenanceWindowsByTeamId(config) { const { authToken, active, page, rowsPerPage, field, order } = config; - const params = new URLSearchParams(); if (active) params.append("status", active); @@ -756,7 +782,7 @@ class NetworkService { } /** * ************************************ - * Get maintenance windows by teamId + * Delete maintenance window by id * ************************************ * * @async diff --git a/Server/controllers/maintenanceWindowController.js b/Server/controllers/maintenanceWindowController.js index 3286cd06f..72028f622 100644 --- a/Server/controllers/maintenanceWindowController.js +++ b/Server/controllers/maintenanceWindowController.js @@ -1,5 +1,6 @@ const { createMaintenanceWindowBodyValidation, + getMaintenanceWindowByIdParamValidation, getMaintenanceWindowsByMonitorIdParamValidation, getMaintenanceWindowsByTeamIdQueryValidation, deleteMaintenanceWindowByIdParamValidation, @@ -50,6 +51,35 @@ const createMaintenanceWindows = async (req, res, next) => { } }; +const getMaintenanceWindowById = async (req, res, next) => { + try { + await getMaintenanceWindowByIdParamValidation.validateAsync(req.params); + } catch (error) { + error.status = 422; + error.service = SERVICE_NAME; + error.message = + error.details?.[0]?.message || error.message || "Validation Error"; + next(error); + return; + } + try { + const maintenanceWindow = await req.db.getMaintenanceWindowById( + req.params.id + ); + return res.status(200).json({ + success: true, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID, + data: maintenanceWindow, + }); + } catch (error) { + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "getMaintenanceWindowById") + : null; + next(error); + } +}; + const getMaintenanceWindowsByTeamId = async (req, res, next) => { try { await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query); @@ -171,6 +201,7 @@ const editMaintenanceWindow = async (req, res, next) => { module.exports = { createMaintenanceWindows, + getMaintenanceWindowById, getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, deleteMaintenanceWindow, diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index f625eb619..21d3b8c46 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -118,6 +118,7 @@ const { //**************************************** const { createMaintenanceWindow, + getMaintenanceWindowById, getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, deleteMaintenanceWindowById, @@ -183,6 +184,7 @@ module.exports = { deletePageSpeedChecksByMonitorId, createMaintenanceWindow, getMaintenanceWindowsByTeamId, + getMaintenanceWindowById, getMaintenanceWindowsByMonitorId, deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, diff --git a/Server/db/mongo/modules/maintenanceWindowModule.js b/Server/db/mongo/modules/maintenanceWindowModule.js index aaf3512f5..6fb3e7317 100644 --- a/Server/db/mongo/modules/maintenanceWindowModule.js +++ b/Server/db/mongo/modules/maintenanceWindowModule.js @@ -45,6 +45,19 @@ const createMaintenanceWindow = async (maintenanceWindowData) => { } }; +const getMaintenanceWindowById = async (maintenanceWindowId) => { + try { + const maintenanceWindow = await MaintenanceWindow.findById({ + _id: maintenanceWindowId, + }); + return maintenanceWindow; + } catch (error) { + error.service = SERVICE_NAME; + error.method = "getMaintenanceWindowById"; + throw error; + } +}; + /** * Asynchronously retrieves all MaintenanceWindow documents associated with a specific team ID. * @async @@ -202,6 +215,7 @@ const editMaintenanceWindowById = async ( module.exports = { createMaintenanceWindow, + getMaintenanceWindowById, getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, deleteMaintenanceWindowById, diff --git a/Server/routes/maintenanceWindowRoute.js b/Server/routes/maintenanceWindowRoute.js index 28928e9c2..ceb27d548 100644 --- a/Server/routes/maintenanceWindowRoute.js +++ b/Server/routes/maintenanceWindowRoute.js @@ -13,6 +13,8 @@ router.get( router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId); +router.get("/:id", maintenanceWindowController.getMaintenanceWindowById); + router.put("/:id", maintenanceWindowController.editMaintenanceWindow); router.delete("/:id", maintenanceWindowController.deleteMaintenanceWindow); diff --git a/Server/utils/messages.js b/Server/utils/messages.js index be194db0f..16b2f7883 100644 --- a/Server/utils/messages.js +++ b/Server/utils/messages.js @@ -93,6 +93,7 @@ const successMessages = { JOB_QUEUE_RESUME_JOB: "Job resumed successfully", //Maintenance Window Controller + MAINTENANCE_WINDOW_GET_BY_ID: "Got Maintenance Window by Id successfully", MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully", MAINTENANCE_WINDOW_GET_BY_TEAM: "Got Maintenance Windows by Team successfully", diff --git a/Server/validation/joi.js b/Server/validation/joi.js index d270444c2..3b07c5bcc 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -374,6 +374,10 @@ const createMaintenanceWindowBodyValidation = joi.object({ expiry: joi.date(), }); +const getMaintenanceWindowByIdParamValidation = joi.object({ + id: joi.string().required(), +}); + const getMaintenanceWindowsByTeamIdQueryValidation = joi.object({ active: joi.boolean(), page: joi.number(), @@ -469,6 +473,7 @@ module.exports = { deletePageSpeedCheckParamValidation, createPageSpeedCheckBodyValidation, createMaintenanceWindowBodyValidation, + getMaintenanceWindowByIdParamValidation, getMaintenanceWindowsByTeamIdQueryValidation, getMaintenanceWindowsByMonitorIdParamValidation, deleteMaintenanceWindowByIdParamValidation,