Close the osTicket ticket along with an alert

The Close drawer can also close the alert's osTicket ticket. osTicket's API
cannot, so this writes to its database with a separate, optional login: the
ticket is set to Closed, a note in its thread says who closed it and why, and
the closed event is recorded, in one transaction. Tables and columns are
checked first, only an open ticket is closed, and a ticket closed by somebody
else at the same moment is left alone. Summary tickets are not closed from here.

"Test closing" (Alert tickets settings) tries every write and rolls it back.
If the ticket cannot be closed the alert is still closed and the reason is
said, noted on the incident and audited.

Not run against a real osTicket: tested against a pretend database only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-25 21:27:38 +01:00
1 parent a94899865e
commit 5ae61a1ece
21 files changed
+8405 -23

No files matched your search

+14 -2
View File
@@ -5,7 +5,8 @@ import { requireAuth, requireRole } from "../auth/middleware.js";
import { db } from "../db/client.js";
import { integrations } from "../db/schema.js";
import { recordAudit } from "../services/audit.js";
import { getAlertTicketStatus, resetBaseline, runAlertTicketCheck, sendTestTicket } from "../services/alertTickets.js";
import { getAlertTicketStatus, resetBaseline, runAlertTicketCheck, sendTestTicket, testClosing } from "../services/alertTickets.js";
import { hasCloseAccess, type OsTicketCloseConfig } from "../integrations/osticket/closeTicket.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { describeSettingsChanges } from "../services/settingsDiff.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
@@ -34,13 +35,15 @@ async function overview() {
const osticket = [];
for (const row of rows) {
let ready = false;
let closeReady = false;
try {
const loaded = await loadIntegrationConfig(row.id);
ready = loaded ? hasTicketApi(loaded.config as OsTicketApiConfig) : false;
closeReady = loaded ? hasCloseAccess(loaded.config as OsTicketCloseConfig) : false;
} catch {
ready = false;
}
osticket.push({ id: row.id, name: row.name, enabled: row.enabled, ticketApiReady: ready });
osticket.push({ id: row.id, name: row.name, enabled: row.enabled, ticketApiReady: ready, closeReady });
}
return { settings: (await getSettings()).alertTickets, integrations: osticket, categories: CATEGORIES, status: await getAlertTicketStatus() };
}
@@ -113,6 +116,15 @@ alertTicketsRouter.post("/test", asyncHandler(async (req, res) => {
}
}));
// "Test closing": the login, the tables and the writes a close makes, tried inside a transaction that is rolled back. Changes nothing that stays.
alertTicketsRouter.post("/test-close", asyncHandler(async (req, res) => {
const parsed = z.object({ integrationId: z.number().int().positive() }).safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say which osTicket integration." });
const report = await testClosing(parsed.data.integrationId);
await recordAudit({ actor: req.currentUser!, category: "alert_ticket", action: "test_close", targetType: "integration", targetId: parsed.data.integrationId, detail: { ok: report.ok } });
res.json(report);
}));
alertTicketsRouter.post("/run", asyncHandler(async (req, res) => {
const summary = await runAlertTicketCheck({ actor: req.currentUser! });
res.json({ summary, status: await getAlertTicketStatus() });
+21 -2
View File
@@ -8,7 +8,8 @@ import { AckError, assignableUsers, attentionCounts, clearAck, loadAcks, MAX_HOU
import { recordAudit } from "../services/audit.js";
import { addNote, openIncidentIds } from "../services/incidents.js";
import { buildNoiseReport, NOISE_DAYS } from "../services/alertNoise.js";
import { ticketsForAlerts, type AlertTicketInfo } from "../services/alertTickets.js";
import { closeTicketForAlert, ticketsForAlerts, type AlertTicketInfo, type TicketCloseOutcome } from "../services/alertTickets.js";
import { CloseError } from "../integrations/osticket/closeTicket.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const alertsRouter = Router();
@@ -138,6 +139,8 @@ const closeSchema = z.object({
alertId: z.string().min(1).max(600),
/** Why — shown on the alert and written into its incident. */
note: z.string().max(MAX_CLOSE_NOTE).nullable().optional(),
/** Also close the osTicket ticket opened for it, if there is one that can be closed from here. */
closeTicket: z.boolean().optional(),
});
// "I have dealt with it, or I accept it": the alert is hidden with no end, until it gets worse or has gone for half an hour. The reason goes on the incident, so the history says why.
@@ -155,7 +158,23 @@ alertsRouter.put("/close", requireRole("operator"), asyncHandler(async (req, res
if (incidentId) await addNote(incidentId, who, closed.note ? "Closed: " + closed.note : "Closed without a note.");
await recordAudit({ actor, category: "alert", action: "close", ...auditTargetOf(alert), detail: { alertId: alert.id, message: alert.message, note: closed.note } });
invalidateAlertsCache();
res.json({ closed: { by: closed.snoozedBy, at: closed.snoozedAt, note: closed.note } });
// The ticket, if asked for. The alert is closed either way: a ticket that could not be closed is said, not hidden, and does not undo the closing.
let ticket: { number: string | null; result: TicketCloseOutcome["result"] | "failed"; message: string | null } | null = null;
if (parsed.data.closeTicket) {
const number = (await ticketsForAlerts([alert.id])).get(alert.id)?.number ?? null;
try {
const outcome = await closeTicketForAlert(alert.id, { closedBy: who, reason: closed.note });
ticket = { number: outcome.number, result: outcome.result, message: null };
await recordAudit({ actor, category: "alert_ticket", action: "close_ticket", ...auditTargetOf(alert), detail: { alertId: alert.id, number: outcome.number, result: outcome.result } });
if (incidentId) await addNote(incidentId, who, outcome.result === "closed" ? "osTicket ticket " + outcome.number + " closed." : "osTicket ticket " + outcome.number + " was already closed.");
} catch (err) {
const message = err instanceof CloseError ? err.message : "Closing the ticket failed: " + (err instanceof Error ? err.message : String(err));
ticket = { number, result: "failed", message };
await recordAudit({ actor, category: "alert_ticket", action: "close_ticket_failed", ...auditTargetOf(alert), detail: { alertId: alert.id, number, message } });
if (incidentId) await addNote(incidentId, who, "osTicket ticket" + (number ? " " + number : "") + " could not be closed: " + message);
}
}
res.json({ closed: { by: closed.snoozedBy, at: closed.snoozedAt, note: closed.note }, ticket });
} catch (err) {
if (err instanceof SnoozeError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;