Files
Homelab-manager/server/src/routes/incidents.ts
T
bobbanandClaude Sonnet 5.5 0277126add What changed before this? on every incident, ranked
- A card on the incident page, and GET /api/incidents/:id/before and /api/v1/incidents/:id/before: what the app knows happened in the 30 min to 24 h before an alert began (audit log, maintenance windows, update runs and patch plans, other alerts, a step in the server's own figures), scored by how related, what kind and how close
- A host going down is shown once; Likely, Possible and Background; at most ten
- Docs and roles updated

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 20:25:45 +02:00

62 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Router } from "express";
import { z } from "zod";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { addNote, getIncident, listIncidents, MAX_NOTE, removeNote } from "../services/incidents.js";
import { whatChangedBefore } from "../services/incidentBefore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const incidentsRouter = Router();
// Everyone signed in can read what happened — it is made of what the Alerts page, the server pages and the Maintenance page already show.
// Writing a note is for operators.
incidentsRouter.use(requireAuth);
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
incidentsRouter.get("/", asyncHandler(async (_req, res) => {
res.json({ incidents: await listIncidents() });
}));
incidentsRouter.get("/:id", asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const detail = await getIncident(id);
if (!detail) return res.status(404).json({ error: "not_found" });
res.json(detail);
}));
// What changed in the minutes before it began, ranked by how likely each is to be connected. `?minutes=` 15–1440 (120 by default).
incidentsRouter.get("/:id/before", asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const report = await whatChangedBefore(id, req.query.minutes === undefined ? undefined : Number(req.query.minutes));
if (!report) return res.status(404).json({ error: "not_found" });
res.json(report);
}));
const noteSchema = z.object({ body: z.string().trim().min(1, "Write something first.").max(MAX_NOTE) });
incidentsRouter.post("/:id/notes", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const parsed = noteSchema.safeParse(req.body);
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.success ? undefined : parsed.error.issues[0]?.message });
const actor = req.currentUser!;
const note = await addNote(id, actor.name ?? actor.email ?? actor.oidcSub, parsed.data.body);
if (!note) return res.status(404).json({ error: "not_found" });
await recordAudit({ actor, category: "incident", action: "note", targetType: "incident", targetId: id });
res.status(201).json({ id: note.id });
}));
incidentsRouter.delete("/:id/notes/:noteId", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const noteId = idOf(req.params.noteId);
if (!Number.isInteger(id) || !Number.isInteger(noteId)) return res.status(400).json({ error: "invalid_id" });
const actor = req.currentUser!;
const outcome = await removeNote(id, noteId, { name: actor.name ?? actor.email ?? actor.oidcSub, isAdmin: actor.role === "admin" });
if (outcome === "not_found") return res.status(404).json({ error: "not_found" });
if (outcome === "forbidden") return res.status(403).json({ error: "forbidden", message: "A note can be removed by the person who wrote it, or an administrator." });
await recordAudit({ actor, category: "incident", action: "note_removed", targetType: "incident", targetId: id });
res.status(204).end();
}));