1import { NextRequest, NextResponse } from 'next/server';
2import { CWAGnapApi } from '@/lib/cwa-gnap-api';
4export async function GET(request: NextRequest) {
5 const { searchParams } = new URL(request.url);
6 const field13 = searchParams.get('field13');
7 const field7 = searchParams.get('field7');
8 const field9 = searchParams.get('field9');
10 const api = new CWAGnapApi();
12 // Try to get API key from cookies or headers
13 const apiKey = request.cookies.get('FieldpineApiKey')?.value ||
14 request.headers.get('Authorization')?.replace('Bearer ', '');
17 api.setApiKey(apiKey);
22 field13: field13 ? parseInt(field13) : undefined,
23 field7: field7 ? parseInt(field7) : undefined,
24 field9: field9 || undefined
27 const result = await api.getSalesTotals(params);
30 return NextResponse.json(
31 { success: false, error: result.error },
32 { status: 200 } // Return 200 to allow client to handle gracefully
36 return NextResponse.json({ success: true, data: result.data });
38 console.error('API Error:', error);
39 return NextResponse.json(
40 { success: false, error: 'Internal server error' },
41 { status: 200 } // Return 200 to allow client to handle gracefully