Skip to content

Open-source status page, incident response and uptime monitoring

fix: trpc issue (#1896)

Thibault Le Ouay42a7b95ee9c0
diff --git a/packages/api/src/router/maintenance.test.ts b/packages/api/src/router/maintenance.test.ts
index 156c48668..0acdc264b 100644
--- a/packages/api/src/router/maintenance.test.ts
+++ b/packages/api/src/router/maintenance.test.ts
@@ -3,6 +3,7 @@ import { db, eq } from "@openstatus/db";
import {
maintenance,
maintenancesToPageComponents,
+ page,
} from "@openstatus/db/src/schema";
import { TRPCError } from "@trpc/server";
@@ -10,8 +11,22 @@ import { edgeRouter } from "../edge";
import { createInnerTRPCContext } from "../trpc";
let otherWorkspaceMaintenanceId: number;
+let otherWorkspacePageId: number;
beforeAll(async () => {
+ const p = await db
+ .insert(page)
+ .values({
+ workspaceId: 2,
+ title: "Maintenance IDOR Test Page",
+ description: "Page for maintenance IDOR testing",
+ slug: "maintenance-idor-test-page",
+ customDomain: "",
+ })
+ .returning()
+ .get();
+ otherWorkspacePageId = p.id;
+
// Maintenance for workspace 2, referencing page 1 (page ownership is not
// enforced at DB level, only at API level, so this insert works for testing)
const m = await db
@@ -49,6 +64,7 @@ afterAll(async () => {
await db
.delete(maintenance)
.where(eq(maintenance.id, otherWorkspaceMaintenanceId));
+ await db.delete(page).where(eq(page.id, otherWorkspacePageId));
});
test("maintenance.update rejects maintenance from another workspace", async () => {
@@ -109,3 +125,55 @@ test("maintenance.update succeeds for own workspace maintenance", async () => {
});
expect(updated?.title).toBe("Updated Maintenance Title");
});
+
+test("maintenance.new rejects pageId from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.maintenance.new({
+ title: "Cross-workspace maintenance",
+ message: "Should be rejected",
+ pageId: otherWorkspacePageId, // page from workspace 2
+ startDate: new Date(),
+ endDate: new Date(Date.now() + 3_600_000),
+ pageComponents: [],
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("NOT_FOUND");
+ }
+});
+
+test("maintenance.new succeeds for own workspace page", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ const result = await caller.maintenance.new({
+ title: "Valid Maintenance",
+ message: "Own workspace maintenance",
+ pageId: 1, // page 1 belongs to workspace 1
+ startDate: new Date(),
+ endDate: new Date(Date.now() + 3_600_000),
+ pageComponents: [1],
+ });
+
+ expect(result).toBeDefined();
+
+ // Cleanup
+ await db
+ .delete(maintenancesToPageComponents)
+ .where(eq(maintenancesToPageComponents.maintenanceId, result.id));
+ await db.delete(maintenance).where(eq(maintenance.id, result.id));
+});
diff --git a/packages/api/src/router/maintenance.ts b/packages/api/src/router/maintenance.ts
index 2b2ed6b25..d70cae931 100644
--- a/packages/api/src/router/maintenance.ts
+++ b/packages/api/src/router/maintenance.ts
@@ -4,6 +4,7 @@ import { type SQL, and, asc, desc, eq, gte, inArray } from "@openstatus/db";
import {
maintenance,
maintenancesToPageComponents,
+ page,
pageComponent,
selectMaintenanceSchema,
selectPageComponentSchema,
@@ -97,6 +98,20 @@ export const maintenanceRouter = createTRPCRouter({
}),
)
.mutation(async (opts) => {
+ const existingPage = await opts.ctx.db.query.page.findFirst({
+ where: and(
+ eq(page.id, opts.input.pageId),
+ eq(page.workspaceId, opts.ctx.workspace.id),
+ ),
+ });
+
+ if (!existingPage) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Page not found.",
+ });
+ }
+
// Check if the user has access to the monitors
if (opts.input.pageComponents?.length) {
const whereConditions: SQL[] = [
diff --git a/packages/api/src/router/monitor.test.ts b/packages/api/src/router/monitor.test.ts
index ae4cff535..08637c3f3 100644
--- a/packages/api/src/router/monitor.test.ts
+++ b/packages/api/src/router/monitor.test.ts
@@ -3,6 +3,7 @@ import { db, eq } from "@openstatus/db";
import {
monitorTag,
monitorTagsToMonitors,
+ privateLocation,
privateLocationToMonitors,
} from "@openstatus/db/src/schema";
import { allPlans } from "@openstatus/db/src/schema/plan/config";
@@ -12,6 +13,8 @@ import { edgeRouter } from "../edge";
import { createInnerTRPCContext } from "../trpc";
let tagId: number;
+let otherWorkspacePrivateLocationId: number;
+let ownWorkspacePrivateLocationId: number;
beforeAll(async () => {
const tag = await db
@@ -24,6 +27,28 @@ beforeAll(async () => {
.returning()
.get();
tagId = tag.id;
+
+ const otherLoc = await db
+ .insert(privateLocation)
+ .values({
+ workspaceId: 2,
+ name: "Other workspace private location",
+ token: "monitor-test-other-token",
+ })
+ .returning()
+ .get();
+ otherWorkspacePrivateLocationId = otherLoc.id;
+
+ const ownLoc = await db
+ .insert(privateLocation)
+ .values({
+ workspaceId: 1,
+ name: "Own workspace private location",
+ token: "monitor-test-own-token",
+ })
+ .returning()
+ .get();
+ ownWorkspacePrivateLocationId = ownLoc.id;
});
afterAll(async () => {
@@ -31,6 +56,20 @@ afterAll(async () => {
.delete(monitorTagsToMonitors)
.where(eq(monitorTagsToMonitors.monitorTagId, tagId));
await db.delete(monitorTag).where(eq(monitorTag.id, tagId));
+ await db
+ .delete(privateLocationToMonitors)
+ .where(
+ eq(
+ privateLocationToMonitors.privateLocationId,
+ ownWorkspacePrivateLocationId,
+ ),
+ );
+ await db
+ .delete(privateLocation)
+ .where(eq(privateLocation.id, otherWorkspacePrivateLocationId));
+ await db
+ .delete(privateLocation)
+ .where(eq(privateLocation.id, ownWorkspacePrivateLocationId));
});
test("monitor.updateTags rejects monitor from another workspace", async () => {
@@ -152,3 +191,51 @@ test("monitor.updateSchedulingRegions succeeds for own workspace monitor", async
privateLocations: [],
});
});
+
+test("monitor.updateSchedulingRegions rejects privateLocations from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1, limits: allPlans.team.limits },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.monitor.updateSchedulingRegions({
+ id: 1, // own monitor
+ regions: ["ams"],
+ periodicity: "1m",
+ privateLocations: [otherWorkspacePrivateLocationId], // workspace 2
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+});
+
+test("monitor.updateSchedulingRegions succeeds with own workspace privateLocations", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1, limits: allPlans.team.limits },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ await caller.monitor.updateSchedulingRegions({
+ id: 1,
+ regions: ["ams"],
+ periodicity: "1m",
+ privateLocations: [ownWorkspacePrivateLocationId],
+ });
+
+ const associations = await db.query.privateLocationToMonitors.findMany({
+ where: eq(privateLocationToMonitors.monitorId, 1),
+ });
+ const linked = associations.find(
+ (a) => a.privateLocationId === ownWorkspacePrivateLocationId,
+ );
+ expect(linked).toBeDefined();
+});
diff --git a/packages/api/src/router/monitor.ts b/packages/api/src/router/monitor.ts
index 4d93a2843..c93541c3c 100644
--- a/packages/api/src/router/monitor.ts
+++ b/packages/api/src/router/monitor.ts
@@ -27,6 +27,7 @@ import {
monitorsToStatusReport,
notification,
notificationsToMonitors,
+ privateLocation,
privateLocationToMonitors,
selectIncidentSchema,
selectMonitorSchema,
@@ -442,6 +443,21 @@ export const monitorRouter = createTRPCRouter({
});
}
+ if (input.privateLocations && input.privateLocations.length > 0) {
+ const validLocations = await ctx.db.query.privateLocation.findMany({
+ where: and(
+ eq(privateLocation.workspaceId, ctx.workspace.id),
+ inArray(privateLocation.id, input.privateLocations),
+ ),
+ });
+ if (validLocations.length !== input.privateLocations.length) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Invalid private location IDs.",
+ });
+ }
+ }
+
await ctx.db.transaction(async (tx) => {
await tx
.update(monitor)
diff --git a/packages/api/src/router/pageComponent.test.ts b/packages/api/src/router/pageComponent.test.ts
new file mode 100644
index 000000000..94535587b
--- /dev/null
+++ b/packages/api/src/router/pageComponent.test.ts
@@ -0,0 +1,127 @@
+import { afterAll, beforeAll, expect, test } from "bun:test";
+import { db, eq } from "@openstatus/db";
+import { monitor } from "@openstatus/db/src/schema";
+import { allPlans } from "@openstatus/db/src/schema/plan/config";
+import { TRPCError } from "@trpc/server";
+
+import { edgeRouter } from "../edge";
+import { createInnerTRPCContext } from "../trpc";
+
+let otherWorkspaceMonitorId: number;
+
+beforeAll(async () => {
+ const m = await db
+ .insert(monitor)
+ .values({
+ workspaceId: 2,
+ name: "Other workspace monitor",
+ jobType: "http",
+ periodicity: "1m",
+ url: "https://example.com",
+ regions: "ams",
+ active: true,
+ })
+ .returning()
+ .get();
+ otherWorkspaceMonitorId = m.id;
+});
+
+afterAll(async () => {
+ await db.delete(monitor).where(eq(monitor.id, otherWorkspaceMonitorId));
+});
+
+test("pageComponent.updateOrder rejects monitorId from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1, limits: allPlans.team.limits },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.pageComponent.updateOrder({
+ pageId: 1, // page 1 belongs to workspace 1
+ components: [
+ {
+ id: 1,
+ monitorId: otherWorkspaceMonitorId, // monitor from workspace 2
+ order: 0,
+ name: "Injected Monitor",
+ type: "monitor",
+ },
+ ],
+ groups: [],
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+});
+
+test("pageComponent.updateOrder rejects monitorId from another workspace in groups", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1, limits: allPlans.team.limits },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.pageComponent.updateOrder({
+ pageId: 1,
+ components: [],
+ groups: [
+ {
+ order: 0,
+ name: "Injected Group",
+ components: [
+ {
+ monitorId: otherWorkspaceMonitorId, // monitor from workspace 2
+ order: 0,
+ name: "Injected Monitor in Group",
+ type: "monitor",
+ },
+ ],
+ },
+ ],
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+});
+
+test("pageComponent.updateOrder succeeds with own workspace monitorId", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1, limits: allPlans.team.limits },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ const result = await caller.pageComponent.updateOrder({
+ pageId: 1,
+ components: [
+ {
+ monitorId: 1, // monitor 1 belongs to workspace 1
+ order: 0,
+ name: "OpenStatus Monitor",
+ type: "monitor",
+ },
+ {
+ monitorId: 2, // monitor 2 belongs to workspace 1
+ order: 1,
+ name: "Google Monitor",
+ type: "monitor",
+ },
+ ],
+ groups: [],
+ });
+
+ expect(result).toEqual({ success: true });
+});
diff --git a/packages/api/src/router/pageComponent.ts b/packages/api/src/router/pageComponent.ts
index 614b362c4..2776b2120 100644
--- a/packages/api/src/router/pageComponent.ts
+++ b/packages/api/src/router/pageComponent.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { type SQL, and, asc, desc, eq, inArray, sql } from "@openstatus/db";
import {
+ monitor,
pageComponent,
pageComponentGroup,
selectMaintenanceSchema,
@@ -180,6 +181,21 @@ export const pageComponentRouter = createTRPCRouter({
),
] as number[];
+ if (inputMonitorIds.length > 0) {
+ const validMonitors = await tx.query.monitor.findMany({
+ where: and(
+ eq(monitor.workspaceId, opts.ctx.workspace.id),
+ inArray(monitor.id, inputMonitorIds),
+ ),
+ });
+ if (validMonitors.length !== inputMonitorIds.length) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Invalid monitor IDs.",
+ });
+ }
+ }
+
// Collect IDs for static components that have IDs in input
const inputStaticComponentIds = [
...opts.input.components
diff --git a/packages/api/src/router/privateLocation.test.ts b/packages/api/src/router/privateLocation.test.ts
index 014d0dd34..c35db682b 100644
--- a/packages/api/src/router/privateLocation.test.ts
+++ b/packages/api/src/router/privateLocation.test.ts
@@ -112,3 +112,79 @@ test("privateLocation.update succeeds for own workspace location", async () => {
expect(result).toBeDefined();
expect(result.name).toBe("Updated Location Name");
});
+
+test("privateLocation.new rejects monitors from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.privateLocation.new({
+ name: "Injected Location",
+ token: "test-token-inject",
+ monitors: [5], // monitor 5 belongs to workspace 3
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+});
+
+test("privateLocation.new succeeds with own workspace monitors", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ const result = await caller.privateLocation.new({
+ name: "Valid Location",
+ token: "test-token-valid-new",
+ monitors: [1], // monitor 1 belongs to workspace 1
+ });
+
+ expect(result).toBeDefined();
+ expect(result.name).toBe("Valid Location");
+
+ // Verify the monitor association was created
+ const associations = await db.query.privateLocationToMonitors.findMany({
+ where: eq(privateLocationToMonitors.privateLocationId, result.id),
+ });
+ expect(associations.length).toBe(1);
+ expect(associations[0].monitorId).toBe(1);
+
+ // Cleanup
+ await db
+ .delete(privateLocationToMonitors)
+ .where(eq(privateLocationToMonitors.privateLocationId, result.id));
+ await db.delete(privateLocation).where(eq(privateLocation.id, result.id));
+});
+
+test("privateLocation.update rejects monitors from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.privateLocation.update({
+ id: ownWorkspaceLocationId,
+ name: "Updated Location Name",
+ monitors: [5], // monitor 5 belongs to workspace 3
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+});
diff --git a/packages/api/src/router/privateLocation.ts b/packages/api/src/router/privateLocation.ts
index 69ffd2bb4..a94e390ef 100644
--- a/packages/api/src/router/privateLocation.ts
+++ b/packages/api/src/router/privateLocation.ts
@@ -1,5 +1,6 @@
-import { and, eq } from "@openstatus/db";
+import { and, eq, inArray } from "@openstatus/db";
import {
+ monitor,
privateLocation,
privateLocationToMonitors,
} from "@openstatus/db/src/schema";
@@ -37,6 +38,21 @@ export const privateLocationRouter = createTRPCRouter({
}),
)
.mutation(async (opts) => {
+ if (opts.input.monitors.length) {
+ const validMonitors = await opts.ctx.db.query.monitor.findMany({
+ where: and(
+ eq(monitor.workspaceId, opts.ctx.workspace.id),
+ inArray(monitor.id, opts.input.monitors),
+ ),
+ });
+ if (validMonitors.length !== opts.input.monitors.length) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Invalid monitor IDs.",
+ });
+ }
+ }
+
return await opts.ctx.db.transaction(async (tx) => {
const _privateLocation = await tx
.insert(privateLocation)
@@ -82,6 +98,21 @@ export const privateLocationRouter = createTRPCRouter({
});
}
+ if (opts.input.monitors.length) {
+ const validMonitors = await opts.ctx.db.query.monitor.findMany({
+ where: and(
+ eq(monitor.workspaceId, opts.ctx.workspace.id),
+ inArray(monitor.id, opts.input.monitors),
+ ),
+ });
+ if (validMonitors.length !== opts.input.monitors.length) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Invalid monitor IDs.",
+ });
+ }
+ }
+
return await opts.ctx.db.transaction(async (tx) => {
const _privateLocation = await tx
.update(privateLocation)
diff --git a/packages/api/src/router/statusReport.test.ts b/packages/api/src/router/statusReport.test.ts
index d3400448b..987399e1a 100644
--- a/packages/api/src/router/statusReport.test.ts
+++ b/packages/api/src/router/statusReport.test.ts
@@ -2,6 +2,7 @@ import { afterAll, beforeAll, expect, test } from "bun:test";
import { db, eq } from "@openstatus/db";
import {
page,
+ pageComponent,
statusReport,
statusReportsToPageComponents,
} from "@openstatus/db/src/schema";
@@ -12,6 +13,7 @@ import { createInnerTRPCContext } from "../trpc";
let otherWorkspacePageId: number;
let otherWorkspaceReportId: number;
+let otherWorkspaceComponentId: number;
beforeAll(async () => {
const p = await db
@@ -27,6 +29,18 @@ beforeAll(async () => {
.get();
otherWorkspacePageId = p.id;
+ const component = await db
+ .insert(pageComponent)
+ .values({
+ workspaceId: 2,
+ pageId: otherWorkspacePageId,
+ name: "Other workspace component",
+ type: "static",
+ })
+ .returning()
+ .get();
+ otherWorkspaceComponentId = component.id;
+
const report = await db
.insert(statusReport)
.values({
@@ -54,6 +68,9 @@ afterAll(async () => {
await db
.delete(statusReport)
.where(eq(statusReport.id, otherWorkspaceReportId));
+ await db
+ .delete(pageComponent)
+ .where(eq(pageComponent.id, otherWorkspaceComponentId));
await db.delete(page).where(eq(page.id, otherWorkspacePageId));
});
@@ -160,3 +177,57 @@ test("statusReport.updateStatus succeeds for own workspace report", async () =>
});
expect(updated?.title).toBe("Updated Title");
});
+
+test("statusReport.create rejects pageComponents from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.statusReport.create({
+ title: "Cross-workspace injection",
+ status: "investigating",
+ pageId: 1, // valid page for workspace 1
+ pageComponents: [otherWorkspaceComponentId], // component from workspace 2
+ date: new Date(),
+ message: "Should be rejected",
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+});
+
+test("statusReport.updateStatus rejects pageComponents from another workspace", async () => {
+ const ctx = createInnerTRPCContext({
+ req: undefined,
+ session: { user: { id: "1" } },
+ // @ts-expect-error - minimal workspace for test
+ workspace: { id: 1 },
+ });
+ const caller = edgeRouter.createCaller(ctx);
+
+ try {
+ await caller.statusReport.updateStatus({
+ id: 1, // valid report for workspace 1
+ pageComponents: [otherWorkspaceComponentId], // component from workspace 2
+ title: "Cross-workspace injection",
+ status: "investigating",
+ });
+ throw new Error("Should have thrown");
+ } catch (e) {
+ expect(e).toBeInstanceOf(TRPCError);
+ expect((e as TRPCError).code).toBe("FORBIDDEN");
+ }
+
+ // Verify the report was NOT modified
+ const report = await db.query.statusReport.findFirst({
+ where: eq(statusReport.id, 1),
+ });
+ expect(report?.title).toBe("Updated Title"); // still the value from the previous test
+});
diff --git a/packages/api/src/router/statusReport.ts b/packages/api/src/router/statusReport.ts
index ceb3cda6d..bd21b4d78 100644
--- a/packages/api/src/router/statusReport.ts
+++ b/packages/api/src/router/statusReport.ts
@@ -4,6 +4,7 @@ import { type SQL, and, asc, desc, eq, gte } from "@openstatus/db";
import {
insertStatusReportUpdateSchema,
page,
+ pageComponent,
selectPageComponentSchema,
selectPageSchema,
selectStatusReportSchema,
@@ -211,6 +212,25 @@ export const statusReportRouter = createTRPCRouter({
});
}
+ if (opts.input.pageComponents.length > 0) {
+ const components = await opts.ctx.db.query.pageComponent.findMany({
+ where: and(
+ eq(pageComponent.pageId, opts.input.pageId),
+ eq(pageComponent.workspaceId, opts.ctx.workspace.id),
+ ),
+ });
+ const validIds = new Set(components.map((c) => c.id));
+ const invalid = opts.input.pageComponents.filter(
+ (id) => !validIds.has(id),
+ );
+ if (invalid.length > 0) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Invalid page component IDs.",
+ });
+ }
+ }
+
return opts.ctx.db.transaction(async (tx) => {
const newStatusReport = await tx
.insert(statusReport)
@@ -271,13 +291,32 @@ export const statusReportRouter = createTRPCRouter({
),
});
- if (!existing) {
+ if (!existing || !existing.pageId) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Status report not found",
});
}
+ if (opts.input.pageComponents.length > 0) {
+ const components = await opts.ctx.db.query.pageComponent.findMany({
+ where: and(
+ eq(pageComponent.pageId, existing.pageId),
+ eq(pageComponent.workspaceId, opts.ctx.workspace.id),
+ ),
+ });
+ const validIds = new Set(components.map((c) => c.id));
+ const invalid = opts.input.pageComponents.filter(
+ (id) => !validIds.has(id),
+ );
+ if (invalid.length > 0) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Invalid page component IDs.",
+ });
+ }
+ }
+
await opts.ctx.db.transaction(async (tx) => {
await tx
.update(statusReport)