1import { NextRequest, NextResponse } from "next/server";
2import { getStoredAuth } from "@/lib/server/auth";
3import { fieldpineServerApi } from "@/lib/server/fieldpineApi";
6 * GET /api/v1/reports/trading-summary
7 * Get trading summary for date range
8 * Uses retailmax.elink.summary.trading
10export async function GET(request: NextRequest) {
12 const authData = await getStoredAuth();
13 if (!authData?.apiKey) {
14 return NextResponse.json(
15 { success: false, error: "Unauthorized" },
20 const searchParams = request.nextUrl.searchParams;
21 const dateFrom = searchParams.get("dateFrom") || new Date().toISOString().split('T')[0];
22 const dateTo = searchParams.get("dateTo");
23 const groupBy = searchParams.get("groupBy"); // 'location', 'srcuid' (lane/till), or null for totals
25 // Build the filter - date range
26 const dateFilter = dateTo
27 ? `f101,100,${dateFrom},${dateTo}`
28 : `f101,100,${dateFrom}`;
30 const buckParams: Record<string, string> = {
31 "3": "retailmax.elink.summary.trading",
32 "10": "311,320", // Fields to return
36 // Add grouping if specified
38 buckParams["15"] = `2,${groupBy}`;
41 console.log("[Trading Summary API] Params:", buckParams);
43 const result = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
45 console.log("[Trading Summary API] Result:", {
47 resultKeys: Object.keys(result || {})
50 return NextResponse.json({
56 groupBy: groupBy || 'totals'
59 } catch (error: any) {
60 console.error("[Trading Summary API] Error:", error);
61 return NextResponse.json(
62 { success: false, error: error.message || "Failed to fetch trading summary" },