1import { NextRequest, NextResponse } from "next/server";
2import { fieldpineServerApi } from "@/lib/server/fieldpineApi";
3import { getStoredAuth } from "@/lib/server/auth";
5export async function GET(request: NextRequest) {
7 const authData = await getStoredAuth();
9 return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
12 const { searchParams } = new URL(request.url);
14 // Get today's date in YYYYMMDD format
15 const today = new Date();
16 const dateStr = today.getFullYear() +
17 String(today.getMonth() + 1).padStart(2, '0') +
18 String(today.getDate()).padStart(2, '0');
20 // Use sale header search for today's sales
21 const result = await fieldpineServerApi.buckApiCall({
22 "3": "retailmax.elink.sale.header.search",
23 "100": dateStr, // From date
24 "101": dateStr, // To date
25 "f4": "1" // Include summary stats
28 console.log('[Sales Totals API] BUCK Response:', JSON.stringify(result, null, 2));
30 return NextResponse.json({
35 } catch (error: any) {
36 console.error("Error fetching sales totals:", error);
37 return NextResponse.json(
38 { success: false, error: error.message || "Failed to fetch sales totals" },
39 { status: error.status || 500 }