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.
1.8 kB ยท 57 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import { eq, sql } from "@openstatus/db";import { maintenanceUpdate } 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 { touchMaintenance } from "./add-update";import { getMaintenanceUpdateInWorkspace } from "./internal";import { DeleteMaintenanceUpdateInput } from "./schemas";
export async function deleteMaintenanceUpdate(args: { ctx: ServiceContext; input: DeleteMaintenanceUpdateInput;}): Promise<void> { const { ctx } = args; requireScope(ctx, "write"); const input = DeleteMaintenanceUpdateInput.parse(args.input); const actorUserId = tryGetActorUserId(ctx.actor);
await withTransaction(ctx, async (tx) => { const existing = await getMaintenanceUpdateInWorkspace({ tx, id: input.id, workspaceId: ctx.workspace.id, });
// the newest update is the maintenance's public message const remaining = await tx .select({ count: sql<number>`count(*)` }) .from(maintenanceUpdate) .where(eq(maintenanceUpdate.maintenanceId, existing.maintenanceId)) .get(); if ((remaining?.count ?? 0) <= 1) { throw new ConflictError( "A maintenance needs at least one update. Delete the maintenance instead.", ); }
await tx .delete(maintenanceUpdate) .where(eq(maintenanceUpdate.id, existing.id)); await touchMaintenance(tx, existing.maintenanceId, actorUserId);
await emitAudit(tx, ctx, { action: "maintenance_update.delete", entityType: "maintenance_update", entityId: existing.id, before: existing, metadata: { maintenanceId: existing.maintenanceId }, }); });}