Files
Checkmate/server/controllers/maintenanceWindowController.js
T

141 lines
4.2 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
import {
createMaintenanceWindowBodyValidation,
editMaintenanceWindowByIdParamValidation,
editMaintenanceByIdWindowBodyValidation,
getMaintenanceWindowByIdParamValidation,
getMaintenanceWindowsByMonitorIdParamValidation,
getMaintenanceWindowsByTeamIdQueryValidation,
deleteMaintenanceWindowByIdParamValidation,
} from "../validation/joi.js";
2025-07-24 15:06:46 -07:00
import BaseController from "./baseController.js";
2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "maintenanceWindowController";
2025-07-24 15:06:46 -07:00
class MaintenanceWindowController extends BaseController {
2025-07-25 10:16:29 -07:00
constructor(commonDependencies, { settingsService, maintenanceWindowService }) {
super(commonDependencies);
2025-04-20 11:29:53 -07:00
this.settingsService = settingsService;
2025-07-23 15:14:47 -07:00
this.maintenanceWindowService = maintenanceWindowService;
2025-04-20 11:29:53 -07:00
}
2025-07-24 15:06:46 -07:00
createMaintenanceWindows = this.asyncHandler(
2025-07-23 15:14:47 -07:00
async (req, res) => {
2025-04-20 11:29:53 -07:00
await createMaintenanceWindowBodyValidation.validateAsync(req.body);
2025-07-21 12:27:43 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 12:27:43 -07:00
}
2025-07-23 15:14:47 -07:00
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, body: req.body });
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.maintenanceWindowCreate,
});
},
SERVICE_NAME,
"createMaintenanceWindows"
);
2025-04-20 11:29:53 -07:00
2025-07-24 15:06:46 -07:00
getMaintenanceWindowById = this.asyncHandler(
2025-07-23 15:14:47 -07:00
async (req, res) => {
2025-04-20 11:29:53 -07:00
await getMaintenanceWindowByIdParamValidation.validateAsync(req.params);
2025-07-21 12:27:43 -07:00
const teamId = req.user.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 12:27:43 -07:00
}
2025-07-23 15:14:47 -07:00
const maintenanceWindow = await this.maintenanceWindowService.getMaintenanceWindowById({ id: req.params.id, teamId });
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.maintenanceWindowGetById,
data: maintenanceWindow,
});
},
SERVICE_NAME,
"getMaintenanceWindowById"
);
2025-04-20 11:29:53 -07:00
2025-07-24 15:06:46 -07:00
getMaintenanceWindowsByTeamId = this.asyncHandler(
2025-07-23 15:14:47 -07:00
async (req, res) => {
2025-04-20 11:29:53 -07:00
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
2025-07-21 12:27:43 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 12:27:43 -07:00
}
2025-07-23 15:14:47 -07:00
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByTeamId({ teamId, query: req.query });
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.maintenanceWindowGetByTeam,
data: maintenanceWindows,
});
},
SERVICE_NAME,
"getMaintenanceWindowsByTeamId"
);
2025-04-20 11:29:53 -07:00
2025-07-24 15:06:46 -07:00
getMaintenanceWindowsByMonitorId = this.asyncHandler(
2025-07-23 15:14:47 -07:00
async (req, res) => {
2025-04-20 11:29:53 -07:00
await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(req.params);
2025-07-21 12:27:43 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 12:27:43 -07:00
}
2025-07-23 15:14:47 -07:00
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByMonitorId({ monitorId: req.params.monitorId, teamId });
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.maintenanceWindowGetByUser,
data: maintenanceWindows,
});
},
SERVICE_NAME,
"getMaintenanceWindowsByMonitorId"
);
2025-04-20 11:29:53 -07:00
2025-07-24 15:06:46 -07:00
deleteMaintenanceWindow = this.asyncHandler(
2025-07-23 15:14:47 -07:00
async (req, res) => {
2025-04-20 11:29:53 -07:00
await deleteMaintenanceWindowByIdParamValidation.validateAsync(req.params);
2025-07-21 12:27:43 -07:00
const teamId = req?.user?.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 12:27:43 -07:00
}
2025-07-23 15:14:47 -07:00
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: req.params.id, teamId });
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.maintenanceWindowDelete,
});
},
SERVICE_NAME,
"deleteMaintenanceWindow"
);
2025-04-20 11:29:53 -07:00
2025-07-24 15:06:46 -07:00
editMaintenanceWindow = this.asyncHandler(
2025-07-23 15:14:47 -07:00
async (req, res) => {
2025-04-20 11:29:53 -07:00
await editMaintenanceWindowByIdParamValidation.validateAsync(req.params);
await editMaintenanceByIdWindowBodyValidation.validateAsync(req.body);
2025-07-21 12:27:43 -07:00
const teamId = req.user.teamId;
if (!teamId) {
2025-07-24 15:06:46 -07:00
throw this.errorService.createBadRequestError("Team ID is required");
2025-07-21 12:27:43 -07:00
}
2025-07-23 15:14:47 -07:00
const editedMaintenanceWindow = await this.maintenanceWindowService.editMaintenanceWindow({ id: req.params.id, body: req.body, teamId });
2025-04-20 11:29:53 -07:00
return res.success({
msg: this.stringService.maintenanceWindowEdit,
data: editedMaintenanceWindow,
});
},
SERVICE_NAME,
"editMaintenanceWindow"
);
2025-04-20 11:29:53 -07:00
}
export default MaintenanceWindowController;