Bulk close can close the osTicket tickets; Alerts top-level, Records group
Closing several alerts together can now also close their osTicket tickets (one by one, each written on its incident and in the audit log; one that fails does not stop the rest; Undo does not reopen tickets). The single and bulk close share one helper. Alerts moves to the top of the sidebar, and Monthly report and Change log move to a new Records group. Docs updated. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
215c0de8bf
commit
78822346b1
13 files changed
+234
-37
No files matched your search
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Closing the osTicket ticket that belongs to an alert, and writing down that it was: the same for one alert (Close, with the tick) and for several at once (Close, picked on the
|
||||
* Alerts page). The alert is closed either way — a ticket that could not be closed is said, never hidden, and never undoes the closing. Closing a ticket cannot be undone from here:
|
||||
* osTicket keeps it closed even if the alert is opened again.
|
||||
*/
|
||||
import { recordAudit } from "./audit.js";
|
||||
import { addNote } from "./incidents.js";
|
||||
import { closeTicketForAlert, ticketsForAlerts, type TicketCloseOutcome } from "./alertTickets.js";
|
||||
import { CloseError } from "../integrations/osticket/closeTicket.js";
|
||||
|
||||
type Actor = Parameters<typeof recordAudit>[0]["actor"];
|
||||
|
||||
export interface TicketResult {
|
||||
number: string | null;
|
||||
result: TicketCloseOutcome["result"] | "failed";
|
||||
message: string | null;
|
||||
}
|
||||
|
||||
const targetOf = (alert: { target: { type: string; id: number } | null }) => (alert.target ? { targetType: alert.target.type, targetId: alert.target.id } : {});
|
||||
|
||||
/** Closes the ticket of one alert and records it in the audit log and, when there is one, on the alert's incident. Never throws for a ticket that cannot be closed. */
|
||||
export async function closeTicketAndRecord(input: { alert: { id: string; target: { type: string; id: number } | null }; number: string | null; actor: Actor; who: string; reason: string | null; incidentId?: number }): Promise<TicketResult> {
|
||||
const { alert, number, actor, who, reason, incidentId } = input;
|
||||
try {
|
||||
const outcome = await closeTicketForAlert(alert.id, { closedBy: who, reason });
|
||||
await recordAudit({ actor, category: "alert_ticket", action: "close_ticket", ...targetOf(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.");
|
||||
return { number: outcome.number, result: outcome.result, message: null };
|
||||
} catch (err) {
|
||||
const message = err instanceof CloseError ? err.message : "Closing the ticket failed: " + (err instanceof Error ? err.message : String(err));
|
||||
await recordAudit({ actor, category: "alert_ticket", action: "close_ticket_failed", ...targetOf(alert), detail: { alertId: alert.id, number, message } });
|
||||
if (incidentId) await addNote(incidentId, who, "osTicket ticket" + (number ? " " + number : "") + " could not be closed: " + message);
|
||||
return { number, result: "failed", message };
|
||||
}
|
||||
}
|
||||
|
||||
export interface BulkTicketResult {
|
||||
/** Closed in osTicket now. */
|
||||
closed: number;
|
||||
/** Not open any more in osTicket, so nothing was changed. */
|
||||
alreadyClosed: number;
|
||||
/** Could be closed from here but was not: why, for each. */
|
||||
failed: { alertId: string; number: string | null; message: string }[];
|
||||
/** Has a ticket that is not closed from here: a summary ticket for several alerts, one already closed from here, or an osTicket with no login for closing. */
|
||||
notClosable: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the tickets of these alerts, one after another, for those that have a ticket that can be closed from here (the others are counted, not tried). `incidentIds` is the open
|
||||
* incident of each alert, if it has one. One that fails does not stop the rest.
|
||||
*/
|
||||
export async function closeTicketsOfAlerts(alerts: { id: string; target: { type: string; id: number } | null }[], ctx: { actor: Actor; who: string; reason: string | null; incidentIds: Map<string, number> }): Promise<BulkTicketResult> {
|
||||
const result: BulkTicketResult = { closed: 0, alreadyClosed: 0, failed: [], notClosable: 0 };
|
||||
const tickets = await ticketsForAlerts(alerts.map((a) => a.id));
|
||||
for (const alert of alerts) {
|
||||
const ticket = tickets.get(alert.id);
|
||||
if (!ticket || !ticket.number) continue;
|
||||
if (!ticket.canClose) {
|
||||
result.notClosable++;
|
||||
continue;
|
||||
}
|
||||
const r = await closeTicketAndRecord({ alert, number: ticket.number, actor: ctx.actor, who: ctx.who, reason: ctx.reason, incidentId: ctx.incidentIds.get(alert.id) });
|
||||
if (r.result === "closed") result.closed++;
|
||||
else if (r.result === "already_closed") result.alreadyClosed++;
|
||||
else result.failed.push({ alertId: alert.id, number: r.number, message: r.message ?? "it could not be closed" });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user