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 // Build BUCK endpoint for account financial data
15 let endpoint = "/buck?3=retailmax.elink.account.financial&9=f100,4,0&10=1003";
17 const limit = searchParams.get("limit");
18 const search = searchParams.get("search");
19 const showZeroBalance = searchParams.get("showZeroBalance") === "true";
20 const showRetired = searchParams.get("showRetired") === "true";
23 endpoint += `&8=${limit}`;
27 endpoint += `&9=f101,like,${encodeURIComponent(search)}`;
30 if (!showZeroBalance) {
31 endpoint += "&9=f114,5,0";
35 endpoint += "&9=f147,0,1";
38 const result = await fieldpineServerApi.buckApiCall(endpoint, authData.apiKey!);
40 return NextResponse.json({
42 data: result.DATS || [],
45 } catch (error: any) {
46 console.error("Error fetching account financial data:", error);
47 return NextResponse.json(
48 { success: false, error: error.message || "Failed to fetch account financial data" },
49 { status: error.status || 500 }