2026-01-09 15:39:30 -08:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
2025-04-20 11:29:53 -07:00
|
|
|
import {
|
|
|
|
|
createMaintenanceWindowBodyValidation,
|
|
|
|
|
editMaintenanceWindowByIdParamValidation,
|
|
|
|
|
editMaintenanceByIdWindowBodyValidation,
|
|
|
|
|
getMaintenanceWindowByIdParamValidation,
|
|
|
|
|
getMaintenanceWindowsByMonitorIdParamValidation,
|
|
|
|
|
getMaintenanceWindowsByTeamIdQueryValidation,
|
|
|
|
|
deleteMaintenanceWindowByIdParamValidation,
|
2026-01-09 15:32:53 -08:00
|
|
|
} from "@/validation/joi.js";
|
2026-01-21 23:26:13 +00:00
|
|
|
import { requireTeamId } from "@/controllers/controllerUtils.js";
|
2025-04-20 11:29:53 -07:00
|
|
|
|
|
|
|
|
const SERVICE_NAME = "maintenanceWindowController";
|
|
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
class MaintenanceWindowController {
|
2025-07-25 15:15:14 -07:00
|
|
|
static SERVICE_NAME = SERVICE_NAME;
|
2026-01-09 15:32:53 -08:00
|
|
|
private maintenanceWindowService: any;
|
|
|
|
|
constructor(maintenanceWindowService: any) {
|
2025-07-23 15:14:47 -07:00
|
|
|
this.maintenanceWindowService = maintenanceWindowService;
|
2025-04-20 11:29:53 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 15:15:14 -07:00
|
|
|
get serviceName() {
|
|
|
|
|
return MaintenanceWindowController.SERVICE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2025-04-20 11:29:53 -07:00
|
|
|
await createMaintenanceWindowBodyValidation.validateAsync(req.body);
|
2026-01-21 23:26:13 +00:00
|
|
|
const teamId = requireTeamId(req?.user?.teamId);
|
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
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Maintenance window created successfully",
|
2025-04-20 11:29:53 -07:00
|
|
|
});
|
2026-01-09 15:32:53 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
getMaintenanceWindowById = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2025-04-20 11:29:53 -07:00
|
|
|
await getMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
2025-07-21 12:27:43 -07:00
|
|
|
|
2026-01-21 23:26:13 +00:00
|
|
|
const teamId = requireTeamId(req.user?.teamId);
|
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 });
|
|
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Maintenance window fetched successfully",
|
2025-04-20 11:29:53 -07:00
|
|
|
data: maintenanceWindow,
|
|
|
|
|
});
|
2026-01-09 15:32:53 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
getMaintenanceWindowsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2025-04-20 11:29:53 -07:00
|
|
|
await getMaintenanceWindowsByTeamIdQueryValidation.validateAsync(req.query);
|
|
|
|
|
|
2026-01-21 23:26:13 +00:00
|
|
|
const teamId = requireTeamId(req?.user?.teamId);
|
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
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Maintenance windows fetched successfully",
|
2025-04-20 11:29:53 -07:00
|
|
|
data: maintenanceWindows,
|
|
|
|
|
});
|
2026-01-09 15:32:53 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
getMaintenanceWindowsByMonitorId = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2025-04-20 11:29:53 -07:00
|
|
|
await getMaintenanceWindowsByMonitorIdParamValidation.validateAsync(req.params);
|
|
|
|
|
|
2026-01-21 23:26:13 +00:00
|
|
|
const teamId = requireTeamId(req?.user?.teamId);
|
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
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Maintenance windows fetched successfully",
|
2025-04-20 11:29:53 -07:00
|
|
|
data: maintenanceWindows,
|
|
|
|
|
});
|
2026-01-09 15:32:53 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
deleteMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2025-04-20 11:29:53 -07:00
|
|
|
await deleteMaintenanceWindowByIdParamValidation.validateAsync(req.params);
|
2025-07-21 12:27:43 -07:00
|
|
|
|
2026-01-21 23:26:13 +00:00
|
|
|
const teamId = requireTeamId(req?.user?.teamId);
|
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 });
|
|
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Maintenance window deleted successfully",
|
2025-04-20 11:29:53 -07:00
|
|
|
});
|
2026-01-09 15:32:53 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2026-01-09 15:32:53 -08:00
|
|
|
editMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
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
|
|
|
|
2026-01-21 23:26:13 +00:00
|
|
|
const teamId = requireTeamId(req.user?.teamId);
|
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 });
|
2026-01-09 15:32:53 -08:00
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Maintenance window edited successfully",
|
2025-04-20 11:29:53 -07:00
|
|
|
data: editedMaintenanceWindow,
|
|
|
|
|
});
|
2026-01-09 15:32:53 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-04-20 11:29:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MaintenanceWindowController;
|