Add getMaintenanceWindowsByTeamId

This commit is contained in:
Alex Holliday
2024-10-04 13:49:53 +08:00
parent 686e1f09d9
commit 68296eccc3
6 changed files with 64 additions and 34 deletions

View File

@@ -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,
};

View File

@@ -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,

View File

@@ -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<Array<MaintenanceWindow>>} 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<MaintenanceWindow>} 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,
};

View File

@@ -11,9 +11,6 @@ router.get(
maintenanceWindowController.getMaintenanceWindowsByMonitorId
);
router.get(
"/user/:userId",
maintenanceWindowController.getMaintenanceWindowsByUserId
);
router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId);
module.exports = router;

View File

@@ -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",

View File

@@ -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,
};