File size: 3,238 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 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,
})),
});
});
|