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

106 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-01-20 18:56:06 +00:00
import { AppError } from "@/utils/AppError.js";
2026-01-09 15:44:11 -08:00
import { Request, Response, NextFunction } from "express";
2026-02-02 06:08:51 +00:00
import { requireTeamId, requireUserId, requireUserEmail } from "./controllerUtils.js";
2026-03-11 11:10:38 -07:00
import { IIncidentService } from "@/service/index.js";
import { getIncidentsByTeamQueryValidation, getIncidentSummaryQueryValidation } from "@/validation/incidentValidation.js";
2026-01-09 15:44:11 -08:00
2026-03-11 11:10:38 -07:00
const SERVICE_NAME = "IncidentController";
2026-03-20 11:50:47 -07:00
export interface IIncidentController {
getIncidentsByTeam: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
getIncidentSummary: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
getIncidentById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
resolveIncidentManually: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
}
class IncidentController implements IIncidentController {
2026-03-11 11:10:38 -07:00
private incidentService: IIncidentService;
constructor(incidentService: IIncidentService) {
2026-01-09 15:44:11 -08:00
this.incidentService = incidentService;
}
getIncidentsByTeam = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-11 11:10:38 -07:00
const validatedQuery = getIncidentsByTeamQueryValidation.parse(req.query);
2026-01-21 10:15:13 -08:00
const teamId = requireTeamId(req.user?.teamId);
const result = await this.incidentService.getIncidentsByTeam(
teamId,
2026-03-11 11:10:38 -07:00
validatedQuery.sortOrder,
validatedQuery.dateRange,
validatedQuery.page,
validatedQuery.rowsPerPage,
validatedQuery.status,
validatedQuery.monitorId,
validatedQuery.resolutionType
2026-01-21 10:15:13 -08:00
);
2026-01-09 15:44:11 -08:00
return res.status(200).json({
success: true,
msg: "Incidents retrieved successfully",
data: result,
});
} catch (error) {
next(error);
}
};
getIncidentSummary = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-02-02 06:09:57 +00:00
const teamId = requireTeamId(req.user?.teamId);
2026-03-11 11:10:38 -07:00
const validatedQuery = getIncidentSummaryQueryValidation.parse(req.query);
2026-01-20 18:56:06 +00:00
2026-03-11 11:10:38 -07:00
const summary = await this.incidentService.getIncidentSummary(teamId, validatedQuery.limit);
2026-01-09 15:44:11 -08:00
return res.status(200).json({
success: true,
msg: "Incident summary retrieved successfully",
data: summary,
});
} catch (error) {
next(error);
}
};
getIncidentById = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-01-22 21:28:28 +00:00
const teamId = requireTeamId(req.user?.teamId);
2026-01-27 21:54:20 +00:00
const incidentId = req.params.incidentId as string;
2026-01-22 21:28:28 +00:00
if (!incidentId) {
throw new AppError({ message: "Incident ID is required", service: SERVICE_NAME, status: 400 });
}
const incident = await this.incidentService.getIncidentById(incidentId, teamId);
2026-01-09 15:44:11 -08:00
return res.status(200).json({
success: true,
msg: "Incident retrieved successfully",
data: incident,
});
} catch (error) {
next(error);
}
};
resolveIncidentManually = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-02-02 06:08:51 +00:00
const teamId = requireTeamId(req.user?.teamId);
const userId = requireUserId(req.user?.id);
const userEmail = requireUserEmail(req.user?.email);
const incidentId = req.params?.incidentId;
if (!incidentId) {
throw new AppError({ message: "Incident ID is required", service: SERVICE_NAME, status: 400 });
}
const resolvedIncident = await this.incidentService.resolveIncident(incidentId, userId, teamId, req?.body?.comment, userEmail);
2026-01-09 15:44:11 -08:00
return res.status(200).json({
success: true,
msg: "Incident resolved successfully",
data: resolvedIncident,
});
} catch (error) {
next(error);
}
};
}
export default IncidentController;