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

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

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                                                                                                                                                                                     
import { eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { db } from '@/db'
import type { GameStatsBreakdown } from '@/db/schema/player-stats'
import { playerStats } from '@/db/schema/player-stats'
import { players } from '@/db/schema/players'
import type { GetAllPlayerStatsResponse, PlayerStatsData } from '@/lib/arcade/stats/types'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'

// Force dynamic rendering - this route uses headers()
export const dynamic = 'force-dynamic'

/**
 * GET /api/player-stats
 *
 * Fetches stats for all of the current user's players.
 */
export const GET = withAuth(async () => {
  try {
    // 1. Authenticate user
    const userId = await getUserId()
    if (!userId) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    // 2. Fetch all user's players
    const userPlayers = await db.select().from(players).where(eq(players.userId, userId))

    const playerIds = userPlayers.map((p) => p.id)

    // 3. Fetch stats for all players
    const allStats: PlayerStatsData[] = []

    for (const playerId of playerIds) {
      const stats = await db
        .select()
        .from(playerStats)
        .where(eq(playerStats.playerId, playerId))
        .limit(1)
        .then((rows) => rows[0])

      if (stats) {
        allStats.push(convertToPlayerStatsData(stats))
      } else {
        // Player exists but has no stats yet
        allStats.push(createDefaultPlayerStats(playerId))
      }
    }

    // 4. Return response
    const response: GetAllPlayerStatsResponse = {
      playerStats: allStats,
    }

    return NextResponse.json(response)
  } catch (error) {
    console.error('❌ Failed to fetch player stats:', error)
    return NextResponse.json(
      {
        error: 'Failed to fetch player stats',
        details: error instanceof Error ? error.message : String(error),
      },
      { status: 500 }
    )
  }
})

/**
 * Convert DB record to PlayerStatsData
 */
function convertToPlayerStatsData(dbStats: typeof playerStats.$inferSelect): PlayerStatsData {
  return {
    playerId: dbStats.playerId,
    gamesPlayed: dbStats.gamesPlayed,
    totalWins: dbStats.totalWins,
    totalLosses: dbStats.totalLosses,
    bestTime: dbStats.bestTime,
    highestAccuracy: dbStats.highestAccuracy,
    favoriteGameType: dbStats.favoriteGameType,
    gameStats: (dbStats.gameStats as Record<string, GameStatsBreakdown>) || {},
    lastPlayedAt: dbStats.lastPlayedAt,
    createdAt: dbStats.createdAt,
    updatedAt: dbStats.updatedAt,
  }
}

/**
 * Create default player stats for new player
 */
function createDefaultPlayerStats(playerId: string): PlayerStatsData {
  const now = new Date()
  return {
    playerId,
    gamesPlayed: 0,
    totalWins: 0,
    totalLosses: 0,
    bestTime: null,
    highestAccuracy: 0,
    favoriteGameType: null,
    gameStats: {},
    lastPlayedAt: null,
    createdAt: now,
    updatedAt: now,
  }
}