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 { getRequestContext, getStoreApiUrl } from '@/lib/server/sessionUtils';
3import { FieldpineServerApi } from '@/lib/server/fieldpineApi';
4
5/**
6 * Statistics API Endpoint
7 * Fetches product performance and analytics data using GNAP/BUCK
8 * Routes to IIG management portal for reporting
9 */
10export async function GET(request: NextRequest) {
11 try {
12 // Get session and store context
13 const context = await getRequestContext(request);
14
15 if (!context) {
16 return NextResponse.json(
17 { error: 'Authentication required' },
18 { status: 401 }
19 );
20 }
21
22 // Parse query parameters
23 const { searchParams } = new URL(request.url);
24
25 const customerId = searchParams.get('customerId');
26 const type = searchParams.get('type'); // 'product-performance' or 'brands'
27 const limit = searchParams.get('limit') || '30';
28
29 if (!customerId) {
30 return NextResponse.json(
31 { error: 'customerId required' },
32 { status: 400 }
33 );
34 }
35
36 try {
37 // GNAP requests always route to IIG for reporting
38 const gnapUrl = getStoreApiUrl(context, 'gnap');
39 const api = new FieldpineServerApi(gnapUrl);
40
41 let response;
42
43 if (type === 'brands') {
44 // Fetch brand data
45 let buckParams: Record<string, string> = {
46 "3": "retailmax.elink.customers",
47 "10": "1120",
48 "9": `f100,0,${customerId}`,
49 "99": Math.random().toString()
50 };
51 response = await api.buckApiCall(buckParams, context.session.apiKey);
52 } else {
53 // Fetch product performance stats
54 let buckParams: Record<string, string> = {
55 "3": "retailmax.elink.stats.product.performance",
56 "9": `f107,0,${customerId}`,
57 "15": "2,pid",
58 "8": limit,
59 "100": "1",
60 "99": Math.random().toString()
61 };
62 response = await api.buckApiCall(buckParams, context.session.apiKey);
63 }
64
65 return NextResponse.json({
66 success: true,
67 data: response,
68 meta: {
69 store: context.store.name,
70 requestType: 'gnap',
71 apiUrl: gnapUrl
72 }
73 });
74
75 } catch (error) {
76 console.error('[Stats API] Error:', error);
77 return NextResponse.json(
78 { error: 'Failed to fetch statistics' },
79 { status: 503 }
80 );
81 }
82
83 } catch (error) {
84 console.error('[Stats API] Error:', error);
85 return NextResponse.json(
86 { error: 'Failed to fetch statistics' },
87 { status: 500 }
88 );
89 }
90}