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 { getStoredAuth } from "@/lib/server/auth";
3import { fieldpineServerApi } from "@/lib/server/fieldpineApi";
4
5/**
6 * GET /api/v1/reports/trading-summary
7 * Get trading summary for date range
8 * Uses retailmax.elink.summary.trading
9 */
10export async function GET(request: NextRequest) {
11 try {
12 const authData = await getStoredAuth();
13 if (!authData?.apiKey) {
14 return NextResponse.json(
15 { success: false, error: "Unauthorized" },
16 { status: 401 }
17 );
18 }
19
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
24
25 // Build the filter - date range
26 const dateFilter = dateTo
27 ? `f101,100,${dateFrom},${dateTo}`
28 : `f101,100,${dateFrom}`;
29
30 const buckParams: Record<string, string> = {
31 "3": "retailmax.elink.summary.trading",
32 "10": "311,320", // Fields to return
33 "9": dateFilter
34 };
35
36 // Add grouping if specified
37 if (groupBy) {
38 buckParams["15"] = `2,${groupBy}`;
39 }
40
41 console.log("[Trading Summary API] Params:", buckParams);
42
43 const result = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
44
45 console.log("[Trading Summary API] Result:", {
46 hasData: !!result,
47 resultKeys: Object.keys(result || {})
48 });
49
50 return NextResponse.json({
51 success: true,
52 data: result,
53 meta: {
54 dateFrom,
55 dateTo,
56 groupBy: groupBy || 'totals'
57 },
58 });
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" },
63 { status: 500 }
64 );
65 }
66}