Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions apps/backend/src/routes/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export async function analyticsRoutes(
// Count unique viewers
// In raw SQL this is `SELECT COUNT(DISTINCT viewer_id) FROM card_views WHERE owner_id = ?`
// Prisma group-by as workaround:
const uniqueViewersQuery =
await app.prisma.cardView.groupBy({
by: ['viewerId', 'viewerIp'],
where: { ownerId: userId },
});

const uniqueViewers = uniqueViewersQuery.length;
const uniqueViewersQuery = await app.prisma.$queryRaw<[{ count: bigint }]>`
SELECT COUNT(DISTINCT viewer_id) AS count
FROM card_views
WHERE owner_id = ${userId}
AND viewer_id IS NOT NULL
`;

const uniqueViewers = Number(uniqueViewersQuery[0]?.count ?? 0);

return {
totalViews,
Expand Down