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 * Store Performance Statistics
7 * ELINK: retailmax.elink.sale.totals.group (grouped by location)
8 * Returns sales performance by store/location
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
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
31 };
32
33 // Add date filters if provided
34 if (startDate) {
35 buckParams["9"] = `f110,4,${startDate}`;
36 }
37 if (endDate) {
38 // Add as second date filter
39 const key = startDate ? "9.1" : "9";
40 buckParams[key] = `f110,1,${endDate}`;
41 }
42
43 const data = await fieldpineServerApi.buckApiCall(buckParams, context.session.apiKey);
44
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' },
50 { status: 500 }
51 );
52 }
53}