Skip to content

Commit f587ef1

Browse files
committed
fix(analytics): derive tenantId from DB-fetched ownerId instead of client-supplied value
1 parent 5ba1611 commit f587ef1

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

  • apps/web/app/api/analytics/track

apps/web/app/api/analytics/track/route.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ export async function POST(request: NextRequest) {
8282
sanitizeString(request.nextUrl.hostname) ||
8383
"";
8484

85-
const tenantId = body.orgId || (hostname ? `domain:${hostname}` : "public");
86-
8785
const pathname = body.pathname ?? `/s/${body.videoId}`;
8886

8987
await runPromise(
@@ -101,19 +99,24 @@ export async function POST(request: NextRequest) {
10199
return currentUser.id;
102100
},
103101
});
104-
if (userId) {
105-
const [videoRecord] = yield* Effect.tryPromise(() =>
106-
db()
107-
.select({ ownerId: videos.ownerId })
108-
.from(videos)
109-
.where(eq(videos.id, Video.VideoId.make(body.videoId)))
110-
.limit(1),
111-
).pipe(Effect.orElseSucceed(() => [] as { ownerId: string }[]));
112-
if (videoRecord && userId === videoRecord.ownerId) {
113-
return;
114-
}
102+
103+
const [videoRecord] = yield* Effect.tryPromise(() =>
104+
db()
105+
.select({ ownerId: videos.ownerId })
106+
.from(videos)
107+
.where(eq(videos.id, Video.VideoId.make(body.videoId)))
108+
.limit(1),
109+
).pipe(Effect.orElseSucceed(() => [] as { ownerId: string }[]));
110+
111+
if (userId && videoRecord && userId === videoRecord.ownerId) {
112+
return;
115113
}
116114

115+
const tenantId =
116+
body.orgId ||
117+
videoRecord?.ownerId ||
118+
(hostname ? `domain:${hostname}` : "public");
119+
117120
const tinybird = yield* Tinybird;
118121
yield* tinybird.appendEvents([
119122
{

0 commit comments

Comments
 (0)