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

173 lines
5.8 KiB
TypeScript
Raw Normal View History

2026-01-09 15:21:56 -08:00
import { Request, Response, NextFunction } from "express";
2026-03-01 17:35:00 +00:00
import {
createStatusPageBodyValidation,
getStatusPageParamValidation,
getStatusPageQueryValidation,
imageValidation,
} from "@/validation/statusPageValidation.js";
2026-01-09 15:21:56 -08:00
import { AppError } from "@/utils/AppError.js";
2026-01-22 20:57:20 +00:00
import { requireTeamId, requireUserId } from "@/controllers/controllerUtils.js";
import { IStatusPageService } from "@/service/business/statusPageService.js";
import { IMonitorsRepository } from "@/repositories/index.js";
import { ISettingsService } from "@/service/system/settingsService.js";
import { NormalizeData } from "@/utils/dataUtils.js";
2026-01-09 15:21:56 -08:00
const SERVICE_NAME = "statusPageController";
2026-03-20 11:39:19 -07:00
export interface IStatusPageController {
readonly serviceName: string;
createStatusPage(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
updateStatusPage(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
getStatusPageByUrl(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
getStatusPagesByTeamId(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
deleteStatusPage(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
}
class StatusPageController implements IStatusPageController {
2026-01-09 15:21:56 -08:00
static SERVICE_NAME = SERVICE_NAME;
2026-01-22 20:57:20 +00:00
private statusPageService: IStatusPageService;
private monitorsRepository: IMonitorsRepository;
private settingsService: ISettingsService;
constructor(statusPageService: IStatusPageService, monitorsRepository: IMonitorsRepository, settingsService: ISettingsService) {
this.statusPageService = statusPageService;
this.monitorsRepository = monitorsRepository;
this.settingsService = settingsService;
2026-01-09 15:21:56 -08:00
}
get serviceName() {
return StatusPageController.SERVICE_NAME;
}
createStatusPage = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-02 19:25:34 +00:00
createStatusPageBodyValidation.parse(req.body);
if (req.file) {
imageValidation.parse(req.file);
}
2026-01-09 15:21:56 -08:00
2026-01-22 20:57:20 +00:00
const teamId = requireTeamId(req?.user?.teamId);
const userId = requireUserId(req?.user?.id);
2026-04-29 10:33:46 -07:00
const statusPage = await this.statusPageService.createStatusPage(userId, teamId, req.file, req.body);
2026-01-22 20:57:20 +00:00
2026-01-09 15:21:56 -08:00
return res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:21:56 -08:00
msg: "Status page created successfully",
2026-04-29 10:33:46 -07:00
data: statusPage,
2026-01-09 15:21:56 -08:00
});
} catch (error) {
next(error);
}
};
updateStatusPage = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-02 19:25:34 +00:00
createStatusPageBodyValidation.parse(req.body);
if (req.file) {
imageValidation.parse(req.file);
}
2026-01-22 20:57:20 +00:00
const teamId = requireTeamId(req?.user?.teamId);
2026-01-27 21:17:57 +00:00
const statusPageId = req.params.id as string;
2026-01-22 20:57:20 +00:00
if (!statusPageId) {
throw new AppError({ message: "Status page ID is required", status: 400 });
}
2026-04-29 10:33:46 -07:00
const statusPage = await this.statusPageService.updateStatusPage(statusPageId, teamId, req.file, req.body);
2026-01-09 15:21:56 -08:00
if (statusPage === null) {
throw new AppError({ message: "Status page not found", status: 404 });
}
2026-03-20 11:39:19 -07:00
res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:21:56 -08:00
msg: "Status page updated successfully",
2026-04-29 10:33:46 -07:00
data: statusPage,
2026-01-09 15:21:56 -08:00
});
} catch (error) {
next(error);
}
};
getStatusPageByUrl = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-02 19:25:34 +00:00
getStatusPageParamValidation.parse(req.params);
getStatusPageQueryValidation.parse(req.query);
2026-01-09 15:21:56 -08:00
2026-01-22 20:57:20 +00:00
if (!req.params.url) {
throw new AppError({ message: "Status page URL is required", status: 400 });
}
2026-01-27 21:17:57 +00:00
const statusPage = await this.statusPageService.getStatusPageByUrl(req.params.url as string);
2026-02-20 00:34:43 +05:30
if (!statusPage.isPublished) {
const teamId = requireTeamId(req?.user?.teamId);
if (statusPage.teamId !== teamId) {
throw new AppError({ message: "Forbidden", status: 403 });
}
}
2026-01-22 20:57:20 +00:00
const settings = await this.settingsService.getDBSettings();
const showURL = settings.showURL;
2026-02-02 19:00:03 +00:00
const monitors = await this.monitorsRepository.findByIds(statusPage.monitors);
2026-02-03 22:07:59 +00:00
// Sort monitors according to the order in statusPage.monitors
const monitorOrder = new Map(statusPage.monitors.map((id, index) => [id, index]));
const sortedMonitors = [...monitors].sort((a, b) => {
const orderA = monitorOrder.get(a.id) ?? Number.MAX_SAFE_INTEGER;
const orderB = monitorOrder.get(b.id) ?? Number.MAX_SAFE_INTEGER;
return orderA - orderB;
});
const normalizedMonitors = sortedMonitors.map((monitor) => {
2026-01-27 19:24:02 +00:00
const normalizedChecks = NormalizeData(monitor.recentChecks, 10, 100);
2026-01-22 20:57:20 +00:00
if (!showURL) {
2026-03-04 17:50:27 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2026-01-22 20:57:20 +00:00
const { url, port, secret, notifications, ...rest } = monitor;
return { ...rest, checks: normalizedChecks };
}
return { ...monitor, checks: normalizedChecks };
});
2026-01-09 15:21:56 -08:00
return res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:21:56 -08:00
msg: "Status page retrieved successfully",
data: {
2026-04-29 10:33:46 -07:00
statusPage,
monitors: normalizedMonitors,
},
2026-01-09 15:21:56 -08:00
});
} catch (error) {
next(error);
}
};
getStatusPagesByTeamId = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-01-22 20:57:20 +00:00
const teamId = requireTeamId(req.user?.teamId);
const statusPages = await this.statusPageService.getStatusPagesByTeamId(teamId);
2026-01-09 15:21:56 -08:00
return res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:21:56 -08:00
msg: "Status pages retrieved successfully",
2026-04-29 10:33:46 -07:00
data: statusPages,
2026-01-09 15:21:56 -08:00
});
} catch (error) {
next(error);
}
};
2026-01-22 20:57:20 +00:00
2026-01-09 15:21:56 -08:00
deleteStatusPage = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-01-27 21:17:57 +00:00
const statusPageId = req.params.id as string;
2026-01-22 20:57:20 +00:00
if (!statusPageId) {
throw new AppError({ message: "Status page ID is required", status: 400 });
}
const teamId = requireTeamId(req.user?.teamId);
await this.statusPageService.deleteStatusPage(statusPageId, teamId);
2026-01-09 15:21:56 -08:00
return res.status(200).json({
2026-01-09 15:32:53 -08:00
success: true,
2026-01-09 15:21:56 -08:00
msg: "Status page deleted successfully",
});
} catch (error) {
next(error);
}
};
}
export default StatusPageController;