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 Locations 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 limit = searchParams.get('limit');
38 if (limit) params.limit = parseInt(limit);
39
40 // Call Fieldpine BUCK API via eLink protocol
41 try {
42 const locations = await fieldpineServerApi.apiCall("/Location", {
43 params,
44 cookie: authData.apiKey,
45 useOpenApi: false // Force BUCK/eLink usage
46 });
47
48 return NextResponse.json({
49 success: true,
50 data: locations,
51 source: 'elink'
52 });
53
54 } catch (error) {
55 console.error('eLink locations error:', error);
56 return NextResponse.json(
57 { error: 'eLink endpoint unavailable', source: 'elink' },
58 { status: 503 }
59 );
60 }
61
62 } catch (error) {
63 console.error('eLink locations error:', error);
64 return NextResponse.json(
65 { error: 'Failed to fetch locations' },
66 { status: 500 }
67 );
68 }
69}