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
5export async function POST(request: NextRequest) {
6 try {
7 const context = await getRequestContext(request);
8 if (!context?.isAuthenticated || !context?.session?.apiKey) {
9 return NextResponse.json(
10 { error: 'Unauthorized' },
11 { status: 401 }
12 );
13 }
14
15 // Rate limiting
16 const clientId = request.headers.get('x-forwarded-for') ||
17 request.headers.get('x-real-ip') ||
18 context.session.userId ||
19 'unknown';
20 if (!fieldpineServerApi.checkClientRateLimit(clientId)) {
21 return NextResponse.json(
22 { error: 'Rate limit exceeded' },
23 { status: 429 }
24 );
25 }
26
27 const saleData = await request.json();
28 console.log('Sale data received:', JSON.stringify(saleData, null, 2));
29
30 // Use authenticated API call with store-specific URL
31 const result = await fieldpineServerApi.createSale(saleData, context.session.apiKey, context.store.url);
32 console.log('Sale created successfully:', result);
33
34 return NextResponse.json({ success: true, data: result });
35
36 } catch (error) {
37 console.error('Sales API error:', error);
38 console.error('Error details:', error instanceof Error ? error.message : 'Unknown error');
39
40 // For now, simulate success since the API might not be fully implemented
41 // TODO: Implement proper DATI-based sale creation for Fieldpine
42 return NextResponse.json({
43 success: true,
44 data: {
45 saleId: `DEMO_SALE_${Date.now()}`,
46 status: 'demo_mode',
47 warning: 'Sale was not sent to Fieldpine - demo mode active',
48 note: 'API endpoint returned 400 - payload format may be incorrect'
49 }
50 });
51 }
52}