All files / web/src/app/api/households route.ts

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

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                                                                                             
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserHouseholds, createHousehold, MAX_HOUSEHOLD_SIZE } from '@/lib/household'

/**
 * GET /api/households
 *
 * List the current user's households with member counts.
 */
export const GET = withAuth(
  async (_request, { userId }) => {
    const households = await getUserHouseholds(userId)
    return NextResponse.json({ households })
  },
  { role: 'user' }
)

/**
 * POST /api/households
 *
 * Create a new household with the current user as owner.
 * Body: { name: string }
 */
export const POST = withAuth(
  async (request, { userId }) => {
    const body = await request.json()
    const { name } = body

    if (!name || typeof name !== 'string' || name.trim().length === 0) {
      return NextResponse.json({ error: 'Name is required' }, { status: 400 })
    }

    if (name.trim().length > 100) {
      return NextResponse.json({ error: 'Name must be 100 characters or less' }, { status: 400 })
    }

    try {
      const household = await createHousehold(userId, name.trim())
      return NextResponse.json({ household }, { status: 201 })
    } catch (err) {
      const message = err instanceof Error ? err.message : 'Failed to create household'
      return NextResponse.json({ error: message }, { status: 409 })
    }
  },
  { role: 'user' }
)