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.2 kB ยท 104 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import { and, eq, inArray, isNull } from "@openstatus/db";import { monitor, page, pageComponent, pageComponentGroup,} from "@openstatus/db/src/schema";
import type { DB } from "../context";import { ForbiddenError, NotFoundError } from "../errors";
/** Assert a page is in the workspace. Throws `ForbiddenError` otherwise. */export async function assertPageInWorkspace(args: { tx: DB; pageId: number; workspaceId: number;}): Promise<void> { const { tx, pageId, workspaceId } = args; const row = await tx .select({ id: page.id }) .from(page) .where(and(eq(page.id, pageId), eq(page.workspaceId, workspaceId))) .get(); if (!row) throw new ForbiddenError("You don't have access to this page.");}
/** Load a component by id, scoped to the workspace. */export async function getPageComponentInWorkspace(args: { tx: DB; id: number; workspaceId: number;}) { const { tx, id, workspaceId } = args; const row = await tx .select() .from(pageComponent) .where( and(eq(pageComponent.id, id), eq(pageComponent.workspaceId, workspaceId)), ) .get(); if (!row) throw new NotFoundError("page_component", id); return row;}
/** * Assert a group exists and sits on `pageId`. Workspace scope alone isn't * enough โ a group from a sibling page would otherwise be accepted and the * component would render under a group it isn't on. Reported as not-found * so the check doesn't confirm the group exists on another page. */export async function assertGroupOnPage(args: { tx: DB; groupId: number; pageId: number; workspaceId: number;}): Promise<void> { const { tx, groupId, pageId, workspaceId } = args; const row = await tx .select({ pageId: pageComponentGroup.pageId }) .from(pageComponentGroup) .where( and( eq(pageComponentGroup.id, groupId), eq(pageComponentGroup.workspaceId, workspaceId), ), ) .get(); if (!row || row.pageId !== pageId) { throw new NotFoundError("page_component_group", groupId); }}
/** * Verify the supplied monitor ids all belong to the workspace. Duplicated * from `monitor/internal.ts` deliberately; a shared `packages/services/src/ * internal/` extraction is a natural follow-up once a third consumer * appears (tags-to-workspace may be a candidate). */export async function validateMonitorIds(args: { tx: DB; workspaceId: number; monitorIds: ReadonlyArray<number>;}): Promise<void> { const { tx, workspaceId, monitorIds } = args; if (monitorIds.length === 0) return; const ids = Array.from(new Set(monitorIds)); // Exclude soft-deleted monitors โ a deleted monitor's id shouldn't be // attachable to a fresh page component. Without this filter the row // count matches on ids pointing at rows that are already tombstoned. const rows = await tx .select({ id: monitor.id }) .from(monitor) .where( and( inArray(monitor.id, ids), eq(monitor.workspaceId, workspaceId), isNull(monitor.deletedAt), ), ) .all(); if (rows.length !== ids.length) { throw new ForbiddenError("Invalid monitor IDs."); }}