All files / web/src/app/api/family/link route.ts

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

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                                                                         
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { linkParentToChild } from '@/lib/classroom'
import { getUserId } from '@/lib/viewer'

/**
 * POST /api/family/link
 * Link current user as parent to a child via family code
 *
 * Body: { familyCode: string }
 * Returns: { success: true, player } or { success: false, error }
 */
export const POST = withAuth(async (request) => {
  try {
    const userId = await getUserId()
    const body = await request.json()

    if (!body.familyCode) {
      return NextResponse.json({ success: false, error: 'Missing familyCode' }, { status: 400 })
    }

    const result = await linkParentToChild(userId, body.familyCode)

    if (!result.success) {
      return NextResponse.json({ success: false, error: result.error }, { status: 400 })
    }

    return NextResponse.json({ success: true, player: result.player })
  } catch (error) {
    console.error('Failed to link parent to child:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to link parent to child' },
      { status: 500 }
    )
  }
})