add getmonitorsbyids
This commit is contained in:
1 parent
5dbec377e5
commit
87f843df1c
2 files changed
+66
-6
No files matched your search
@@ -22,8 +22,25 @@ class MaintenanceWindowController {
|
|||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
await createMaintenanceWindowBodyValidation.validateAsync(req.body);
|
await createMaintenanceWindowBodyValidation.validateAsync(req.body);
|
||||||
|
|
||||||
const { teamId } = req.user;
|
const teamId = req?.user?.teamId;
|
||||||
|
if (!teamId) {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
const monitorIds = req.body.monitors;
|
const monitorIds = req.body.monitors;
|
||||||
|
|
||||||
|
const monitors = await this.db.getMonitorsByIds(monitorIds);
|
||||||
|
|
||||||
|
const unauthorizedMonitors = monitors.filter((monitor) => !monitor.teamId.equals(teamId));
|
||||||
|
|
||||||
|
if (unauthorizedMonitors.length > 0) {
|
||||||
|
const error = new Error("Unauthorized access to one or more monitors");
|
||||||
|
error.status = 403;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "createMaintenanceWindows";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
const dbTransactions = monitorIds.map((monitorId) => {
|
const dbTransactions = monitorIds.map((monitorId) => {
|
||||||
return this.db.createMaintenanceWindow({
|
return this.db.createMaintenanceWindow({
|
||||||
teamId,
|
teamId,
|
||||||
@@ -48,7 +65,13 @@ class MaintenanceWindowController {
|
|||||||
getMaintenanceWindowById = asyncHandler(
|
getMaintenanceWindowById = asyncHandler(
|
||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
await getMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
await getMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
||||||
const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id);
|
|
||||||
|
const teamId = req.user.teamId;
|
||||||
|
if (!teamId) {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
const maintenanceWindow = await this.db.getMaintenanceWindowById({ id: req.params.id, teamId });
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.maintenanceWindowGetById,
|
msg: this.stringService.maintenanceWindowGetById,
|
||||||
data: maintenanceWindow,
|
data: maintenanceWindow,
|
||||||
@@ -62,7 +85,12 @@ class MaintenanceWindowController {
|
|||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
|
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
|
||||||
|
|
||||||
const { teamId } = req.user;
|
const teamId = req?.user?.teamId;
|
||||||
|
|
||||||
|
if (!teamId) {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
const maintenanceWindows = await this.db.getMaintenanceWindowsByTeamId(teamId, req.query);
|
const maintenanceWindows = await this.db.getMaintenanceWindowsByTeamId(teamId, req.query);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
@@ -78,7 +106,14 @@ class MaintenanceWindowController {
|
|||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(req.params);
|
await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(req.params);
|
||||||
|
|
||||||
const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId(req.params.monitorId);
|
const monitorId = req.params.monitorId;
|
||||||
|
|
||||||
|
const teamId = req?.user?.teamId;
|
||||||
|
if (!teamId) {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId({ monitorId, teamId });
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.maintenanceWindowGetByUser,
|
msg: this.stringService.maintenanceWindowGetByUser,
|
||||||
@@ -92,7 +127,13 @@ class MaintenanceWindowController {
|
|||||||
deleteMaintenanceWindow = asyncHandler(
|
deleteMaintenanceWindow = asyncHandler(
|
||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
await deleteMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
await deleteMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
||||||
await this.db.deleteMaintenanceWindowById(req.params.id);
|
|
||||||
|
const teamId = req?.user?.teamId;
|
||||||
|
if (!teamId) {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.db.deleteMaintenanceWindowById({ id: req.params.id, teamId });
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.maintenanceWindowDelete,
|
msg: this.stringService.maintenanceWindowDelete,
|
||||||
});
|
});
|
||||||
@@ -105,7 +146,14 @@ class MaintenanceWindowController {
|
|||||||
async (req, res, next) => {
|
async (req, res, next) => {
|
||||||
await editMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
await editMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
||||||
await editMaintenanceByIdWindowBodyValidation.validateAsync(req.body);
|
await editMaintenanceByIdWindowBodyValidation.validateAsync(req.body);
|
||||||
const editedMaintenanceWindow = await this.db.editMaintenanceWindowById(req.params.id, req.body);
|
|
||||||
|
const teamId = req.user.teamId;
|
||||||
|
if (!teamId) {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
const editedMaintenanceWindow = await this.db.editMaintenanceWindowById({ id: req.params.id, body: req.body, teamId });
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.maintenanceWindowEdit,
|
msg: this.stringService.maintenanceWindowEdit,
|
||||||
data: editedMaintenanceWindow,
|
data: editedMaintenanceWindow,
|
||||||
|
|||||||
@@ -461,6 +461,17 @@ const getMonitorById = async (monitorId) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getMonitorsByIds = async (monitorIds) => {
|
||||||
|
try {
|
||||||
|
const objectIds = monitorIds.map((id) => new ObjectId(id));
|
||||||
|
return await Monitor.find({ _id: { $in: objectIds } }, { _id: 1, teamId: 1 }).lean();
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getMonitorsByIds";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const getMonitorsByTeamId = async ({ limit, type, page, rowsPerPage, filter, field, order, teamId }) => {
|
const getMonitorsByTeamId = async ({ limit, type, page, rowsPerPage, filter, field, order, teamId }) => {
|
||||||
limit = parseInt(limit);
|
limit = parseInt(limit);
|
||||||
page = parseInt(page);
|
page = parseInt(page);
|
||||||
@@ -753,6 +764,7 @@ export {
|
|||||||
getAllMonitorsWithUptimeStats,
|
getAllMonitorsWithUptimeStats,
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
|
getMonitorsByIds,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
getMonitorsAndSummaryByTeamId,
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsWithChecksByTeamId,
|
getMonitorsWithChecksByTeamId,
|
||||||
|
|||||||
Reference in new issue
Block a user