4 * GET /api/v1/fd1/sales - List sales using FD1 protocol
5 * POST /api/v1/fd1/sales - Create new sale
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)
16import { NextRequest, NextResponse } from 'next/server';
17import { getRequestContext } from '@/lib/server/sessionUtils';
18import { createFD1Client } from '@/lib/client/fd1Client';
20export async function GET(request: NextRequest) {
22 const context = await getRequestContext(request);
24 if (!context?.session?.storeUrl) {
25 return NextResponse.json(
26 { success: false, error: 'Not authenticated' },
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);
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);
48 const fd1Client = createFD1Client({
49 baseUrl: context.session.storeUrl!,
50 apiKey: context.session.apiKey,
55 const sales = await fd1Client.listSales(
56 Object.keys(filters).length > 0 ? filters : undefined,
62 return NextResponse.json({
71 console.error('FD1 sales API error:', error);
72 return NextResponse.json(
75 error: error instanceof Error ? error.message : 'Failed to fetch sales',
82export async function POST(request: NextRequest) {
84 const context = await getRequestContext(request);
86 if (!context?.session?.storeUrl) {
87 return NextResponse.json(
88 { success: false, error: 'Not authenticated' },
93 const body = await request.json();
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' },
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' },
114 const fd1Client = createFD1Client({
115 baseUrl: context.session.storeUrl!,
116 apiKey: context.session.apiKey,
121 const sale = await fd1Client.createSale({
123 customer_id: body.customer_id,
124 location_id: body.location_id || context.session.storeId,
127 return NextResponse.json({
133 console.error('FD1 create sale API error:', error);
134 return NextResponse.json(
137 error: error instanceof Error ? error.message : 'Failed to create sale',