2026-07-15 10:02:36 -07:00
|
|
|
import { Request, Response, RequestHandler } from "express";
|
|
|
|
|
import { catchAsync } from "@/utils/catchAsync.js";
|
2026-01-09 15:21:56 -08:00
|
|
|
|
2026-03-01 17:35:00 +00:00
|
|
|
import {
|
|
|
|
|
createStatusPageBodyValidation,
|
|
|
|
|
getStatusPageParamValidation,
|
|
|
|
|
getStatusPageQueryValidation,
|
|
|
|
|
imageValidation,
|
2026-06-07 22:37:24 -04:00
|
|
|
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";
|
2026-06-07 22:37:24 -04:00
|
|
|
import { resolveStatusPageDomainFromRequest } from "@/utils/statusPageDomain.js";
|
2026-01-09 15:21:56 -08:00
|
|
|
|
2026-03-20 11:39:19 -07:00
|
|
|
export interface IStatusPageController {
|
2026-07-15 10:02:36 -07:00
|
|
|
createStatusPage: RequestHandler;
|
|
|
|
|
updateStatusPage: RequestHandler;
|
|
|
|
|
getStatusPageByUrl: RequestHandler;
|
|
|
|
|
resolveStatusPageByDomain: RequestHandler;
|
|
|
|
|
getStatusPagesByTeamId: RequestHandler;
|
|
|
|
|
deleteStatusPage: RequestHandler;
|
2026-03-20 11:39:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class StatusPageController implements IStatusPageController {
|
2026-01-22 20:57:20 +00:00
|
|
|
private statusPageService: IStatusPageService;
|
2026-07-07 09:47:29 -07:00
|
|
|
constructor(statusPageService: IStatusPageService) {
|
2026-01-22 20:57:20 +00:00
|
|
|
this.statusPageService = statusPageService;
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
|
|
|
|
|
2026-07-15 10:02:36 -07:00
|
|
|
createStatusPage = catchAsync(async (req: Request, res: Response) => {
|
|
|
|
|
createStatusPageBodyValidation.parse(req.body);
|
|
|
|
|
if (req.file) {
|
|
|
|
|
imageValidation.parse(req.file);
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
2026-07-15 10:02:36 -07:00
|
|
|
|
|
|
|
|
const teamId = requireTeamId(req?.user?.teamId);
|
|
|
|
|
const userId = requireUserId(req?.user?.id);
|
|
|
|
|
const statusPage = await this.statusPageService.createStatusPage(userId, teamId, req.file, req.body);
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Status page created successfully",
|
|
|
|
|
data: statusPage,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
updateStatusPage = catchAsync(async (req: Request, res: Response) => {
|
|
|
|
|
createStatusPageBodyValidation.parse(req.body);
|
|
|
|
|
if (req.file) {
|
|
|
|
|
imageValidation.parse(req.file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const teamId = requireTeamId(req?.user?.teamId);
|
|
|
|
|
const statusPageId = req.params.id as string;
|
|
|
|
|
if (!statusPageId) {
|
|
|
|
|
throw new AppError({ message: "Status page ID is required", status: 400 });
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
2026-07-15 10:02:36 -07:00
|
|
|
const statusPage = await this.statusPageService.updateStatusPage(statusPageId, teamId, req.file, req.body);
|
|
|
|
|
if (statusPage === null) {
|
|
|
|
|
throw new AppError({ message: "Status page not found", status: 404 });
|
2026-06-07 22:37:24 -04:00
|
|
|
}
|
2026-07-15 10:02:36 -07:00
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Status page updated successfully",
|
|
|
|
|
data: statusPage,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getStatusPageByUrl = catchAsync(async (req: Request, res: Response) => {
|
|
|
|
|
getStatusPageParamValidation.parse(req.params);
|
2026-08-17 12:31:01 -07:00
|
|
|
const { range } = getStatusPageQueryValidation.parse(req.query);
|
2026-07-15 10:02:36 -07:00
|
|
|
|
|
|
|
|
if (!req.params.url) {
|
|
|
|
|
throw new AppError({ message: "Status page URL is required", status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const statusPage = await this.statusPageService.getStatusPageByUrl(req.params.url as string);
|
2026-08-17 12:31:01 -07:00
|
|
|
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId, range);
|
2026-07-15 10:02:36 -07:00
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Status page retrieved successfully",
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
resolveStatusPageByDomain = catchAsync(async (req: Request, res: Response) => {
|
2026-08-17 12:31:01 -07:00
|
|
|
const { range } = resolveStatusPageQueryValidation.parse(req.query);
|
2026-07-15 10:02:36 -07:00
|
|
|
const domain = resolveStatusPageDomainFromRequest(req.hostname, req.query.domain as string | undefined);
|
|
|
|
|
if (!domain) {
|
|
|
|
|
throw new AppError({ message: "Domain is required", status: 400 });
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
2026-07-15 10:02:36 -07:00
|
|
|
|
|
|
|
|
const statusPage = await this.statusPageService.getStatusPageByCustomDomain(domain);
|
|
|
|
|
if (!statusPage.isPublished) {
|
|
|
|
|
throw new AppError({ message: "Status page not found", status: 404 });
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
2026-07-15 10:02:36 -07:00
|
|
|
|
2026-08-17 12:31:01 -07:00
|
|
|
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId, range);
|
2026-07-15 10:02:36 -07:00
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Status page retrieved successfully",
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getStatusPagesByTeamId = catchAsync(async (req: Request, res: Response) => {
|
|
|
|
|
const teamId = requireTeamId(req.user?.teamId);
|
|
|
|
|
const statusPages = await this.statusPageService.getStatusPagesByTeamId(teamId);
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Status pages retrieved successfully",
|
|
|
|
|
data: statusPages,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
deleteStatusPage = catchAsync(async (req: Request, res: Response) => {
|
|
|
|
|
const statusPageId = req.params.id as string;
|
|
|
|
|
if (!statusPageId) {
|
|
|
|
|
throw new AppError({ message: "Status page ID is required", status: 400 });
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
2026-07-15 10:02:36 -07:00
|
|
|
const teamId = requireTeamId(req.user?.teamId);
|
|
|
|
|
await this.statusPageService.deleteStatusPage(statusPageId, teamId);
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
msg: "Status page deleted successfully",
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-01-09 15:21:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default StatusPageController;
|