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 { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
4
5/**
6 * Sales API Endpoint
7 * Fetches sales data filtered by customer, account, date range, etc.
8 */
9export async function GET(request: NextRequest) {
10 try {
11 // Verify authentication
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
16 { status: 401 }
17 );
18 }
19
20 // Parse query parameters
21 const { searchParams } = new URL(request.url);
22
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');
28
29 // Build BUCK API parameters
30 let buckParams: Record<string, string> = {
31 "3": "retailmax.elink.saleflat.list",
32 "8": limit || "20",
33 "99": Math.random().toString()
34 };
35
36 // Add customer filter if provided
37 if (customerId) {
38 buckParams["9"] = `f108,0,${customerId}`;
39 }
40
41 // Add account filter if provided
42 if (accountId) {
43 buckParams["9"] = `f117,0,${accountId}`;
44 }
45
46 try {
47 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
48
49 const sales = response?.DATS || [];
50
51 return NextResponse.json({
52 success: true,
53 data: sales
54 });
55
56 } catch (error) {
57 console.error('Sales API error:', error);
58 return NextResponse.json(
59 { error: 'Failed to fetch sales' },
60 { status: 503 }
61 );
62 }
63
64 } catch (error) {
65 console.error('Sales API error:', error);
66 return NextResponse.json(
67 { error: 'Failed to fetch sales' },
68 { status: 500 }
69 );
70 }
71}