import type { Monitor } from "@/domain/monitors/monitor.type.js"; import type { Check } from "@/domain/checks/check.type.js"; import { AppError } from "@/utils/AppError.js"; import { getDateForRange } from "@/utils/dataUtils.js"; import type { IIncidentsRepository } from "@/domain/incidents/incident.repository.interface.js"; import type { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js"; import type { IUsersRepository } from "@/domain/users/user.repository.interface.js"; import type { Incident, IncidentSummary } from "@/domain/incidents/incident.type.js"; import type { User } from "@/domain/users/user.type.js"; import type { MonitorActionDecision } from "@/worker/worker.interface.js"; import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js"; import type { ILogger } from "@/utils/logger.js"; import { DateRange } from "@/types/query.js"; const SERVICE_NAME = "incidentService"; export interface IIncidentService { handleIncident(monitor: Monitor, decision: MonitorActionDecision, check: Check): Promise; resolveIncident(incidentId: string, userId: string, teamId: string, comment?: string, userEmail?: string): Promise; getIncidentsByTeam( teamId: string, sortOrder: string, dateRange: DateRange, page: number, rowsPerPage: number, status: boolean | undefined, monitorId: string | undefined, resolutionType: string | undefined ): Promise<{ incidents: Incident[]; count: number }>; getIncidentSummary(teamId: string, limit?: number): Promise; getIncidentById(incidentId: string, teamId: string): Promise<{ incident: Incident; monitor: Monitor; user: User | null }>; } export class IncidentService implements IIncidentService { static SERVICE_NAME = SERVICE_NAME; private logger: ILogger; private incidentsRepository: IIncidentsRepository; private monitorsRepository: IMonitorsRepository; private usersRepository: IUsersRepository; private notificationMessageBuilder: INotificationMessageBuilder; constructor( logger: ILogger, incidentsRepository: IIncidentsRepository, monitorsRepository: IMonitorsRepository, usersRepository: IUsersRepository, notificationMessageBuilder: INotificationMessageBuilder ) { this.logger = logger; this.incidentsRepository = incidentsRepository; this.monitorsRepository = monitorsRepository; this.usersRepository = usersRepository; this.notificationMessageBuilder = notificationMessageBuilder; } handleIncident = async (monitor: Monitor, decision: MonitorActionDecision, check: Check): Promise => { const { transition } = decision; if (transition === null) return null; const activeIncident = await this.incidentsRepository.findActiveByMonitorId(monitor.id, monitor.teamId); switch (transition) { case "status_down": case "threshold_breach": { if (activeIncident) return activeIncident; // A threshold breach has no HTTP status const isBreach = transition === "threshold_breach"; return await this.incidentsRepository.create({ monitorId: monitor.id, teamId: monitor.teamId, startTime: Date.now().toString(), status: true, statusCode: isBreach ? 9999 : check.statusCode, message: isBreach ? this.notificationMessageBuilder.buildThresholdBreachMessage(monitor, check, decision.thresholdBreaches) : undefined, }); } case "status_up": case "threshold_resolved": { if (!activeIncident) return null; activeIncident.status = false; activeIncident.endTime = Date.now().toString(); activeIncident.resolutionType = "automatic"; return await this.incidentsRepository.updateById(activeIncident.id, activeIncident.teamId, activeIncident); } } }; resolveIncident = async (incidentId: string, userId: string, teamId: string, comment?: string, userEmail?: string) => { try { if (!incidentId) { throw new AppError({ message: "No incident ID in request", service: SERVICE_NAME, method: "resolveIncident" }); } if (!userId) { throw new AppError({ message: "No user ID in request", service: SERVICE_NAME, method: "resolveIncident" }); } if (!teamId) { throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "resolveIncident" }); } const incident = await this.incidentsRepository.findActiveByIncidentId(incidentId, teamId); if (!incident) { throw new AppError({ message: "Incident not found", service: SERVICE_NAME, method: "resolveIncident" }); } if (incident.status === false) { throw new AppError({ message: "Incident is already resolved", service: SERVICE_NAME, method: "resolveIncident" }); } incident.resolutionType = "manual"; incident.status = false; incident.resolvedBy = userId; incident.resolvedByEmail = userEmail || null; incident.comment = comment || null; incident.endTime = Date.now().toString(); const resolvedIncident = await this.incidentsRepository.updateById(incident.id, teamId, incident); this.logger.debug({ service: SERVICE_NAME, method: "resolveIncidentManually", message: `Incident manually resolved by user`, details: { incidentId: resolvedIncident.id }, }); return resolvedIncident; } catch (error: unknown) { this.logger.error({ service: SERVICE_NAME, method: "resolveIncident", message: error instanceof Error ? error.message : "Unknown error", details: { id: incidentId }, stack: error instanceof Error ? error.stack : undefined, }); throw error; } }; getIncidentsByTeam = async ( teamId: string, sortOrder: string, dateRange: DateRange, page: number, rowsPerPage: number, status: boolean | undefined, monitorId: string | undefined, resolutionType: string | undefined ) => { try { if (!teamId) { throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getIncidentsByTeam", status: 400 }); } const startDate = getDateForRange(dateRange); const parsedPage = page ?? 0; const parsedRowsPerPage = rowsPerPage ?? 20; const incidents = await this.incidentsRepository.findByTeamId( teamId, startDate, parsedPage, parsedRowsPerPage, sortOrder, status, monitorId, resolutionType ); const count = await this.incidentsRepository.countByTeamId(teamId, startDate, status, monitorId, resolutionType); return { incidents, count }; } catch (error: unknown) { this.logger.error({ service: SERVICE_NAME, method: "getIncidentsByTeam", message: error instanceof Error ? error.message : "Unknown error", details: { teamId }, stack: error instanceof Error ? error.stack : undefined, }); throw error; } }; getIncidentSummary = async (teamId: string, limit?: number) => { try { if (!teamId) { throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getIncidentSummary", status: 400 }); } const parsedLimit = limit ?? 10; const summary = await this.incidentsRepository.findSummaryByTeamId(teamId, parsedLimit); return summary; } catch (error: unknown) { this.logger.error({ service: SERVICE_NAME, method: "getIncidentSummary", message: error instanceof Error ? error.message : "Unknown error", details: { teamId }, stack: error instanceof Error ? error.stack : undefined, }); throw error; } }; getIncidentById = async (incidentId: string, teamId: string) => { try { const incident = await this.incidentsRepository.findById(incidentId, teamId); const monitor = await this.monitorsRepository.findById(incident.monitorId, teamId); let user = null; if (incident.resolvedBy) { user = await this.usersRepository.findById(incident.resolvedBy); } return { incident, monitor, user }; } catch (error: unknown) { this.logger.error({ service: SERVICE_NAME, method: "getIncidentById", message: error instanceof Error ? error.message : "Unknown error", details: { incidentId }, stack: error instanceof Error ? error.stack : undefined, }); throw error; } }; }