Skip to content

Open-source status page, incident response and uptime monitoring

3.7 KB
Raw Blame
import { Code, ConnectError } from "@connectrpc/connect";
import { count, db, eq } from "@openstatus/db";
import { page, pageComponent } from "@openstatus/db/src/schema";
import type { Limits } from "@openstatus/db/src/schema/plan/schema";

/**
 * Check workspace limits for creating a new status page.
 * Throws ConnectError with PermissionDenied if limit is exceeded.
 */
export async function checkStatusPageLimits(
  workspaceId: number,
  limits: Limits,
): Promise<void> {
  // Check status page count limit
  const countResult = await db
    .select({ count: count() })
    .from(page)
    .where(eq(page.workspaceId, workspaceId))
    .get();

  const currentCount = countResult?.count ?? 0;
  if (currentCount >= limits["status-pages"]) {
    throw new ConnectError(
      "Upgrade for more status pages",
      Code.PermissionDenied,
    );
  }
}

/**
 * Check if custom domain feature is available on the workspace plan.
 * Throws ConnectError with PermissionDenied if not available.
 */
export function checkCustomDomainLimit(limits: Limits): void {
  if (!limits["custom-domain"]) {
    throw new ConnectError("Upgrade for custom domains", Code.PermissionDenied);
  }
}

/**
 * Check if password protection feature is available on the workspace plan.
 * Throws ConnectError with PermissionDenied if not available.
 */
export function checkPasswordProtectionLimit(limits: Limits): void {
  if (!limits["password-protection"]) {
    throw new ConnectError(
      "Upgrade for password protection",
      Code.PermissionDenied,
    );
  }
}

/**
 * Check if email domain protection feature is available on the workspace plan.
 * Throws ConnectError with PermissionDenied if not available.
 */
export function checkEmailDomainProtectionLimit(limits: Limits): void {
  if (!limits["email-domain-protection"]) {
    throw new ConnectError(
      "Upgrade for email domain protection",
      Code.PermissionDenied,
    );
  }
}

/**
 * Check if IP restriction feature is available on the workspace plan.
 * Throws ConnectError with PermissionDenied if not available.
 */
export function checkIpRestrictionLimit(limits: Limits): void {
  if (!limits["ip-restriction"]) {
    throw new ConnectError("Upgrade for IP restriction", Code.PermissionDenied);
  }
}

/**
 * Check if no-index feature is available on the workspace plan.
 * Throws ConnectError with PermissionDenied if not available.
 */
export function checkNoIndexLimit(limits: Limits): void {
  if (!limits["no-index"]) {
    throw new ConnectError(
      "Upgrade for search engine indexing toggle",
      Code.PermissionDenied,
    );
  }
}

/**
 * Check if the custom theme feature is available on the workspace plan.
 * Throws ConnectError with PermissionDenied if not available.
 */
export function checkCustomThemeLimit(limits: Limits): void {
  if (!limits["custom-theme"]) {
    throw new ConnectError("Upgrade for custom theme", Code.PermissionDenied);
  }
}

export function checkStatusSubscribersLimit(limits: Limits): void {
  if (!limits["status-subscribers"]) {
    throw new ConnectError(
      "Upgrade to use status subscribers",
      Code.PermissionDenied,
    );
  }
}

/**
 * Check workspace limits for creating a new page component.
 * Throws ConnectError with PermissionDenied if limit is exceeded.
 */
export async function checkPageComponentLimits(
  pageId: number,
  limits: Limits,
): Promise<void> {
  const countResult = await db
    .select({ count: count() })
    .from(pageComponent)
    .where(eq(pageComponent.pageId, pageId))
    .get();

  const currentCount = countResult?.count ?? 0;
  if (currentCount >= limits["page-components"]) {
    throw new ConnectError(
      "Upgrade for more page components",
      Code.PermissionDenied,
    );
  }
}