bookwiz.io / app / api / usage / monthly / route.ts
route.ts
Raw
import { NextRequest, NextResponse } from 'next/server'
import { usageTracker } from '@/lib/services/usage-tracker'

export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'

export async function GET(request: NextRequest) {
  try {
    const userId = request.nextUrl.searchParams.get('userId')
    const monthParam = request.nextUrl.searchParams.get('month')
    
    if (!userId) {
      return NextResponse.json({ error: 'User ID is required' }, { status: 400 })
    }

    // Parse the month parameter or default to current month
    const month = monthParam ? new Date(monthParam) : new Date()
    
    const monthlyUsage = await usageTracker.getMonthlyUsage(userId, month)
    
    if (monthlyUsage === null) {
      return NextResponse.json({ error: 'Failed to get monthly usage' }, { status: 500 })
    }

    const response = NextResponse.json(monthlyUsage)
    
    // Add cache control headers to prevent any caching
    response.headers.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
    response.headers.set('Pragma', 'no-cache')
    response.headers.set('Expires', '0')
    
    return response
  } catch (error) {
    console.error('Error in monthly usage API:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
}