File size: 1,436 Bytes
9831def
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * User Activity Route
 * 
 * GET /api/user/activity
 * Returns user activity data for contribution heatmap and streak info
 */

import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { getUserStreak, getUserCalendar } from "@/lib/db/queries/gamification";

export async function GET(request: NextRequest) {
    try {
        const user = await getCurrentUser(request);
        if (!user) {
            return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
        }

        const { searchParams } = new URL(request.url);
        const days = parseInt(searchParams.get("days") || "365");

        // Get streak and calendar data
        const [streak, calendar] = await Promise.all([
            getUserStreak(user.username),
            getUserCalendar(user.username, days)
        ]);

        return NextResponse.json({
            activity: calendar,
            streak: {
                current_streak: streak.current_streak,
                longest_streak: streak.longest_streak,
                is_active: streak.is_active,
                total_contribution_days: streak.total_contribution_days
            },
            lastUpdated: new Date().toISOString()
        });
    } catch (error) {
        console.error("GET /api/user/activity error:", error);
        return NextResponse.json({ error: "Internal server error" }, { status: 500 });
    }
}