All files / web/src/app/api/postcards/unread-count route.ts

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

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                                                                     
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, sql } from 'drizzle-orm'

/** GET /api/postcards/unread-count — count unread postcards */
export const GET = withAuth(async (request: NextRequest) => {
  try {
    const userId = await getUserId()
    const url = new URL(request.url)
    const playerId = url.searchParams.get('playerId')

    const conditions = [
      eq(schema.numberLinePostcards.userId, userId),
      eq(schema.numberLinePostcards.status, 'ready'),
      eq(schema.numberLinePostcards.isRead, false),
    ]
    if (playerId) {
      conditions.push(eq(schema.numberLinePostcards.playerId, playerId))
    }

    const [result] = await db
      .select({ count: sql<number>`count(*)` })
      .from(schema.numberLinePostcards)
      .where(and(...conditions))

    return NextResponse.json({ count: result?.count ?? 0 })
  } catch (err) {
    console.error('[postcards/unread-count] GET failed:', err)
    return NextResponse.json({ error: 'Failed to count postcards' }, { status: 500 })
  }
})