Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.1 kB ยท 101 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import { eq } from "@openstatus/db";import { type Incident, incident } from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, withTransaction } from "../context";import { appendIncidentEvent, assertMember, assertNotClosed, getIncidentInWorkspace, requireIncidentFeature, userDisplayName,} from "./internal";import { UpdateIncidentInput } from "./schemas";
/** Edits title, severity, summary, commander and start time. */export async function updateIncident(args: { ctx: ServiceContext; input: UpdateIncidentInput;}): Promise<Incident> { const { ctx } = args; requireScope(ctx, "write"); requireIncidentFeature(ctx); const input = UpdateIncidentInput.parse(args.input);
return withTransaction(ctx, async (tx) => { const existing = await getIncidentInWorkspace( tx, ctx.workspace.id, input.id, ); assertNotClosed(existing);
const patch: Partial<typeof incident.$inferInsert> = {}; if (input.title !== undefined && input.title !== existing.title) { patch.title = input.title; } if (input.severity !== undefined && input.severity !== existing.severity) { patch.severity = input.severity; } if (input.summary !== undefined && input.summary !== existing.summary) { patch.summary = input.summary; } if ( input.commanderId !== undefined && input.commanderId !== existing.commanderId ) { if (input.commanderId !== null) { await assertMember(tx, ctx.workspace.id, input.commanderId); } patch.commanderId = input.commanderId; } if ( input.startedAt !== undefined && input.startedAt.getTime() !== existing.startedAt.getTime() ) { patch.startedAt = input.startedAt; } if (Object.keys(patch).length === 0) return existing;
const updated = await tx .update(incident) .set({ ...patch, updatedAt: new Date() }) .where(eq(incident.id, existing.id)) .returning() .get();
await emitAudit(tx, ctx, { action: "incident.update", entityType: "incident", entityId: updated.id, before: existing, after: updated, });
if (patch.severity !== undefined) { await appendIncidentEvent(tx, ctx, { incidentId: updated.id, type: "severity_changed", message: `Severity changed from ${existing.severity} to ${updated.severity}`, }); } if (patch.commanderId !== undefined) { const name = await userDisplayName(tx, updated.commanderId); await appendIncidentEvent(tx, ctx, { incidentId: updated.id, type: "commander_changed", message: name ? `Commander set to ${name}` : "Commander removed", }); } if (patch.startedAt !== undefined) { await appendIncidentEvent(tx, ctx, { incidentId: updated.id, type: "started_at_changed", message: `Start time changed to ${updated.startedAt.toISOString()}`, }); } return updated; });}