Download src/app/api/dashboard/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/dashboard/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/dashboard/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/dashboard/route.ts
3.24 kB
| import { NextResponse } from "next/server"; | |
| import { prisma } from "@/lib/db"; | |
| import { xpForLevel } from "@/config/xp"; | |
| import { withApiError } from "@/lib/api-error"; | |
| export const GET = withApiError(async () => { | |
| const user = await prisma.user.findFirst({ | |
| where: { id: 1 }, | |
| include: { categorySkills: true }, | |
| }); | |
| if (!user) { | |
| return NextResponse.json({ error: "No user found", status: 404 }, { status: 404 }); | |
| } | |
| // Today's UTC midnight boundaries | |
| const now = new Date(); | |
| const todayStart = new Date( | |
| Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()) | |
| ); | |
| const tomorrowStart = new Date(todayStart.getTime() + 86_400_000); | |
| const [todayLog, recentActivities, weeklyLogs, lastPlayedRows] = await Promise.all([ | |
| prisma.dailyLog.findFirst({ | |
| where: { | |
| userId: 1, | |
| date: { gte: todayStart, lt: tomorrowStart }, | |
| }, | |
| }), | |
| prisma.activity.findMany({ | |
| where: { userId: 1 }, | |
| orderBy: { createdAt: "desc" }, | |
| take: 10, | |
| }), | |
| prisma.dailyLog.findMany({ | |
| where: { userId: 1 }, | |
| orderBy: { date: "desc" }, | |
| take: 7, | |
| }), | |
| // One query: latest activity per category for user 1 | |
| prisma.activity.groupBy({ | |
| by: ["category"], | |
| where: { userId: 1 }, | |
| _max: { createdAt: true }, | |
| }), | |
| ]); | |
| // Build a lookup map: category (lowercase) → last createdAt | |
| const lastPlayedMap = new Map<string, Date | null>( | |
| (lastPlayedRows as Array<{ category: string; _max: { createdAt: Date | null } }>).map( | |
| (row) => [row.category.toLowerCase(), row._max.createdAt] | |
| ) | |
| ); | |
| return NextResponse.json({ | |
| user: { | |
| id: user.id, | |
| name: user.name, | |
| level: user.level, | |
| totalXp: user.totalXp, | |
| currentXp: user.currentXp, | |
| xpToNextLevel: xpForLevel(user.level + 1), | |
| title: user.title, | |
| dailyGoalXp: user.dailyGoalXp, | |
| streak: user.streak, | |
| longestStreak: user.longestStreak, | |
| streakFreezes: user.streakFreezes, | |
| }, | |
| todayLog: todayLog | |
| ? { | |
| xpEarned: todayLog.xpEarned, | |
| activityCount: todayLog.activityCount, | |
| goalMet: todayLog.goalMet, | |
| } | |
| : null, | |
| categorySkills: user.categorySkills.map((cs: { category: string; level: number; totalXp: number; currentXp: number; xpToNext: number }) => ({ | |
| category: cs.category, | |
| level: cs.level, | |
| totalXp: cs.totalXp, | |
| currentXp: cs.currentXp, | |
| xpToNext: cs.xpToNext, | |
| lastPlayedAt: (lastPlayedMap.get(cs.category.toLowerCase()) ?? null)?.toISOString() ?? null, | |
| })), | |
| recentActivities: recentActivities.map((a: { id: number; category: string; exerciseType: string; description: string; totalXp: number; isCriticalHit: boolean; createdAt: Date }) => ({ | |
| id: a.id, | |
| category: a.category, | |
| exerciseType: a.exerciseType, | |
| description: a.description, | |
| totalXp: a.totalXp, | |
| isCriticalHit: a.isCriticalHit, | |
| createdAt: a.createdAt, | |
| })), | |
| weeklyLogs: weeklyLogs.map((l: { date: Date; xpEarned: number; activityCount: number; goalMet: boolean }) => ({ | |
| date: l.date, | |
| xpEarned: l.xpEarned, | |
| activityCount: l.activityCount, | |
| goalMet: l.goalMet, | |
| })), | |
| }); | |
| }); | |