diff --git a/Server/controllers/maintenanceWindowController.js b/Server/controllers/maintenanceWindowController.js index d11e757bd..09dd177a9 100644 --- a/Server/controllers/maintenanceWindowController.js +++ b/Server/controllers/maintenanceWindowController.js @@ -1,7 +1,7 @@ const { createMaintenanceWindowBodyValidation, - getMaintenanceWindowsByUserIdParamValidation, getMaintenanceWindowsByMonitorIdParamValidation, + getMaintenanceWindowsByTeamIdQueryValidation, } = require("../validation/joi"); const jwt = require("jsonwebtoken"); const { getTokenFromHeaders } = require("../utils/utils"); @@ -48,11 +48,9 @@ const createMaintenanceWindows = async (req, res, next) => { } }; -const getMaintenanceWindowsByUserId = async (req, res, next) => { +const getMaintenanceWindowsByTeamId = async (req, res, next) => { try { - await getMaintenanceWindowsByUserIdParamValidation.validateAsync( - req.params - ); + await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query); } catch (error) { error.status = 422; error.service = SERVICE_NAME; @@ -61,14 +59,19 @@ const getMaintenanceWindowsByUserId = async (req, res, next) => { next(error); return; } + try { - const maintenanceWindows = await req.db.getMaintenanceWindowsByUserId( - req.params.userId + const token = getTokenFromHeaders(req.headers); + const { jwtSecret } = req.settingsService.getSettings(); + const { teamId } = jwt.verify(token, jwtSecret); + const maintenanceWindows = await req.db.getMaintenanceWindowsByTeamId( + teamId, + req.query ); return res.status(201).json({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM, data: maintenanceWindows, }); } catch (error) { @@ -114,6 +117,6 @@ const getMaintenanceWindowsByMonitorId = async (req, res, next) => { }; module.exports = { createMaintenanceWindows, - getMaintenanceWindowsByUserId, + getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, }; diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index aeb993433..0d3d2b70f 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -118,12 +118,12 @@ const { //**************************************** const { createMaintenanceWindow, - getMaintenanceWindowsByUserId, + getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, - deleteMaintenaceWindowById, + deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, -} = require("./modules/maintenaceWindowModule"); +} = require("./modules/maintenanceWindowModule"); //**************************************** // Notifications @@ -181,9 +181,9 @@ module.exports = { getPageSpeedChecks, deletePageSpeedChecksByMonitorId, createMaintenanceWindow, - getMaintenanceWindowsByUserId, + getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, - deleteMaintenaceWindowById, + deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, createNotification, diff --git a/Server/db/mongo/modules/maintenaceWindowModule.js b/Server/db/mongo/modules/maintenanceWindowModule.js similarity index 83% rename from Server/db/mongo/modules/maintenaceWindowModule.js rename to Server/db/mongo/modules/maintenanceWindowModule.js index f49c9dca4..57b1eb05f 100644 --- a/Server/db/mongo/modules/maintenaceWindowModule.js +++ b/Server/db/mongo/modules/maintenanceWindowModule.js @@ -46,21 +46,46 @@ const createMaintenanceWindow = async (maintenanceWindowData) => { }; /** - * Asynchronously retrieves all MaintenanceWindow documents associated with a specific user ID. + * Asynchronously retrieves all MaintenanceWindow documents associated with a specific team ID. * @async * @function getMaintenanceWindowByUserId - * @param {String} userId - The ID of the user. + * @param {String} teamId - The ID of the team. + * @param {Object} query - The request body. * @returns {Promise>} An array of MaintenanceWindow documents. * @throws {Error} If there is an error retrieving the documents. * @example - * getMaintenanceWindowByUserId('userId') + * getMaintenanceWindowByUserId(teamId) * .then(maintenanceWindows => console.log(maintenanceWindows)) * .catch(error => console.error(error)); */ -const getMaintenanceWindowsByUserId = async (userId) => { +const getMaintenanceWindowsByTeamId = async (teamId, query) => { try { - const maintenanceWindows = await MaintenanceWindow.find({ userId: userId }); - return maintenanceWindows; + let { active, page, rowsPerPage, field, order } = query || {}; + const maintenanceQuery = { teamId }; + + if (active !== undefined) maintenanceQuery.active = active; + + const maintenanceWindowCount = + await MaintenanceWindow.countDocuments(maintenanceQuery); + + // Pagination + let skip = 0; + if (page && rowsPerPage) { + skip = page * rowsPerPage; + } + + // Sorting + let sort = {}; + if (field !== undefined && order !== undefined) { + sort[field] = order === "asc" ? 1 : -1; + } + + const maintenanceWindows = await MaintenanceWindow.find(maintenanceQuery) + .skip(skip) + .limit(rowsPerPage) + .sort(sort); + + return { maintenanceWindows, maintenanceWindowCount }; } catch (error) { error.service = SERVICE_NAME; error.method = "getMaintenanceWindowByUserId"; @@ -96,23 +121,23 @@ const getMaintenanceWindowsByMonitorId = async (monitorId) => { /** * Asynchronously deletes a MaintenanceWindow document by its ID. * @async - * @function deleteMaintenaceWindowById + * @function deleteMaintenanceWindowById * @param {mongoose.Schema.Types.ObjectId} maintenanceWindowId - The ID of the MaintenanceWindow document to delete. * @returns {Promise} The deleted MaintenanceWindow document. * @throws {Error} If there is an error deleting the document. * @example - * deleteMaintenaceWindowById('maintenanceWindowId') + * deleteMaintenanceWindowById('maintenanceWindowId') * .then(maintenanceWindow => console.log(maintenanceWindow)) * .catch(error => console.error(error)); */ -const deleteMaintenaceWindowById = async (maintenanceWindowId) => { +const deleteMaintenanceWindowById = async (maintenanceWindowId) => { try { const maintenanceWindow = await MaintenanceWindow.findByIdAndDelete(maintenanceWindowId); return maintenanceWindow; } catch (error) { error.service = SERVICE_NAME; - error.method = "deleteMaintenaceWindowById"; + error.method = "deleteMaintenanceWindowById"; throw error; } }; @@ -165,9 +190,9 @@ const deleteMaintenanceWindowByUserId = async (userId) => { module.exports = { createMaintenanceWindow, - getMaintenanceWindowsByUserId, + getMaintenanceWindowsByTeamId, getMaintenanceWindowsByMonitorId, - deleteMaintenaceWindowById, + deleteMaintenanceWindowById, deleteMaintenanceWindowByMonitorId, deleteMaintenanceWindowByUserId, }; diff --git a/Server/routes/maintenanceWindowRoute.js b/Server/routes/maintenanceWindowRoute.js index 27bdfe605..d4aa2908a 100644 --- a/Server/routes/maintenanceWindowRoute.js +++ b/Server/routes/maintenanceWindowRoute.js @@ -11,9 +11,6 @@ router.get( maintenanceWindowController.getMaintenanceWindowsByMonitorId ); -router.get( - "/user/:userId", - maintenanceWindowController.getMaintenanceWindowsByUserId -); +router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId); module.exports = router; diff --git a/Server/utils/messages.js b/Server/utils/messages.js index c1e4d887b..e05602552 100644 --- a/Server/utils/messages.js +++ b/Server/utils/messages.js @@ -93,7 +93,8 @@ const successMessages = { //Maintenance Window Controller MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully", - MAINTENANCE_WINDOW_GET_BY_USER: "Got Maintenance Windows by User successfully", + MAINTENANCE_WINDOW_GET_BY_TEAM: + "Got Maintenance Windows by Team successfully", //Ping Operations PING_SUCCESS: "Success", diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 5dffcc6be..d8e9b61cd 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -372,8 +372,12 @@ const createMaintenanceWindowBodyValidation = joi.object({ expiry: joi.date(), }); -const getMaintenanceWindowsByUserIdParamValidation = joi.object({ - userId: joi.string().required(), +const getMaintenanceWindowsByTeamIdQueryValidation = joi.object({ + active: joi.boolean(), + page: joi.number(), + rowsPerPage: joi.number(), + field: joi.string(), + order: joi.string().valid("asc", "desc"), }); const getMaintenanceWindowsByMonitorIdParamValidation = joi.object({ @@ -446,7 +450,7 @@ module.exports = { deletePageSpeedCheckParamValidation, createPageSpeedCheckBodyValidation, createMaintenanceWindowBodyValidation, - getMaintenanceWindowsByUserIdParamValidation, + getMaintenanceWindowsByTeamIdQueryValidation, getMaintenanceWindowsByMonitorIdParamValidation, updateAppSettingsBodyValidation, };