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 * GET /api/v1/elink/sales/flat
7 * Fetch flattened sales list with line items, customer, and product details
8 * Query params:
9 * - startDate: Start date filter (YYYY-MM-DD)
10 * - endDate: End date filter (YYYY-MM-DD)
11 * - limit: Max number of records (default 200)
12 * - includeCustomer: Include customer details (default true)
13 * - includeProduct: Include product details (default true)
14 */
15export async function GET(request: NextRequest) {
16 try {
17 const context = await getRequestContext(request);
18 if (!context?.session) {
19 return NextResponse.json(
20 { success: false, error: 'Not authenticated' },
21 { status: 401 }
22 );
23 }
24
25 const { searchParams } = new URL(request.url);
26 const startDate = searchParams.get('startDate');
27 const endDate = searchParams.get('endDate');
28 const limit = searchParams.get('limit') || '200';
29 const includeCustomer = searchParams.get('includeCustomer') !== 'false';
30 const includeProduct = searchParams.get('includeProduct') !== 'false';
31
32 console.log('[Sales Flat] Fetching flat sales list:', {
33 startDate,
34 endDate,
35 limit,
36 includeCustomer,
37 includeProduct
38 });
39
40 // Build BUCK parameters for retailmax.elink.saleflat.list
41 const buckParams: Record<string, string> = {
42 '3': 'retailmax.elink.saleflat.list',
43 '8': limit,
44 '103': includeCustomer ? '1' : '0', // Include customer details
45 '104': includeProduct ? '1' : '0', // Include product details
46 // Field list: sale date, location name, sale ID, total, tax, payment method, product, qty, line total
47 '10': '102,106,101,107,104,201,202,203,301'
48 };
49
50 // Add date filters if provided
51 if (startDate) {
52 buckParams['9'] = `f102,4,${startDate}`; // >= start date
53 }
54 if (endDate) {
55 buckParams['9.1'] = `f102,1,${endDate}`; // <= end date
56 }
57
58 console.log('[Sales Flat] BUCK params:', buckParams);
59
60 const result = await fieldpineServerApi.buckApiCall(buckParams, context.session.apiKey);
61
62 if (!result || !result.APPT) {
63 return NextResponse.json({
64 success: true,
65 data: [],
66 message: 'No sales found for the specified period'
67 });
68 }
69
70 console.log(`[Sales Flat] Successfully fetched ${result.APPT.length} line items`);
71
72 return NextResponse.json({
73 success: true,
74 data: result.APPT,
75 count: result.APPT.length
76 });
77
78 } catch (error) {
79 console.error('[Sales Flat] Error:', error);
80
81 // Check if it's a 403 error
82 if (error instanceof Error && error.message.includes('403')) {
83 return NextResponse.json(
84 {
85 success: false,
86 error: 'Authentication expired. Please log in again.'
87 },
88 { status: 403 }
89 );
90 }
91
92 return NextResponse.json(
93 {
94 success: false,
95 error: error instanceof Error ? error.message : 'Failed to fetch sales data'
96 },
97 { status: 500 }
98 );
99 }
100}