1import { NextRequest, NextResponse } from 'next/server';
2import { getRequestContext, getStoreApiUrl } from '@/lib/server/sessionUtils';
3import { FieldpineServerApi } from '@/lib/server/fieldpineApi';
6 * Statistics API Endpoint
7 * Fetches product performance and analytics data using GNAP/BUCK
8 * Routes to IIG management portal for reporting
10export async function GET(request: NextRequest) {
12 // Get session and store context
13 const context = await getRequestContext(request);
16 return NextResponse.json(
17 { error: 'Authentication required' },
22 // Parse query parameters
23 const { searchParams } = new URL(request.url);
25 const customerId = searchParams.get('customerId');
26 const type = searchParams.get('type'); // 'product-performance' or 'brands'
27 const limit = searchParams.get('limit') || '30';
30 return NextResponse.json(
31 { error: 'customerId required' },
37 // GNAP requests always route to IIG for reporting
38 const gnapUrl = getStoreApiUrl(context, 'gnap');
39 const api = new FieldpineServerApi(gnapUrl);
43 if (type === 'brands') {
45 let buckParams: Record<string, string> = {
46 "3": "retailmax.elink.customers",
48 "9": `f100,0,${customerId}`,
49 "99": Math.random().toString()
51 response = await api.buckApiCall(buckParams, context.session.apiKey);
53 // Fetch product performance stats
54 let buckParams: Record<string, string> = {
55 "3": "retailmax.elink.stats.product.performance",
56 "9": `f107,0,${customerId}`,
60 "99": Math.random().toString()
62 response = await api.buckApiCall(buckParams, context.session.apiKey);
65 return NextResponse.json({
69 store: context.store.name,
76 console.error('[Stats API] Error:', error);
77 return NextResponse.json(
78 { error: 'Failed to fetch statistics' },
84 console.error('[Stats API] Error:', error);
85 return NextResponse.json(
86 { error: 'Failed to fetch statistics' },