1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * eLink/BUCK Customers Endpoint
7 * Uses Fieldpine's BUCK API via eLink protocol
8 * Documentation: https://docs.fieldpine.com/pos/elink.htm
10export async function GET(request: NextRequest) {
12 // Verify authentication
13 const authData = await getStoredAuth();
14 if (!authData || !authData.authenticated) {
15 return NextResponse.json(
16 { error: 'Authentication required' },
22 const clientId = request.headers.get('x-forwarded-for') ||
23 request.headers.get('x-real-ip') ||
26 if (!fieldpineServerApi.checkClientRateLimit(clientId)) {
27 return NextResponse.json(
28 { error: 'Rate limit exceeded' },
33 // Parse query parameters
34 const { searchParams } = new URL(request.url);
35 const params: Record<string, string | number> = {};
37 const type = searchParams.get('type'); // 'list', 'single', 'search'
38 const id = searchParams.get('id');
40 const search = searchParams.get('search');
41 if (search) params.search = search;
43 const customerId = searchParams.get('customerId') || id;
44 if (customerId) params.customerId = customerId;
46 const phone = searchParams.get('phone');
47 if (phone) params.phone = phone;
49 const email = searchParams.get('email');
50 if (email) params.email = email;
52 const limit = searchParams.get('limit');
53 if (limit) params.limit = parseInt(limit);
55 // Call Fieldpine BUCK API via eLink protocol
59 // If requesting a single customer by ID
60 if (type === 'single' && customerId) {
61 customers = await fieldpineServerApi.getCustomerById(customerId, authData.apiKey);
63 customers = await fieldpineServerApi.getCustomers(params, authData.apiKey);
66 return NextResponse.json({
73 console.error('eLink customers error:', error);
74 return NextResponse.json(
75 { error: 'eLink endpoint unavailable', source: 'elink' },
81 console.error('eLink customers error:', error);
82 return NextResponse.json(
83 { error: 'Failed to fetch customers' },