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.1 kB ยท 70 lines
TypeScript
at incident-api
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { and, eq } from "@openstatus/db";import { privateLocation, selectPrivateLocationSchema,} from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, withTransaction } from "../context";import { NotFoundError } from "../errors";import type { PrivateLocation } from "../types";import { SetPrivateLocationStatusInput } from "./schemas";
/** * Set a private location's health status. Cron-only writer โ the value is * derived from the agent heartbeat, never from user input. Returns early * without a write or audit row when the status is unchanged. */export async function setPrivateLocationStatus(args: { ctx: ServiceContext; input: SetPrivateLocationStatusInput;}): Promise<PrivateLocation> { const { ctx } = args; requireScope(ctx, "write"); const input = SetPrivateLocationStatusInput.parse(args.input);
return withTransaction(ctx, async (tx) => { const existing = await tx.query.privateLocation.findFirst({ where: and( eq(privateLocation.id, input.id), eq(privateLocation.workspaceId, ctx.workspace.id), ), });
if (!existing) { throw new NotFoundError("private_location", input.id); }
const beforeParsed = selectPrivateLocationSchema.parse(existing); if (beforeParsed.status === input.status) { return beforeParsed; }
const row = await tx .update(privateLocation) .set({ status: input.status, updatedAt: new Date() }) .where( and( eq(privateLocation.id, input.id), eq(privateLocation.workspaceId, ctx.workspace.id), ), ) .returning() .get();
if (!row) throw new NotFoundError("private_location", input.id);
const afterParsed = selectPrivateLocationSchema.parse(row);
await emitAudit(tx, ctx, { action: "private_location.update", entityType: "private_location", entityId: afterParsed.id, before: beforeParsed, after: afterParsed, });
return afterParsed; });}