Add getMaintenanceWindowsByTeamId
This commit is contained in:
1 parent
686e1f09d9
commit
68296eccc3
6 files changed
+64
-34
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
const {
|
const {
|
||||||
createMaintenanceWindowBodyValidation,
|
createMaintenanceWindowBodyValidation,
|
||||||
getMaintenanceWindowsByUserIdParamValidation,
|
|
||||||
getMaintenanceWindowsByMonitorIdParamValidation,
|
getMaintenanceWindowsByMonitorIdParamValidation,
|
||||||
|
getMaintenanceWindowsByTeamIdQueryValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
const { getTokenFromHeaders } = require("../utils/utils");
|
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 {
|
try {
|
||||||
await getMaintenanceWindowsByUserIdParamValidation.validateAsync(
|
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
|
||||||
req.params
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.status = 422;
|
error.status = 422;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
@@ -61,14 +59,19 @@ const getMaintenanceWindowsByUserId = async (req, res, next) => {
|
|||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const maintenanceWindows = await req.db.getMaintenanceWindowsByUserId(
|
const token = getTokenFromHeaders(req.headers);
|
||||||
req.params.userId
|
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({
|
return res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
|
||||||
data: maintenanceWindows,
|
data: maintenanceWindows,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -114,6 +117,6 @@ const getMaintenanceWindowsByMonitorId = async (req, res, next) => {
|
|||||||
};
|
};
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createMaintenanceWindows,
|
createMaintenanceWindows,
|
||||||
getMaintenanceWindowsByUserId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
};
|
};
|
||||||
@@ -118,12 +118,12 @@ const {
|
|||||||
//****************************************
|
//****************************************
|
||||||
const {
|
const {
|
||||||
createMaintenanceWindow,
|
createMaintenanceWindow,
|
||||||
getMaintenanceWindowsByUserId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenaceWindowById,
|
deleteMaintenanceWindowById,
|
||||||
deleteMaintenanceWindowByMonitorId,
|
deleteMaintenanceWindowByMonitorId,
|
||||||
deleteMaintenanceWindowByUserId,
|
deleteMaintenanceWindowByUserId,
|
||||||
} = require("./modules/maintenaceWindowModule");
|
} = require("./modules/maintenanceWindowModule");
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Notifications
|
// Notifications
|
||||||
@@ -181,9 +181,9 @@ module.exports = {
|
|||||||
getPageSpeedChecks,
|
getPageSpeedChecks,
|
||||||
deletePageSpeedChecksByMonitorId,
|
deletePageSpeedChecksByMonitorId,
|
||||||
createMaintenanceWindow,
|
createMaintenanceWindow,
|
||||||
getMaintenanceWindowsByUserId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenaceWindowById,
|
deleteMaintenanceWindowById,
|
||||||
deleteMaintenanceWindowByMonitorId,
|
deleteMaintenanceWindowByMonitorId,
|
||||||
deleteMaintenanceWindowByUserId,
|
deleteMaintenanceWindowByUserId,
|
||||||
createNotification,
|
createNotification,
|
||||||
|
|||||||
+37
-12
@@ -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
|
* @async
|
||||||
* @function getMaintenanceWindowByUserId
|
* @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.
|
* @returns {Promise<Array<MaintenanceWindow>>} An array of MaintenanceWindow documents.
|
||||||
* @throws {Error} If there is an error retrieving the documents.
|
* @throws {Error} If there is an error retrieving the documents.
|
||||||
* @example
|
* @example
|
||||||
* getMaintenanceWindowByUserId('userId')
|
* getMaintenanceWindowByUserId(teamId)
|
||||||
* .then(maintenanceWindows => console.log(maintenanceWindows))
|
* .then(maintenanceWindows => console.log(maintenanceWindows))
|
||||||
* .catch(error => console.error(error));
|
* .catch(error => console.error(error));
|
||||||
*/
|
*/
|
||||||
const getMaintenanceWindowsByUserId = async (userId) => {
|
const getMaintenanceWindowsByTeamId = async (teamId, query) => {
|
||||||
try {
|
try {
|
||||||
const maintenanceWindows = await MaintenanceWindow.find({ userId: userId });
|
let { active, page, rowsPerPage, field, order } = query || {};
|
||||||
return maintenanceWindows;
|
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) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "getMaintenanceWindowByUserId";
|
error.method = "getMaintenanceWindowByUserId";
|
||||||
@@ -96,23 +121,23 @@ const getMaintenanceWindowsByMonitorId = async (monitorId) => {
|
|||||||
/**
|
/**
|
||||||
* Asynchronously deletes a MaintenanceWindow document by its ID.
|
* Asynchronously deletes a MaintenanceWindow document by its ID.
|
||||||
* @async
|
* @async
|
||||||
* @function deleteMaintenaceWindowById
|
* @function deleteMaintenanceWindowById
|
||||||
* @param {mongoose.Schema.Types.ObjectId} maintenanceWindowId - The ID of the MaintenanceWindow document to delete.
|
* @param {mongoose.Schema.Types.ObjectId} maintenanceWindowId - The ID of the MaintenanceWindow document to delete.
|
||||||
* @returns {Promise<MaintenanceWindow>} The deleted MaintenanceWindow document.
|
* @returns {Promise<MaintenanceWindow>} The deleted MaintenanceWindow document.
|
||||||
* @throws {Error} If there is an error deleting the document.
|
* @throws {Error} If there is an error deleting the document.
|
||||||
* @example
|
* @example
|
||||||
* deleteMaintenaceWindowById('maintenanceWindowId')
|
* deleteMaintenanceWindowById('maintenanceWindowId')
|
||||||
* .then(maintenanceWindow => console.log(maintenanceWindow))
|
* .then(maintenanceWindow => console.log(maintenanceWindow))
|
||||||
* .catch(error => console.error(error));
|
* .catch(error => console.error(error));
|
||||||
*/
|
*/
|
||||||
const deleteMaintenaceWindowById = async (maintenanceWindowId) => {
|
const deleteMaintenanceWindowById = async (maintenanceWindowId) => {
|
||||||
try {
|
try {
|
||||||
const maintenanceWindow =
|
const maintenanceWindow =
|
||||||
await MaintenanceWindow.findByIdAndDelete(maintenanceWindowId);
|
await MaintenanceWindow.findByIdAndDelete(maintenanceWindowId);
|
||||||
return maintenanceWindow;
|
return maintenanceWindow;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "deleteMaintenaceWindowById";
|
error.method = "deleteMaintenanceWindowById";
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -165,9 +190,9 @@ const deleteMaintenanceWindowByUserId = async (userId) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createMaintenanceWindow,
|
createMaintenanceWindow,
|
||||||
getMaintenanceWindowsByUserId,
|
getMaintenanceWindowsByTeamId,
|
||||||
getMaintenanceWindowsByMonitorId,
|
getMaintenanceWindowsByMonitorId,
|
||||||
deleteMaintenaceWindowById,
|
deleteMaintenanceWindowById,
|
||||||
deleteMaintenanceWindowByMonitorId,
|
deleteMaintenanceWindowByMonitorId,
|
||||||
deleteMaintenanceWindowByUserId,
|
deleteMaintenanceWindowByUserId,
|
||||||
};
|
};
|
||||||
@@ -11,9 +11,6 @@ router.get(
|
|||||||
maintenanceWindowController.getMaintenanceWindowsByMonitorId
|
maintenanceWindowController.getMaintenanceWindowsByMonitorId
|
||||||
);
|
);
|
||||||
|
|
||||||
router.get(
|
router.get("/team/", maintenanceWindowController.getMaintenanceWindowsByTeamId);
|
||||||
"/user/:userId",
|
|
||||||
maintenanceWindowController.getMaintenanceWindowsByUserId
|
|
||||||
);
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@@ -93,7 +93,8 @@ const successMessages = {
|
|||||||
|
|
||||||
//Maintenance Window Controller
|
//Maintenance Window Controller
|
||||||
MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully",
|
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 Operations
|
||||||
PING_SUCCESS: "Success",
|
PING_SUCCESS: "Success",
|
||||||
|
|||||||
@@ -372,8 +372,12 @@ const createMaintenanceWindowBodyValidation = joi.object({
|
|||||||
expiry: joi.date(),
|
expiry: joi.date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const getMaintenanceWindowsByUserIdParamValidation = joi.object({
|
const getMaintenanceWindowsByTeamIdQueryValidation = joi.object({
|
||||||
userId: joi.string().required(),
|
active: joi.boolean(),
|
||||||
|
page: joi.number(),
|
||||||
|
rowsPerPage: joi.number(),
|
||||||
|
field: joi.string(),
|
||||||
|
order: joi.string().valid("asc", "desc"),
|
||||||
});
|
});
|
||||||
|
|
||||||
const getMaintenanceWindowsByMonitorIdParamValidation = joi.object({
|
const getMaintenanceWindowsByMonitorIdParamValidation = joi.object({
|
||||||
@@ -446,7 +450,7 @@ module.exports = {
|
|||||||
deletePageSpeedCheckParamValidation,
|
deletePageSpeedCheckParamValidation,
|
||||||
createPageSpeedCheckBodyValidation,
|
createPageSpeedCheckBodyValidation,
|
||||||
createMaintenanceWindowBodyValidation,
|
createMaintenanceWindowBodyValidation,
|
||||||
getMaintenanceWindowsByUserIdParamValidation,
|
getMaintenanceWindowsByTeamIdQueryValidation,
|
||||||
getMaintenanceWindowsByMonitorIdParamValidation,
|
getMaintenanceWindowsByMonitorIdParamValidation,
|
||||||
updateAppSettingsBodyValidation,
|
updateAppSettingsBodyValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user