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

138 lines
4.0 KiB
TypeScript

import { Request, Response, NextFunction } from "express";
import {
createMaintenanceWindowBodyValidation,
editMaintenanceWindowByIdParamValidation,
editMaintenanceByIdWindowBodyValidation,
getMaintenanceWindowByIdParamValidation,
getMaintenanceWindowsByMonitorIdParamValidation,
getMaintenanceWindowsByTeamIdQueryValidation,
deleteMaintenanceWindowByIdParamValidation,
} from "@/validation/maintenanceWindowValidation.js";
import { requireTeamId } from "@/controllers/controllerUtils.js";
const SERVICE_NAME = "maintenanceWindowController";
class MaintenanceWindowController {
static SERVICE_NAME = SERVICE_NAME;
private maintenanceWindowService: any;
constructor(maintenanceWindowService: any) {
this.maintenanceWindowService = maintenanceWindowService;
}
get serviceName() {
return MaintenanceWindowController.SERVICE_NAME;
}
createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => {
try {
createMaintenanceWindowBodyValidation.parse(req.body);
const teamId = requireTeamId(req?.user?.teamId);
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, body: req.body });
return res.status(200).json({
success: true,
msg: "Maintenance window created successfully",
});
} catch (error) {
next(error);
}
};
getMaintenanceWindowById = async (req: Request, res: Response, next: NextFunction) => {
try {
getMaintenanceWindowByIdParamValidation.parse(req.params);
const teamId = requireTeamId(req.user?.teamId);
const maintenanceWindow = await this.maintenanceWindowService.getMaintenanceWindowById({ id: req.params.id as string, teamId });
return res.status(200).json({
success: true,
msg: "Maintenance window fetched successfully",
data: maintenanceWindow,
});
} catch (error) {
next(error);
}
};
getMaintenanceWindowsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
try {
getMaintenanceWindowsByTeamIdQueryValidation.parse(req.query);
const teamId = requireTeamId(req?.user?.teamId);
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByTeamId({ teamId, query: req.query });
return res.status(200).json({
success: true,
msg: "Maintenance windows fetched successfully",
data: maintenanceWindows,
});
} catch (error) {
next(error);
}
};
getMaintenanceWindowsByMonitorId = async (req: Request, res: Response, next: NextFunction) => {
try {
getMaintenanceWindowsByMonitorIdParamValidation.parse(req.params);
const teamId = requireTeamId(req?.user?.teamId);
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByMonitorId({
monitorId: req.params.monitorId as string,
teamId,
});
return res.status(200).json({
success: true,
msg: "Maintenance windows fetched successfully",
data: maintenanceWindows,
});
} catch (error) {
next(error);
}
};
deleteMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
try {
deleteMaintenanceWindowByIdParamValidation.parse(req.params);
const teamId = requireTeamId(req?.user?.teamId);
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: req.params.id as string, teamId });
return res.status(200).json({
success: true,
msg: "Maintenance window deleted successfully",
});
} catch (error) {
next(error);
}
};
editMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
try {
editMaintenanceWindowByIdParamValidation.parse(req.params);
editMaintenanceByIdWindowBodyValidation.parse(req.body);
const teamId = requireTeamId(req.user?.teamId);
const editedMaintenanceWindow = await this.maintenanceWindowService.editMaintenanceWindow({
id: req.params.id as string,
body: req.body,
teamId,
});
return res.status(200).json({
success: true,
msg: "Maintenance window edited successfully",
data: editedMaintenanceWindow,
});
} catch (error) {
next(error);
}
};
}
export default MaintenanceWindowController;