From 114cfd6cb78e52fee5db4008e660c26af107a4f8 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 9 Oct 2024 11:33:12 +0800 Subject: [PATCH] add put route, controller, and db method for maintenance window --- .../maintenanceWindowController.js | 28 +++++++++++++++++++ Server/db/mongo/MongoDB.js | 2 ++ .../mongo/modules/maintenanceWindowModule.js | 13 +++++++++ Server/routes/maintenanceWindowRoute.js | 2 ++ Server/utils/messages.js | 1 + Server/validation/joi.js | 17 +++++++++++ 6 files changed, 63 insertions(+) diff --git a/Server/controllers/maintenanceWindowController.js b/Server/controllers/maintenanceWindowController.js index ee15e06db..3286cd06f 100644 --- a/Server/controllers/maintenanceWindowController.js +++ b/Server/controllers/maintenanceWindowController.js @@ -142,9 +142,37 @@ const deleteMaintenanceWindow = async (req, res, next) => { : null; } }; + +const editMaintenanceWindow = async (req, res, next) => { + try { + await editMaintenanceWindowByIdParamValidation.validateAsync(req.params); + await editMaintenanceByIdWindowBodyValidation.validateAsync(req.body); + } catch (error) { + error.status = 422; + error.service = SERVICE_NAME; + error.message = + error.details?.[0]?.message || error.message || "Validation Error"; + next(error); + return; + } + try { + await req.db.editMaintenanceWindow(req.params.id, req.body); + return res.status(201).json({ + success: true, + msg: successMessages.MAINTENANCE_WINDOW_EDIT, + }); + } catch (error) { + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined + ? (error.method = "editMaintenanceWindow") + : null; + } +}; + module.exports = { createMaintenanceWindows, getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, deleteMaintenanceWindow, + editMaintenanceWindow, }; diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index 0d3d2b70f..f625eb619 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -123,6 +123,7 @@ const { deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, + editMaintenanceWindowById, } = require("./modules/maintenanceWindowModule"); //**************************************** @@ -186,6 +187,7 @@ module.exports = { deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, + editMaintenanceWindowById, createNotification, getNotificationsByMonitorId, deleteNotificationsByMonitorId, diff --git a/Server/db/mongo/modules/maintenanceWindowModule.js b/Server/db/mongo/modules/maintenanceWindowModule.js index 7e328e27c..aaf3512f5 100644 --- a/Server/db/mongo/modules/maintenanceWindowModule.js +++ b/Server/db/mongo/modules/maintenanceWindowModule.js @@ -188,6 +188,18 @@ const deleteMaintenanceWindowByUserId = async (userId) => { } }; +const editMaintenanceWindowById = async ( + maintenanceWindowId, + maintenanceWindowData +) => { + try { + } catch (error) { + error.service = SERVICE_NAME; + error.method = "editMaintenanceWindowById"; + throw error; + } +}; + module.exports = { createMaintenanceWindow, getMaintenanceWindowsByTeamId, @@ -195,4 +207,5 @@ module.exports = { deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, + editMaintenanceWindowById, }; diff --git a/Server/routes/maintenanceWindowRoute.js b/Server/routes/maintenanceWindowRoute.js index f7a095abc..28928e9c2 100644 --- a/Server/routes/maintenanceWindowRoute.js +++ b/Server/routes/maintenanceWindowRoute.js @@ -13,6 +13,8 @@ router.get( router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId); +router.put("/:id", maintenanceWindowController.editMaintenanceWindow); + router.delete("/:id", maintenanceWindowController.deleteMaintenanceWindow); module.exports = router; diff --git a/Server/utils/messages.js b/Server/utils/messages.js index 3b6244faa..be194db0f 100644 --- a/Server/utils/messages.js +++ b/Server/utils/messages.js @@ -97,6 +97,7 @@ const successMessages = { MAINTENANCE_WINDOW_GET_BY_TEAM: "Got Maintenance Windows by Team successfully", MAINTENANCE_WINDOW_DELETE: "Maintenance Window deleted successfully", + MAINTENANCE_WINDOW_EDIT: "Maintenance Window edited successfully", //Ping Operations PING_SUCCESS: "Success", diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 43ca26c85..d270444c2 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -1,5 +1,7 @@ +const exp = require("constants"); const joi = require("joi"); const { normalize } = require("path"); +const { start } = require("repl"); //**************************************** // Custom Validators @@ -388,6 +390,19 @@ const deleteMaintenanceWindowByIdParamValidation = joi.object({ id: joi.string().required(), }); +const editMaintenanceWindowByIdParamValidation = joi.object({ + id: joi.string().required(), +}); + +const editMaintenanceByIdWindowBodyValidation = joi.object({ + active: joi.boolean(), + name: joi.string(), + repeat: joi.number(), + start: joi.date(), + end: joi.date(), + expiry: joi.date(), +}); + //**************************************** // SettingsValidation //**************************************** @@ -457,5 +472,7 @@ module.exports = { getMaintenanceWindowsByTeamIdQueryValidation, getMaintenanceWindowsByMonitorIdParamValidation, deleteMaintenanceWindowByIdParamValidation, + editMaintenanceWindowByIdParamValidation, + editMaintenanceByIdWindowBodyValidation, updateAppSettingsBodyValidation, };