1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getRequestContext } from '@/lib/server/sessionUtils';
6 * Store Performance Statistics
7 * ELINK: retailmax.elink.sale.totals.group (grouped by location)
8 * Returns sales performance by store/location
10export async function GET(request: NextRequest) {
12 const context = await getRequestContext(request);
13 if (!context?.session) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 const { searchParams } = new URL(request.url);
21 const startDate = searchParams.get('startDate');
22 const endDate = searchParams.get('endDate');
24 // Build BUCK request parameters for grouped location sales
25 const buckParams: Record<string, string> = {
26 "3": "retailmax.elink.sale.totals.group",
27 "10": "11000", // Quartile stats
28 "15": "2,1,lid", // Group by location ID
29 "13": "120", // Sort by sales amount
30 "8": "200" // Limit to 200 results
33 // Add date filters if provided
35 buckParams["9"] = `f110,4,${startDate}`;
38 // Add as second date filter
39 const key = startDate ? "9.1" : "9";
40 buckParams[key] = `f110,1,${endDate}`;
43 const data = await fieldpineServerApi.buckApiCall(buckParams, context.session.apiKey);
45 return NextResponse.json(data);
46 } catch (error: any) {
47 console.error('Error fetching store performance:', error);
48 return NextResponse.json(
49 { error: error.message || 'Failed to fetch store performance' },