EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1/**
2 * FD1 Sales API Route
3 *
4 * GET /api/v1/fd1/sales - List sales using FD1 protocol
5 * POST /api/v1/fd1/sales - Create new sale
6 *
7 * Query parameters (GET):
8 * - date_from: Start date (YYYY-MM-DD)
9 * - date_to: End date (YYYY-MM-DD)
10 * - customer_id: Filter by customer
11 * - location_id: Filter by location
12 * - limit: Number of results (default 100)
13 * - offset: Pagination offset (default 0)
14 */
15
16import { NextRequest, NextResponse } from 'next/server';
17import { getRequestContext } from '@/lib/server/sessionUtils';
18import { createFD1Client } from '@/lib/client/fd1Client';
19
20export async function GET(request: NextRequest) {
21 try {
22 const context = await getRequestContext(request);
23
24 if (!context?.session?.storeUrl) {
25 return NextResponse.json(
26 { success: false, error: 'Not authenticated' },
27 { status: 401 }
28 );
29 }
30
31 // Parse query parameters
32 const searchParams = request.nextUrl.searchParams;
33 const date_from = searchParams.get('date_from');
34 const date_to = searchParams.get('date_to');
35 const customer_id = searchParams.get('customer_id');
36 const location_id = searchParams.get('location_id');
37 const limit = parseInt(searchParams.get('limit') || '100', 10);
38 const offset = parseInt(searchParams.get('offset') || '0', 10);
39
40 // Build query filters
41 const filters: Record<string, any> = {};
42 if (date_from) filters.date_from = date_from;
43 if (date_to) filters.date_to = date_to;
44 if (customer_id) filters.customer_id = parseInt(customer_id, 10);
45 if (location_id) filters.location_id = parseInt(location_id, 10);
46
47 // Create FD1 client
48 const fd1Client = createFD1Client({
49 baseUrl: context.session.storeUrl!,
50 apiKey: context.session.apiKey,
51 protocol: 'fd3',
52 });
53
54 // Fetch sales
55 const sales = await fd1Client.listSales(
56 Object.keys(filters).length > 0 ? filters : undefined,
57 undefined,
58 limit,
59 offset
60 );
61
62 return NextResponse.json({
63 success: true,
64 data: sales,
65 count: sales.length,
66 limit,
67 offset,
68 });
69
70 } catch (error) {
71 console.error('FD1 sales API error:', error);
72 return NextResponse.json(
73 {
74 success: false,
75 error: error instanceof Error ? error.message : 'Failed to fetch sales',
76 },
77 { status: 500 }
78 );
79 }
80}
81
82export async function POST(request: NextRequest) {
83 try {
84 const context = await getRequestContext(request);
85
86 if (!context?.session?.storeUrl) {
87 return NextResponse.json(
88 { success: false, error: 'Not authenticated' },
89 { status: 401 }
90 );
91 }
92
93 const body = await request.json();
94
95 // Validate request body
96 if (!body.lines || !Array.isArray(body.lines) || body.lines.length === 0) {
97 return NextResponse.json(
98 { success: false, error: 'Sales must have at least one line item' },
99 { status: 400 }
100 );
101 }
102
103 // Validate line items
104 for (const line of body.lines) {
105 if (!line.product_id || !line.qty) {
106 return NextResponse.json(
107 { success: false, error: 'Each line must have product_id and qty' },
108 { status: 400 }
109 );
110 }
111 }
112
113 // Create FD1 client
114 const fd1Client = createFD1Client({
115 baseUrl: context.session.storeUrl!,
116 apiKey: context.session.apiKey,
117 protocol: 'fd3',
118 });
119
120 // Create sale
121 const sale = await fd1Client.createSale({
122 lines: body.lines,
123 customer_id: body.customer_id,
124 location_id: body.location_id || context.session.storeId,
125 });
126
127 return NextResponse.json({
128 success: true,
129 data: sale,
130 });
131
132 } catch (error) {
133 console.error('FD1 create sale API error:', error);
134 return NextResponse.json(
135 {
136 success: false,
137 error: error instanceof Error ? error.message : 'Failed to create sale',
138 },
139 { status: 500 }
140 );
141 }
142}