|
| 1 | +import { supabase } from '@/lib/supabaseClient'; |
| 2 | +import axios from 'axios'; |
| 3 | +import { NextRequest, NextResponse } from 'next/server'; |
| 4 | + |
| 5 | +// Fetch LeetCode stats |
| 6 | +const fetchLeetCodeStats = async (username: string) => { |
| 7 | + const query = ` |
| 8 | + query getUserProfile($username: String!) { |
| 9 | + matchedUser(username: $username) { |
| 10 | + username |
| 11 | + profile { |
| 12 | + realName |
| 13 | + ranking |
| 14 | + } |
| 15 | + submitStats { |
| 16 | + acSubmissionNum { |
| 17 | + difficulty |
| 18 | + count |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + `; |
| 24 | + try { |
| 25 | + const variables = { username }; |
| 26 | + const { data } = await axios.post('https://leetcode.com/graphql', { |
| 27 | + query, |
| 28 | + variables, |
| 29 | + }); |
| 30 | + return data.data.matchedUser; |
| 31 | + } catch (error) { |
| 32 | + console.error('Error fetching LeetCode data:', error); |
| 33 | + return null; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +// Store transformed user stats in Supabase |
| 38 | +const storeUserStats = async (userId: string, stats: any) => { |
| 39 | + const entry = { |
| 40 | + user_id: String(userId), |
| 41 | + ranking: stats.profile.ranking, |
| 42 | + solved_easy: stats.submitStats.acSubmissionNum.find((item: any) => item.difficulty === 'Easy')?.count || "0", |
| 43 | + solved_medium: stats.submitStats.acSubmissionNum.find((item: any) => item.difficulty === 'Medium')?.count || "0", |
| 44 | + solved_hard: stats.submitStats.acSubmissionNum.find((item: any) => item.difficulty === 'Hard')?.count || "0", |
| 45 | + }; |
| 46 | + const { data, error } = await supabase.from('user_info').upsert([entry]); |
| 47 | + |
| 48 | + if (error) { |
| 49 | + console.error('Error storing data in Supabase:', error); |
| 50 | + } |
| 51 | + |
| 52 | + return data; |
| 53 | +}; |
| 54 | + |
| 55 | +// Transform LeetCode data into a UI-friendly structure |
| 56 | +const transformLeetCodeData = (stats: any) => { |
| 57 | + return { |
| 58 | + username: stats.username, |
| 59 | + profile: { |
| 60 | + realName: stats.profile.realName || "Unknown", |
| 61 | + ranking: stats.profile.ranking?.toString() || "0", |
| 62 | + }, |
| 63 | + submitStats: { |
| 64 | + acSubmissionNum: stats.submitStats.acSubmissionNum.map((item: any) => ({ |
| 65 | + difficulty: item.difficulty, |
| 66 | + count: item.count?.toString() || "0", |
| 67 | + })), |
| 68 | + }, |
| 69 | + }; |
| 70 | +}; |
| 71 | + |
| 72 | +// API POST Handler |
| 73 | +export async function POST(req: NextRequest, res: NextResponse) { |
| 74 | + const searchParams = req.nextUrl.searchParams; |
| 75 | + const username = searchParams.get('username'); |
| 76 | + const userId = searchParams.get('userId'); |
| 77 | + |
| 78 | + if (!username || !userId) { |
| 79 | + return NextResponse.json({ error: "Username and userId are required" }, { status: 400 }); |
| 80 | + } |
| 81 | + |
| 82 | + const stats = await fetchLeetCodeStats(username); |
| 83 | + |
| 84 | + if (!stats) { |
| 85 | + return NextResponse.json({ error: "User not found" }, { status: 404 }); |
| 86 | + } |
| 87 | + |
| 88 | + const transformedStats = transformLeetCodeData(stats); |
| 89 | + |
| 90 | + console.log('Transformed Stats:', JSON.stringify(transformedStats, null, 2)); |
| 91 | + |
| 92 | + await storeUserStats(userId, transformedStats); |
| 93 | + |
| 94 | + return NextResponse.json({ message: "Success", stats:transformedStats }); |
| 95 | +} |
0 commit comments