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 { getRequestContext } from '@/lib/server/sessionUtils';
4
5/**
6 * eLink/BUCK Products 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 // Get request context with authentication
13 const context = await getRequestContext(request);
14 if (!context?.session) {
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 context.session.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 search = searchParams.get('search');
38 if (search) params.search = search;
39
40 const plu = searchParams.get('plu');
41 if (plu) params.plu = plu;
42
43 const barcode = searchParams.get('barcode');
44 if (barcode) params.barcode = barcode;
45
46 const limit = searchParams.get('limit');
47 if (limit) params.limit = parseInt(limit);
48
49 // Call Fieldpine BUCK API via eLink protocol
50 try {
51 const products = await fieldpineServerApi.getProducts(params, context.session.apiKey);
52
53 return NextResponse.json({
54 success: true,
55 data: products,
56 source: 'elink'
57 });
58
59 } catch (error) {
60 console.error('eLink products error:', error);
61 return NextResponse.json(
62 { error: 'eLink endpoint unavailable', source: 'elink' },
63 { status: 503 }
64 );
65 }
66
67 } catch (error) {
68 console.error('eLink products error:', error);
69 return NextResponse.json(
70 { error: 'Failed to fetch products' },
71 { status: 500 }
72 );
73 }
74}