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
5export async function GET(
6 request: NextRequest,
7 context: { params: Promise<{ id: string }> }
8) {
9 try {
10 const { id: saleId } = await context.params;
11
12 // Get authentication from server-side store
13 const authData = await getStoredAuth();
14 if (!authData?.authenticated) {
15 return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
16 }
17
18 // Fetch sale details using saleflat.list for denormalized data with line items
19 console.log(`[Sale Details API] Fetching sale ${saleId}`);
20
21 // Use saleflat.list which returns denormalized sale data including line items and payments
22 let response = await fieldpineServerApi.buckApiCall(
23 {
24 '3': 'retailmax.elink.saleflat.list',
25 '9': `f100,eq,${saleId}`, // Filter by sale ID
26 '8': '1000'
27 },
28 authData.apiKey
29 );
30
31 console.log(`[Sale Details API] saleflat.list returned ${response?.DATS?.length || 0} records`);
32
33 if (!response?.DATS || response.DATS.length === 0) {
34 // Fallback to sale.list if saleflat doesn't work
35 console.log(`[Sale Details API] Trying sale.list with picking filter...`);
36 response = await fieldpineServerApi.buckApiCall(
37 {
38 '3': 'retailmax.elink.sale.list',
39 '9': 'f108,in,200,202',
40 '10': '2000,2001,2003,7000,905,906,907,130,131,133,902',
41 '8': '1000'
42 },
43 authData.apiKey
44 );
45
46 const sale = response?.DATS?.find((s: any) => s.f100 == saleId);
47
48 if (!sale) {
49 return NextResponse.json(
50 { success: false, error: `Sale ${saleId} not found` },
51 { status: 404 }
52 );
53 }
54
55 console.log(`[Sale Details API] Found sale ${saleId} via sale.list (no line items available)`);
56 return NextResponse.json({
57 success: true,
58 data: sale
59 });
60 }
61
62 // saleflat.list returns one row per line item, so we need to aggregate
63 const saleRows = response.DATS;
64 const firstRow = saleRows[0];
65
66 // Build LINE array from flat rows
67 const lineItems = saleRows.map((row: any) => ({
68 f200: row.f200, // Product ID
69 f202: row.f202, // Qty
70 f205: row.f205, // Total Price
71 f212: row.f212, // Description
72 f213: row.f213, // PLU
73 f303: row.f303, // Total Inc Tax
74 f304: row.f304, // Total Ex Tax
75 f305: row.f305 // Tax
76 }));
77
78 // Build sale object with aggregated data
79 const sale: any = {
80 f100: firstRow.f100,
81 f101: firstRow.f101,
82 f102: firstRow.f102,
83 f108: firstRow.f108,
84 f109: firstRow.f109,
85 f117: firstRow.f117,
86 f125: firstRow.f125,
87 f130: firstRow.f130,
88 f133: firstRow.f133,
89 LINE: lineItems,
90 PAYM: firstRow.PAYM, // Payments might still be in sub-array
91 CUST: firstRow.CUST,
92 DELI: firstRow.DELI
93 };
94
95 console.log(`[Sale Details API] Built sale ${saleId} with ${lineItems.length} line items`);
96
97 return NextResponse.json({
98 success: true,
99 data: sale
100 });
101 } catch (error: any) {
102 console.error('[Sale Details API] Error fetching sale details:', error);
103 console.error('[Sale Details API] Error message:', error?.message);
104 return NextResponse.json(
105 { success: false, error: error?.message || 'Failed to fetch sale details' },
106 { status: 500 }
107 );
108 }
109}