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 { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
4
5/**
6 * eLink/BUCK Customers Endpoint
7 * Uses Fieldpine's BUCK API via eLink protocol
8 * Documentation: https://docs.fieldpine.com/pos/elink.htm
9 */
10export async function GET(request: NextRequest) {
11 try {
12 // Verify authentication
13 const authData = await getStoredAuth();
14 if (!authData || !authData.authenticated) {
15 return NextResponse.json(
16 { error: 'Authentication required' },
17 { status: 401 }
18 );
19 }
20
21 // Rate limiting
22 const clientId = request.headers.get('x-forwarded-for') ||
23 request.headers.get('x-real-ip') ||
24 authData.userId ||
25 'unknown';
26 if (!fieldpineServerApi.checkClientRateLimit(clientId)) {
27 return NextResponse.json(
28 { error: 'Rate limit exceeded' },
29 { status: 429 }
30 );
31 }
32
33 // Parse query parameters
34 const { searchParams } = new URL(request.url);
35 const params: Record<string, string | number> = {};
36
37 const type = searchParams.get('type'); // 'list', 'single', 'search'
38 const id = searchParams.get('id');
39
40 const search = searchParams.get('search');
41 if (search) params.search = search;
42
43 const customerId = searchParams.get('customerId') || id;
44 if (customerId) params.customerId = customerId;
45
46 const phone = searchParams.get('phone');
47 if (phone) params.phone = phone;
48
49 const email = searchParams.get('email');
50 if (email) params.email = email;
51
52 const limit = searchParams.get('limit');
53 if (limit) params.limit = parseInt(limit);
54
55 // Call Fieldpine BUCK API via eLink protocol
56 try {
57 let customers;
58
59 // If requesting a single customer by ID
60 if (type === 'single' && customerId) {
61 customers = await fieldpineServerApi.getCustomerById(customerId, authData.apiKey);
62 } else {
63 customers = await fieldpineServerApi.getCustomers(params, authData.apiKey);
64 }
65
66 return NextResponse.json({
67 success: true,
68 data: customers,
69 source: 'elink'
70 });
71
72 } catch (error) {
73 console.error('eLink customers error:', error);
74 return NextResponse.json(
75 { error: 'eLink endpoint unavailable', source: 'elink' },
76 { status: 503 }
77 );
78 }
79
80 } catch (error) {
81 console.error('eLink customers error:', error);
82 return NextResponse.json(
83 { error: 'Failed to fetch customers' },
84 { status: 500 }
85 );
86 }
87}