All files / web/src/app/api/arcade/rooms/current route.ts

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

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                                                                                                                                                   
import { NextResponse } from 'next/server'
import { getUserRooms } from '@/lib/arcade/room-membership'
import { getRoomById } from '@/lib/arcade/room-manager'
import { getRoomMembers } from '@/lib/arcade/room-membership'
import { getRoomActivePlayers } from '@/lib/arcade/player-manager'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { getAllGameConfigs } from '@/lib/arcade/game-config-helpers'

/**
 * GET /api/arcade/rooms/current
 * Returns the user's current room (if any)
 */
export const GET = withAuth(async () => {
  try {
    const userId = await getUserId()

    // Get all rooms user is in (should be at most 1 due to modal room enforcement)
    const roomIds = await getUserRooms(userId)

    if (roomIds.length === 0) {
      return NextResponse.json({ room: null }, { status: 200 })
    }

    const roomId = roomIds[0]

    // Get room data
    const room = await getRoomById(roomId)
    if (!room) {
      return NextResponse.json({ error: 'Room not found' }, { status: 404 })
    }

    // Get game configs from new room_game_configs table
    const gameConfig = await getAllGameConfigs(roomId)

    console.log(
      '[Current Room API] Room data READ from database:',
      JSON.stringify(
        {
          roomId,
          gameName: room.gameName,
          gameConfig,
        },
        null,
        2
      )
    )

    // Get members
    const members = await getRoomMembers(roomId)

    // Get active players for all members
    const memberPlayers = await getRoomActivePlayers(roomId)

    // Convert Map to object for JSON serialization
    const memberPlayersObj: Record<string, any[]> = {}
    for (const [uid, players] of memberPlayers.entries()) {
      memberPlayersObj[uid] = players
    }

    return NextResponse.json({
      room: {
        ...room,
        gameConfig, // Override with configs from new table
      },
      members,
      memberPlayers: memberPlayersObj,
    })
  } catch (error) {
    console.error('[Current Room API] Error:', error)
    return NextResponse.json({ error: 'Failed to fetch current room' }, { status: 500 })
  }
})