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.
2.0 kB ยท 68 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import { and, eq, isNull } from "@openstatus/db";import { monitorIncidentTable } from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, tryGetActorUserId, withTransaction,} from "../context";import { ConflictError } from "../errors";import type { MonitorIncident } from "../types";import { getMonitorIncidentInWorkspace } from "./internal";import { AcknowledgeMonitorIncidentInput } from "./schemas";
export async function acknowledgeMonitorIncident(args: { ctx: ServiceContext; input: AcknowledgeMonitorIncidentInput;}): Promise<MonitorIncident> { const { ctx } = args; requireScope(ctx, "write"); const input = AcknowledgeMonitorIncidentInput.parse(args.input);
return withTransaction(ctx, async (tx) => { const existing = await getMonitorIncidentInWorkspace({ tx, id: input.id, workspaceId: ctx.workspace.id, }); if (existing.acknowledgedAt) { throw new ConflictError("Incident already acknowledged."); }
const now = new Date(); // Conditional update โ atomically flips `acknowledged_at` only while it // is still NULL. A concurrent acknowledger losing the race returns no // row, at which point we throw the same `ConflictError` the pre-read // would have raised. const updated = await tx .update(monitorIncidentTable) .set({ acknowledgedAt: now, acknowledgedBy: tryGetActorUserId(ctx.actor), updatedAt: now, }) .where( and( eq(monitorIncidentTable.id, existing.id), isNull(monitorIncidentTable.acknowledgedAt), ), ) .returning() .get(); if (!updated) { throw new ConflictError("Incident already acknowledged."); }
await emitAudit(tx, ctx, { action: "monitor_incident.update", entityType: "monitor_incident", entityId: updated.id, before: existing, after: updated, });
return updated; });}