EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1import { NextRequest, NextResponse } from 'next/server';
2import { CWAGnapApi } from '@/lib/cwa-gnap-api';
3
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');
9
10 const api = new CWAGnapApi();
11
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 ', '');
15
16 if (apiKey) {
17 api.setApiKey(apiKey);
18 }
19
20 try {
21 const params = {
22 field13: field13 ? parseInt(field13) : undefined,
23 field7: field7 ? parseInt(field7) : undefined,
24 field9: field9 || undefined
25 };
26
27 const result = await api.getSalesTotals(params);
28
29 if (result.error) {
30 return NextResponse.json(
31 { success: false, error: result.error },
32 { status: 200 } // Return 200 to allow client to handle gracefully
33 );
34 }
35
36 return NextResponse.json({ success: true, data: result.data });
37 } catch (error) {
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
42 );
43 }
44}