1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getRequestContext } from '@/lib/server/sessionUtils';
6 * Teller/Staff Performance Statistics
7 * ELINK: retailmax.elink.sale.totals.group (grouped by teller)
8 * Returns sales performance by staff member
10export async function GET(request: NextRequest) {
12 const context = await getRequestContext(request);
13 if (!context?.session) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 const { searchParams } = new URL(request.url);
21 const startDate = searchParams.get('startDate');
22 const endDate = searchParams.get('endDate');
23 const locationId = searchParams.get('locationId');
25 // Build BUCK parameters for teller-grouped sales totals
26 const buckParams: Record<string, string | string[]> = {
27 "3": "retailmax.elink.sale.totals.group",
28 "10": "11000", // Request field 11000 series (quartile stats)
29 "15": "2,1,tid", // Group by teller ID
30 "13": "120", // Sort by sales amount (f120)
35 const filters: string[] = [];
37 filters.push(`f110,4,${startDate}`); // f110 >= startDate
40 filters.push(`f110,1,${endDate}`); // f110 < endDate
43 filters.push(`f7,0,${locationId}`); // Filter by location
46 if (filters.length > 0) {
47 buckParams["9"] = filters;
50 const result = await fieldpineServerApi.buckApiCall(buckParams, context.session.apiKey);
52 // Extract APPD array from BUCK response
53 const tellerData = result?.APPD || [];
55 console.log(`[Teller Performance] Loaded performance data for ${tellerData.length} tellers`);
57 return NextResponse.json({
64 console.error('[Teller Performance] Error:', error);
65 return NextResponse.json(
66 { error: 'Failed to fetch teller performance', details: error instanceof Error ? error.message : 'Unknown error' },