Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(web): move the space analytics check out of the route file

Richie McIlroy6a15a5a8b7e3
diff --git a/apps/web/app/api/dashboard/analytics/route.ts b/apps/web/app/api/dashboard/analytics/route.ts
index 51b2793d2..5c80d175e 100644
--- a/apps/web/app/api/dashboard/analytics/route.ts
+++ b/apps/web/app/api/dashboard/analytics/route.ts
@@ -1,34 +1,14 @@
import { getCurrentUser } from "@cap/database/auth/session";
-import { AgentManagement } from "@cap/web-backend";
-import { Space, type User } from "@cap/web-domain";
-import { Effect } from "effect";
import type { NextRequest } from "next/server";
import { getOrgAnalyticsData } from "@/app/(org)/dashboard/analytics/data";
import type { AnalyticsRange } from "@/app/(org)/dashboard/analytics/types";
-import { runPromise } from "@/lib/server";
+import { canViewSpaceAnalytics } from "@/lib/space-analytics-access";
const RANGE_VALUES: AnalyticsRange[] = ["24h", "7d", "30d", "lifetime"];
export const dynamic = "force-dynamic";
-const canViewSpaceAnalytics = async (
- userId: User.UserId,
- orgId: string,
- spaceId: string,
-) => {
- if (spaceId === orgId) return true;
-
- return Effect.flatMap(AgentManagement, (management) =>
- management.getSpaceAccess(userId, Space.SpaceId.make(spaceId)),
- ).pipe(
- Effect.map((access) => access.organizationId === orgId),
- Effect.catchTag("PolicyDenied", () => Effect.succeed(false)),
- Effect.provide(AgentManagement.DefaultWithoutDependencies),
- runPromise,
- );
-};
-
export async function GET(request: NextRequest) {
const user = await getCurrentUser();
if (!user) return Response.json({ error: "Unauthorized" }, { status: 401 });
diff --git a/apps/web/lib/space-analytics-access.ts b/apps/web/lib/space-analytics-access.ts
new file mode 100644
index 000000000..a12e84b31
--- /dev/null
+++ b/apps/web/lib/space-analytics-access.ts
@@ -0,0 +1,23 @@
+import "server-only";
+
+import { AgentManagement } from "@cap/web-backend";
+import { Space, type User } from "@cap/web-domain";
+import { Effect } from "effect";
+import { runPromise } from "@/lib/server";
+
+export const canViewSpaceAnalytics = async (
+ userId: User.UserId,
+ orgId: string,
+ spaceId: string,
+) => {
+ if (spaceId === orgId) return true;
+
+ return Effect.flatMap(AgentManagement, (management) =>
+ management.getSpaceAccess(userId, Space.SpaceId.make(spaceId)),
+ ).pipe(
+ Effect.map((access) => access.organizationId === orgId),
+ Effect.catchTag("PolicyDenied", () => Effect.succeed(false)),
+ Effect.provide(AgentManagement.DefaultWithoutDependencies),
+ runPromise,
+ );
+};