/** * Billing Items Data API Endpoint * GET /api/data/billing-items - Query billing items from PostgreSQL */ import { NextRequest, NextResponse } from 'next/server'; import postgresClient from '@/lib/services/postgres-client'; export async function GET(request: NextRequest) { try { const searchParams = request.nextUrl.searchParams; const limit = parseInt(searchParams.get('limit') || '100'); const offset = parseInt(searchParams.get('offset') || '0'); const companyId = searchParams.get('companyId'); const projectId = searchParams.get('projectId'); const ticketId = searchParams.get('ticketId'); const taskId = searchParams.get('taskId'); // Build where clause const where: Record = {}; if (companyId) { where.company_id = parseInt(companyId); } if (projectId) { where.project_id = parseInt(projectId); } if (ticketId) { where.ticket_id = parseInt(ticketId); } if (taskId) { where.task_id = parseInt(taskId); } // Query billing items const billingItems = await postgresClient.find( 'billing_items', where, { limit, offset, orderBy: 'created_at DESC', } ); // Get total count const totalCount = await postgresClient.count('billing_items', where); return NextResponse.json({ billingItems, pagination: { limit, offset, total: totalCount, hasMore: offset + billingItems.length < totalCount, }, }); } catch (error) { console.error('Failed to fetch billing items:', error); return NextResponse.json( { error: 'Failed to fetch billing items' }, { status: 500 } ); } }