1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
7 * Fetches sales data filtered by customer, account, date range, etc.
9export async function GET(request: NextRequest) {
11 // Verify authentication
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 // Parse query parameters
21 const { searchParams } = new URL(request.url);
23 const customerId = searchParams.get('customerId');
24 const accountId = searchParams.get('accountId');
25 const limit = searchParams.get('limit');
26 const from = searchParams.get('from');
27 const to = searchParams.get('to');
29 // Build BUCK API parameters
30 let buckParams: Record<string, string> = {
31 "3": "retailmax.elink.saleflat.list",
33 "99": Math.random().toString()
36 // Add customer filter if provided
38 buckParams["9"] = `f108,0,${customerId}`;
41 // Add account filter if provided
43 buckParams["9"] = `f117,0,${accountId}`;
47 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
49 const sales = response?.DATS || [];
51 return NextResponse.json({
57 console.error('Sales API error:', error);
58 return NextResponse.json(
59 { error: 'Failed to fetch sales' },
65 console.error('Sales API error:', error);
66 return NextResponse.json(
67 { error: 'Failed to fetch sales' },