Skip to content

Open-source status page, incident response and uptime monitoring

add limit check api (#1806)

Thibault Le Ouayf979b98db4d7
diff --git a/apps/server/src/routes/rpc/services/status-page/__tests__/status-page.test.ts b/apps/server/src/routes/rpc/services/status-page/__tests__/status-page.test.ts
index e352bc634..e91470053 100644
--- a/apps/server/src/routes/rpc/services/status-page/__tests__/status-page.test.ts
+++ b/apps/server/src/routes/rpc/services/status-page/__tests__/status-page.test.ts
@@ -671,6 +671,78 @@ describe("StatusPageService.AddMonitorComponent", () => {
.delete(pageComponent)
.where(eq(pageComponent.id, Number(data.component.id)));
});
+
+ test("returns 403 when page component limit is exceeded", async () => {
+ // Workspace 2 is on free plan with page-components limit of 3
+ // Create a page for workspace 2
+ const limitTestPage = await db
+ .insert(page)
+ .values({
+ workspaceId: 2,
+ title: `${TEST_PREFIX}-component-limit-test`,
+ slug: `${TEST_PREFIX}-component-limit-test-slug`,
+ description: "Page for component limit test",
+ customDomain: "",
+ })
+ .returning()
+ .get();
+
+ // Create a monitor for workspace 2
+ const limitTestMonitor = await db
+ .insert(monitor)
+ .values({
+ workspaceId: 2,
+ name: `${TEST_PREFIX}-limit-monitor`,
+ url: "https://example.com",
+ periodicity: "1m",
+ active: true,
+ jobType: "http",
+ })
+ .returning()
+ .get();
+
+ // Create 3 components to hit the limit
+ const createdComponentIds: number[] = [];
+ for (let i = 0; i < 3; i++) {
+ const component = await db
+ .insert(pageComponent)
+ .values({
+ workspaceId: 2,
+ pageId: limitTestPage.id,
+ type: "static",
+ name: `${TEST_PREFIX}-limit-component-${i}`,
+ order: i,
+ })
+ .returning()
+ .get();
+ createdComponentIds.push(component.id);
+ }
+
+ try {
+ // Try to add a 4th component - should fail with PermissionDenied
+ const res = await connectRequest(
+ "AddMonitorComponent",
+ {
+ pageId: String(limitTestPage.id),
+ monitorId: String(limitTestMonitor.id),
+ name: `${TEST_PREFIX}-limit-exceeded-component`,
+ },
+ { "x-openstatus-key": "2" },
+ );
+
+ expect(res.status).toBe(403); // PermissionDenied
+
+ const data = await res.json();
+ expect(data.message).toContain("Upgrade for more page components");
+ } finally {
+ // Clean up
+ for (const id of createdComponentIds) {
+ await db.delete(pageComponent).where(eq(pageComponent.id, id));
+ }
+ await db.delete(monitor).where(eq(monitor.id, limitTestMonitor.id));
+ await db.delete(page).where(eq(page.id, limitTestPage.id));
+ }
+ });
});
describe("StatusPageService.AddStaticComponent", () => {
@@ -721,6 +793,63 @@ describe("StatusPageService.AddStaticComponent", () => {
expect(res.status).toBe(404);
});
+
+ test("returns 403 when page component limit is exceeded", async () => {
+ // Workspace 2 is on free plan with page-components limit of 3
+ // Create a page for workspace 2
+ const limitTestPage = await db
+ .insert(page)
+ .values({
+ workspaceId: 2,
+ title: `${TEST_PREFIX}-static-limit-test`,
+ slug: `${TEST_PREFIX}-static-limit-test-slug`,
+ description: "Page for static component limit test",
+ customDomain: "",
+ })
+ .returning()
+ .get();
+
+ // Create 3 components to hit the limit
+ const createdComponentIds: number[] = [];
+ for (let i = 0; i < 3; i++) {
+ const component = await db
+ .insert(pageComponent)
+ .values({
+ workspaceId: 2,
+ pageId: limitTestPage.id,
+ type: "static",
+ name: `${TEST_PREFIX}-static-limit-component-${i}`,
+ order: i,
+ })
+ .returning()
+ .get();
+ createdComponentIds.push(component.id);
+ }
+
+ try {
+ // Try to add a 4th component - should fail with PermissionDenied
+ const res = await connectRequest(
+ "AddStaticComponent",
+ {
+ pageId: String(limitTestPage.id),
+ name: `${TEST_PREFIX}-static-limit-exceeded`,
+ description: "Should fail due to limit",
+ },
+ { "x-openstatus-key": "2" },
+ );
+
+ expect(res.status).toBe(403); // PermissionDenied
+
+ const data = await res.json();
+ expect(data.message).toContain("Upgrade for more page components");
+ } finally {
+ // Clean up
+ for (const id of createdComponentIds) {
+ await db.delete(pageComponent).where(eq(pageComponent.id, id));
+ }
+ await db.delete(page).where(eq(page.id, limitTestPage.id));
+ }
+ });
});
describe("StatusPageService.RemoveComponent", () => {
diff --git a/apps/server/src/routes/rpc/services/status-page/index.ts b/apps/server/src/routes/rpc/services/status-page/index.ts
index b20503cf9..f56567e3b 100644
--- a/apps/server/src/routes/rpc/services/status-page/index.ts
+++ b/apps/server/src/routes/rpc/services/status-page/index.ts
@@ -53,7 +53,7 @@ import {
subscriberCreateFailedError,
subscriberNotFoundError,
} from "./errors";
-import { checkStatusPageLimits } from "./limits";
+import { checkPageComponentLimits, checkStatusPageLimits } from "./limits";
/**
* Helper to get a status page by ID with workspace scope.
@@ -319,6 +319,7 @@ export const statusPageServiceImpl: ServiceImpl<typeof StatusPageService> = {
async addMonitorComponent(req, ctx) {
const rpcCtx = getRpcContext(ctx);
const workspaceId = rpcCtx.workspace.id;
+ const limits = rpcCtx.workspace.limits;
// Verify page exists and belongs to workspace
const pageData = await getPageById(Number(req.pageId), workspaceId);
@@ -326,6 +327,9 @@ export const statusPageServiceImpl: ServiceImpl<typeof StatusPageService> = {
throw statusPageNotFoundError(req.pageId);
}
+ // Check workspace limits for page components
+ await checkPageComponentLimits(pageData.id, limits);
+
// Verify monitor exists and belongs to workspace
const monitorData = await getMonitorById(
Number(req.monitorId),
@@ -371,6 +375,7 @@ export const statusPageServiceImpl: ServiceImpl<typeof StatusPageService> = {
async addStaticComponent(req, ctx) {
const rpcCtx = getRpcContext(ctx);
const workspaceId = rpcCtx.workspace.id;
+ const limits = rpcCtx.workspace.limits;
// Verify page exists and belongs to workspace
const pageData = await getPageById(Number(req.pageId), workspaceId);
@@ -378,6 +383,9 @@ export const statusPageServiceImpl: ServiceImpl<typeof StatusPageService> = {
throw statusPageNotFoundError(req.pageId);
}
+ // Check workspace limits for page components
+ await checkPageComponentLimits(pageData.id, limits);
+
// Validate group exists if provided
if (req.groupId) {
const group = await getGroupById(Number(req.groupId), workspaceId);
diff --git a/apps/server/src/routes/rpc/services/status-page/limits.ts b/apps/server/src/routes/rpc/services/status-page/limits.ts
index aa6143e83..a65e3a7a2 100644
--- a/apps/server/src/routes/rpc/services/status-page/limits.ts
+++ b/apps/server/src/routes/rpc/services/status-page/limits.ts
@@ -1,6 +1,6 @@
import { Code, ConnectError } from "@connectrpc/connect";
import { count, db, eq } from "@openstatus/db";
-import { page } from "@openstatus/db/src/schema";
+import { page, pageComponent } from "@openstatus/db/src/schema";
import type { Limits } from "@openstatus/db/src/schema/plan/schema";
/**
@@ -62,3 +62,26 @@ export function checkEmailDomainProtectionLimit(limits: Limits): void {
);
}
}
+
+/**
+ * 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,
+ );
+ }
+}