All files / web/src/app/api/user-stats route.ts

0% Statements 0/86
0% Branches 0/1
0% Functions 0/1
0% Lines 0/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                             
import { eq } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { db, schema } from '@/db'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/user-stats
 * Get user statistics for the current viewer
 */
export const GET = withAuth(async () => {
  try {
    const userId = await getUserId()

    // Get stats record
    let stats = await db.query.userStats.findFirst({
      where: eq(schema.userStats.userId, userId),
    })

    // If no stats record exists, create one with defaults
    if (!stats) {
      const [newStats] = await db
        .insert(schema.userStats)
        .values({
          userId,
        })
        .returning()

      stats = newStats
    }

    return NextResponse.json({ stats })
  } catch (error) {
    console.error('Failed to fetch user stats:', error)
    return NextResponse.json({ error: 'Failed to fetch user stats' }, { status: 500 })
  }
})

/**
 * PATCH /api/user-stats
 * Update user statistics for the current viewer
 */
export const PATCH = withAuth(async (request) => {
  try {
    const userId = await getUserId()
    const body = await request.json()

    // Get existing stats
    const stats = await db.query.userStats.findFirst({
      where: eq(schema.userStats.userId, userId),
    })

    // Prepare update values
    const updates: any = {}
    if (body.gamesPlayed !== undefined) updates.gamesPlayed = body.gamesPlayed
    if (body.totalWins !== undefined) updates.totalWins = body.totalWins
    if (body.favoriteGameType !== undefined) updates.favoriteGameType = body.favoriteGameType
    if (body.bestTime !== undefined) updates.bestTime = body.bestTime
    if (body.highestAccuracy !== undefined) updates.highestAccuracy = body.highestAccuracy

    if (stats) {
      // Update existing stats
      const [updatedStats] = await db
        .update(schema.userStats)
        .set(updates)
        .where(eq(schema.userStats.userId, userId))
        .returning()

      return NextResponse.json({ stats: updatedStats })
    } else {
      // Create new stats record
      const [newStats] = await db
        .insert(schema.userStats)
        .values({
          userId,
          ...updates,
        })
        .returning()

      return NextResponse.json({ stats: newStats }, { status: 201 })
    }
  } catch (error) {
    console.error('Failed to update user stats:', error)
    return NextResponse.json({ error: 'Failed to update user stats' }, { status: 500 })
  }
})