← back to Dear Bubbe Nextjs
app/api/messages/route.ts
42 lines
import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { loadUserMemory } from '@/lib/user-memory'
export async function GET(request: NextRequest) {
try {
// Get user ID from query params
const searchParams = request.nextUrl.searchParams
const userId = searchParams.get('userId')
if (!userId) {
return NextResponse.json(
{ error: 'User ID is required' },
{ status: 400 }
)
}
// Load user memory
const memory = loadUserMemory(userId)
if (!memory) {
// Return empty memory structure if no data exists
return NextResponse.json({
userId,
lastVisit: new Date().toISOString(),
totalMessages: 0,
topics: [],
conversations: []
})
}
// Return the user's conversation history
return NextResponse.json(memory)
} catch (error) {
console.error('[MESSAGES API] Error:', error)
return NextResponse.json(
{ error: 'Failed to load messages' },
{ status: 500 }
)
}
}