diff --git a/src/app/api/events/route.ts b/src/app/api/events/route.ts index 29abb57386..5115154005 100644 --- a/src/app/api/events/route.ts +++ b/src/app/api/events/route.ts @@ -6,11 +6,18 @@ import { cloudEventSchema } from '@/schemas'; import { captureEvent } from '@/lib/server/analytics'; export async function POST(request: NextRequest) { - const { userId } = await auth(); + const { userId, orgId } = await auth(); if (!userId) { return NextResponse.json( - { error: 'Unauthorized request' }, + { error: 'Unauthorized: User required' }, + { status: 401 }, + ); + } + + if (!orgId) { + return NextResponse.json( + { error: 'Unauthorized: Organization required' }, { status: 401 }, ); } @@ -29,7 +36,7 @@ export async function POST(request: NextRequest) { } try { - await captureEvent({ id, userId, timestamp, event: result.data }); + await captureEvent({ id, orgId, userId, timestamp, event: result.data }); } catch (error) { console.error(error); diff --git a/src/db/clickhouse.ts b/src/db/clickhouse.ts index bb7759a675..b4fa7f679c 100644 --- a/src/db/clickhouse.ts +++ b/src/db/clickhouse.ts @@ -4,6 +4,7 @@ CREATE TABLE default.events ( -- Shared `id` UUID, + `orgId` String, `userId` String, `timestamp` Int32, `type` String, diff --git a/src/lib/server/analytics.ts b/src/lib/server/analytics.ts index 90274b29bb..2c4ac825ee 100644 --- a/src/lib/server/analytics.ts +++ b/src/lib/server/analytics.ts @@ -11,6 +11,7 @@ const client = createClient({ type AnalyticsEvent = { id: string; + orgId: string; userId: string; timestamp: number; event: CloudEvent;