All files / web/src/app/api/number-line/sessions/end route.ts

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

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                                                                                                                                                           
import { NextResponse, type NextRequest } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { db } from '@/db'
import * as schema from '@/db/schema'
import { eq, and } from 'drizzle-orm'
import { players } from '@/db/schema/players'
import { startMomentCull } from '@/lib/tasks/moment-cull'

/** POST /api/number-line/sessions/end — mark a session as ended and trigger cull */
export const POST = withAuth(async (request: NextRequest) => {
  try {
    const userId = await getUserId()
    const { sessionId } = (await request.json()) as { sessionId: string }

    if (!sessionId) {
      return NextResponse.json({ error: 'Missing sessionId' }, { status: 400 })
    }

    // Verify session exists
    const [session] = await db
      .select()
      .from(schema.numberLineSessions)
      .where(eq(schema.numberLineSessions.id, sessionId))
      .limit(1)

    if (!session) {
      return NextResponse.json({ error: 'Session not found' }, { status: 404 })
    }

    // Verify the session's player belongs to this user
    const [player] = await db
      .select({ id: players.id })
      .from(players)
      .where(and(eq(players.id, session.playerId), eq(players.userId, userId)))
      .limit(1)
    if (!player) {
      return NextResponse.json({ error: 'Not authorized for this session' }, { status: 403 })
    }

    // Mark session as ended
    await db
      .update(schema.numberLineSessions)
      .set({ endedAt: new Date() })
      .where(eq(schema.numberLineSessions.id, sessionId))

    // Skip cull if no moments were marked
    if (session.momentCount === 0) {
      await db
        .update(schema.numberLineSessions)
        .set({ isCulled: true })
        .where(eq(schema.numberLineSessions.id, sessionId))
      return NextResponse.json({ ok: true, culled: false })
    }

    // Enqueue cull task
    const taskId = await startMomentCull(
      {
        sessionId,
        playerId: session.playerId,
        callerNumber: session.callerNumber,
      },
      userId
    )

    // Store task reference on session
    await db
      .update(schema.numberLineSessions)
      .set({ cullTaskId: taskId })
      .where(eq(schema.numberLineSessions.id, sessionId))

    return NextResponse.json({ ok: true, cullTaskId: taskId })
  } catch (err) {
    console.error('[number-line/sessions/end] POST failed:', err)
    return NextResponse.json({ error: 'Failed to end session' }, { status: 500 })
  }
})