|
| 1 | +import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; |
| 2 | + |
| 3 | +export async function analyticsRoutes(app: FastifyInstance) { |
| 4 | + |
| 5 | + app.get('/overview', { |
| 6 | + preHandler: [app.authenticate], |
| 7 | + }, async (request: FastifyRequest, reply: FastifyReply) => { |
| 8 | + const userId = (request.user as any).id; |
| 9 | + |
| 10 | + const today = new Date(); |
| 11 | + today.setHours(0, 0, 0, 0); |
| 12 | + |
| 13 | + const [totalViews, viewsToday, totalFollows, recentViews] = await Promise.all([ |
| 14 | + // Total views of this user's cards/profile |
| 15 | + app.prisma.cardView.count({ |
| 16 | + where: { ownerId: userId }, |
| 17 | + }), |
| 18 | + // Views today |
| 19 | + app.prisma.cardView.count({ |
| 20 | + where: { ownerId: userId, createdAt: { gte: today } }, |
| 21 | + }), |
| 22 | + // Follows performed BY this user |
| 23 | + app.prisma.followLog.count({ |
| 24 | + where: { followerId: userId, status: 'success' }, |
| 25 | + }), |
| 26 | + // Recent views (last 5) |
| 27 | + app.prisma.cardView.findMany({ |
| 28 | + where: { ownerId: userId }, |
| 29 | + orderBy: { createdAt: 'desc' }, |
| 30 | + take: 5, |
| 31 | + include: { |
| 32 | + viewer: { |
| 33 | + select: { displayName: true, avatarUrl: true }, |
| 34 | + }, |
| 35 | + card: { |
| 36 | + select: { title: true }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }), |
| 40 | + ]); |
| 41 | + |
| 42 | + // Count unique viewers |
| 43 | + // In raw SQL this is `SELECT COUNT(DISTINCT viewer_id) FROM card_views WHERE owner_id = ?` |
| 44 | + // Prisma group-by as workaround: |
| 45 | + const uniqueViewersQuery = await app.prisma.cardView.groupBy({ |
| 46 | + by: ['viewerId', 'viewerIp'], |
| 47 | + where: { ownerId: userId }, |
| 48 | + }); |
| 49 | + const uniqueViewers = uniqueViewersQuery.length; |
| 50 | + |
| 51 | + return { |
| 52 | + totalViews, |
| 53 | + viewsToday, |
| 54 | + totalFollows, |
| 55 | + uniqueViewers, |
| 56 | + recentViews, |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + app.get('/views', { |
| 61 | + preHandler: [app.authenticate], |
| 62 | + }, async (request: FastifyRequest<{ Querystring: { page?: string, cardId?: string } }>, reply: FastifyReply) => { |
| 63 | + const userId = (request.user as any).id; |
| 64 | + const page = parseInt(request.query.page || '1', 10); |
| 65 | + const limit = 20; |
| 66 | + const skip = (page - 1) * limit; |
| 67 | + |
| 68 | + const whereClause: any = { ownerId: userId }; |
| 69 | + if (request.query.cardId) { |
| 70 | + whereClause.cardId = request.query.cardId; |
| 71 | + } |
| 72 | + |
| 73 | + const [total, views] = await Promise.all([ |
| 74 | + app.prisma.cardView.count({ where: whereClause }), |
| 75 | + app.prisma.cardView.findMany({ |
| 76 | + where: whereClause, |
| 77 | + orderBy: { createdAt: 'desc' }, |
| 78 | + skip, |
| 79 | + take: limit, |
| 80 | + include: { |
| 81 | + viewer: { |
| 82 | + select: { id: true, username: true, displayName: true, avatarUrl: true }, |
| 83 | + }, |
| 84 | + card: { |
| 85 | + select: { id: true, title: true }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }), |
| 89 | + ]); |
| 90 | + |
| 91 | + return { |
| 92 | + data: views, |
| 93 | + meta: { |
| 94 | + total, |
| 95 | + page, |
| 96 | + limit, |
| 97 | + totalPages: Math.ceil(total / limit), |
| 98 | + }, |
| 99 | + }; |
| 100 | + }); |
| 101 | +} |
0 commit comments