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 { getRequestContext } from '@/lib/server/sessionUtils';
4
5/**
6 * Hourly Sales Statistics
7 * ELINK: retailmax.elink.sale.list (individual transactions)
8 * Returns sales grouped by hour of day
9 */
10export async function GET(request: NextRequest) {
11 try {
12 const context = await getRequestContext(request);
13 if (!context?.session) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
16 { status: 401 }
17 );
18 }
19
20 const { searchParams } = new URL(request.url);
21 const startDate = searchParams.get('startDate');
22 const endDate = searchParams.get('endDate');
23 const location = searchParams.get('location');
24
25 // Build BUCK request parameters for individual sale list
26 const buckParams: Record<string, string> = {
27 "3": "retailmax.elink.sale.list",
28 "8": "5000" // Limit to 5000 transactions (reasonable for one day)
29 };
30
31 // Add date filters if provided
32 if (startDate) {
33 buckParams["9"] = `f110,4,${startDate}`;
34 }
35 if (endDate) {
36 // Add as second date filter
37 const key = startDate ? "9.1" : "9";
38 buckParams[key] = `f110,1,${endDate}`;
39 }
40
41 // Add location filter if provided
42 if (location && location !== '0') {
43 buckParams["7"] = location;
44 }
45
46 const data = await fieldpineServerApi.buckApiCall(buckParams, context.session.apiKey);
47
48 return NextResponse.json(data);
49 } catch (error: any) {
50 console.error('Error fetching hourly sales:', error);
51 return NextResponse.json(
52 { error: error.message || 'Failed to fetch hourly sales' },
53 { status: 500 }
54 );
55 }
56}