File size: 1,855 Bytes
9853b20
b00f0f1
dac0489
9853b20
 
 
b00f0f1
9853b20
 
 
 
 
b00f0f1
123c60b
b00f0f1
 
 
 
123c60b
 
b00f0f1
 
9853b20
 
b00f0f1
9853b20
b00f0f1
9853b20
 
 
b00f0f1
9853b20
dac0489
9853b20
 
b00f0f1
9853b20
 
 
 
b00f0f1
9853b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b00f0f1
9853b20
 
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
import { NextRequest, NextResponse } from 'next/server'
import { isSupabaseConfigured } from 'lib/supabase/server'
import { markCodeAsUsed } from 'lib/roomService'
import { generateSessionHash, generateCode } from 'lib/codes'
import { getCodeById, insertCode, updateCode } from 'lib/persistence'
import { logEvent } from 'lib/logger'
import { getAppSession } from 'lib/appSession'

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { codeId, userAgent } = body

    if (!isSupabaseConfigured()) {
      return NextResponse.json(
        { error: 'Supabase not configured (missing NEXT_PUBLIC_SUPABASE_URL / NEXT_PUBLIC_SUPABASE_ANON_KEY)' },
        { status: 503 }
      )
    }

    const session = await getAppSession()
    const detectedUserId = session?.userId

    const codeData = await getCodeById(codeId)

    if (!codeData) {
      return NextResponse.json({ error: 'Kód nenalezen' }, { status: 404 })
    }

    let roomId = codeData.room_id

    if (!roomId) {
      return NextResponse.json({ error: 'Místnost neexistuje' }, { status: 404 })

    }

    if (codeData.used !== 1) {
      const sessionHash = detectedUserId ? undefined : generateSessionHash(codeData.code, userAgent || '')
      await markCodeAsUsed(codeData.id, detectedUserId, sessionHash)
    }

    await logEvent({
      module: 'codes',
      operation: 'enter',
      data: { codeId: codeData.id, roomId },
      ip: (request as any).headers?.get?.('x-forwarded-for') || null,
      userAgent: (request as any).headers?.get?.('user-agent') || null
    })

    return NextResponse.json({
      roomId,
      codeId: codeData.id,
      success: true
    })
  } catch (error) {
    console.error('Code entry error:', error)
    return NextResponse.json({ error: 'Vnitřní chyba serveru' }, { status: 500 })
  }
}