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

166 lines
5.3 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,
resolveStatusPageQueryValidation,
2026-06-09 12:34:15 -07:00
} from "@/api/validation/statusPageValidation.js";
2026-01-09 15:21:56 -08:00
import { AppError } from "@/utils/AppError.js";
2026-06-09 12:34:15 -07:00
import { requireTeamId, requireUserId } from "@/api/controllers/controllerUtils.js";
import { IStatusPageService } from "@/domain/status-pages/status-page.service.js";
import { resolveStatusPageDomainFromRequest } from "@/utils/statusPageDomain.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>;
resolveStatusPageByDomain(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
2026-03-20 11:39:19 -07:00
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;
constructor(statusPageService: IStatusPageService) {
2026-01-22 20:57:20 +00:00
this.statusPageService = statusPageService;
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);
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId);
2026-02-20 00:34:43 +05:30
return res.status(200).json({
success: true,
msg: "Status page retrieved successfully",
data,
});
} catch (error) {
next(error);
}
};
resolveStatusPageByDomain = async (req: Request, res: Response, next: NextFunction) => {
try {
resolveStatusPageQueryValidation.parse(req.query);
const domain = resolveStatusPageDomainFromRequest(req.hostname, req.query.domain as string | undefined);
if (!domain) {
throw new AppError({ message: "Domain is required", status: 400 });
2026-02-20 00:34:43 +05:30
}
const statusPage = await this.statusPageService.getStatusPageByCustomDomain(domain);
if (!statusPage.isPublished) {
throw new AppError({ message: "Status page not found", status: 404 });
}
2026-01-22 20:57:20 +00:00
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId);
2026-02-03 22:07:59 +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 retrieved successfully",
data,
2026-01-09 15:21:56 -08:00
});
} catch (error) {
next(error);
}
};
2026-01-09 15:21:56 -08:00
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;