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

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

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
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, desc } from 'drizzle-orm'
import type { PostcardManifest } from '@/db/schema/number-line-postcards'
import { startPostcardGenerate } from '@/lib/tasks/postcard-generate'

/** POST /api/postcards — create a new postcard from a voice call */
export const POST = withAuth(async (request: NextRequest) => {
  try {
    const userId = await getUserId()
    const body = await request.json()
    const { playerId, sessionId, manifest } = body as {
      playerId?: string
      sessionId?: string
      manifest: PostcardManifest
    }

    if (!manifest || !manifest.moments || manifest.moments.length === 0) {
      return NextResponse.json({ error: 'Invalid manifest' }, { status: 400 })
    }

    const [postcard] = await db
      .insert(schema.numberLinePostcards)
      .values({
        userId,
        playerId: playerId ?? null,
        callerNumber: manifest.callerNumber,
        sessionId: sessionId ?? null,
        manifest,
        status: 'pending',
      })
      .returning()

    // Start background generation task
    const taskId = await startPostcardGenerate({
      postcardId: postcard.id,
      userId,
    })

    // Store taskId on the postcard record
    await db
      .update(schema.numberLinePostcards)
      .set({ taskId })
      .where(eq(schema.numberLinePostcards.id, postcard.id))

    return NextResponse.json({ postcardId: postcard.id, taskId }, { status: 201 })
  } catch (err) {
    console.error('[postcards] POST failed:', err)
    return NextResponse.json({ error: 'Failed to create postcard' }, { status: 500 })
  }
})

/** GET /api/postcards — list postcards for the current user */
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 ids = url.searchParams.get('ids')
    const conditions = [eq(schema.numberLinePostcards.userId, userId)]
    if (ids) {
      // Filter to specific postcards by ID (comma-separated)
      const idList = ids.split(',').filter(Boolean)
      if (idList.length === 1) {
        conditions.push(eq(schema.numberLinePostcards.id, idList[0]))
      } else if (idList.length > 1) {
        const { inArray } = await import('drizzle-orm')
        conditions.push(inArray(schema.numberLinePostcards.id, idList))
      }
    } else if (playerId) {
      conditions.push(eq(schema.numberLinePostcards.playerId, playerId))
    }

    const postcards = await db
      .select({
        id: schema.numberLinePostcards.id,
        callerNumber: schema.numberLinePostcards.callerNumber,
        status: schema.numberLinePostcards.status,
        imageUrl: schema.numberLinePostcards.imageUrl,
        thumbnailUrl: schema.numberLinePostcards.thumbnailUrl,
        isRead: schema.numberLinePostcards.isRead,
        createdAt: schema.numberLinePostcards.createdAt,
        manifest: schema.numberLinePostcards.manifest,
      })
      .from(schema.numberLinePostcards)
      .where(and(...conditions))
      .orderBy(desc(schema.numberLinePostcards.createdAt))
      .limit(50)

    return NextResponse.json({ postcards })
  } catch (err) {
    console.error('[postcards] GET failed:', err)
    return NextResponse.json({ error: 'Failed to fetch postcards' }, { status: 500 })
  }
})