Files
Checkmate/server/src/controllers/maintenanceWindowController.ts
T

147 lines
4.7 KiB
TypeScript
Raw Normal View History

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-03-01 17:35:00 +00:00
} from "@/validation/maintenanceWindowValidation.js";
2026-01-21 23:26:13 +00:00
import { requireTeamId } from "@/controllers/controllerUtils.js";
2026-03-11 11:50:51 -07:00
import { IMaintenanceWindowService } from "@/service/index.js";
2025-04-20 11:29:53 -07:00
2026-01-09 15:32:53 -08:00
class MaintenanceWindowController {
2026-03-11 11:50:51 -07:00
private maintenanceWindowService: IMaintenanceWindowService;
constructor(maintenanceWindowService: IMaintenanceWindowService) {
2025-07-23 15:14:47 -07:00
this.maintenanceWindowService = maintenanceWindowService;
2025-04-20 11:29:53 -07:00
}
2026-01-09 15:32:53 -08:00
createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-11 12:00:19 -07:00
const validatedBody = createMaintenanceWindowBodyValidation.parse(req.body);
2026-01-21 23:26:13 +00:00
const teamId = requireTeamId(req?.user?.teamId);
2026-03-20 11:20:22 -07:00
const monitorIDs = validatedBody.monitors;
const name = validatedBody.name;
const active = validatedBody.active ? validatedBody.active : true;
const duration = validatedBody.duration;
const durationUnit = validatedBody.durationUnit;
const repeat = validatedBody.repeat;
const start = validatedBody.start;
const end = validatedBody.end;
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, monitorIDs, name, active, duration, durationUnit, repeat, start, end });
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 {
2026-03-11 12:04:28 -07:00
const validatedParams = getMaintenanceWindowByIdParamValidation.parse(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
2026-03-11 12:04:28 -07:00
const maintenanceWindow = await this.maintenanceWindowService.getMaintenanceWindowById({ id: validatedParams.id, teamId });
2025-07-23 15:14:47 -07:00
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 {
2026-03-11 11:50:51 -07:00
const validatedQuery = getMaintenanceWindowsByTeamIdQueryValidation.parse(req.query);
2025-04-20 11:29:53 -07:00
2026-01-21 23:26:13 +00:00
const teamId = requireTeamId(req?.user?.teamId);
2025-07-21 12:27:43 -07:00
2026-03-11 11:50:51 -07:00
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByTeamId({
teamId,
active: validatedQuery.active,
page: validatedQuery.page,
rowsPerPage: validatedQuery.rowsPerPage,
field: validatedQuery.field,
order: validatedQuery.order,
});
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 {
2026-03-11 11:50:51 -07:00
const validatedParams = getMaintenanceWindowsByMonitorIdParamValidation.parse(req.params);
2025-04-20 11:29:53 -07:00
2026-01-21 23:26:13 +00:00
const teamId = requireTeamId(req?.user?.teamId);
2025-07-21 12:27:43 -07:00
2026-01-27 21:57:55 +00:00
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByMonitorId({
2026-03-11 11:50:51 -07:00
monitorId: validatedParams.monitorId,
2026-01-27 21:57:55 +00:00
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 {
2026-03-11 11:50:51 -07:00
const validatedParams = deleteMaintenanceWindowByIdParamValidation.parse(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
2026-03-11 11:50:51 -07:00
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: validatedParams.id, teamId });
2025-07-23 15:14:47 -07:00
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 {
2026-03-11 11:50:51 -07:00
const validatedParams = editMaintenanceWindowByIdParamValidation.parse(req.params);
const validatedBody = editMaintenanceByIdWindowBodyValidation.parse(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
2026-01-27 21:57:55 +00:00
const editedMaintenanceWindow = await this.maintenanceWindowService.editMaintenanceWindow({
2026-03-11 11:50:51 -07:00
id: validatedParams.id,
body: validatedBody,
2026-01-27 21:57:55 +00:00
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;