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 Sale Detail API Route
3 *
4 * GET /api/v1/fd1/sales/[id] - Get single sale by ID with line items
5 */
6
7import { NextRequest, NextResponse } from 'next/server';
8import { getRequestContext } from '@/lib/server/sessionUtils';
9import { createFD1Client } from '@/lib/client/fd1Client';
10
11export async function GET(
12 request: NextRequest,
13 { params }: { params: Promise<{ id: string }> }
14) {
15 try {
16 const context = await getRequestContext(request);
17
18 if (!context?.session?.storeUrl) {
19 return NextResponse.json(
20 { success: false, error: 'Not authenticated' },
21 { status: 401 }
22 );
23 }
24
25 const { id } = await params;
26 const saleId = parseInt(id, 10);
27
28 if (isNaN(saleId)) {
29 return NextResponse.json(
30 { success: false, error: 'Invalid sale ID' },
31 { status: 400 }
32 );
33 }
34
35 // Create FD1 client
36 const fd1Client = createFD1Client({
37 baseUrl: context.session.storeUrl!,
38 apiKey: context.session.apiKey,
39 protocol: 'fd3',
40 });
41
42 // Fetch sale
43 const sale = await fd1Client.getSale(saleId);
44
45 if (!sale) {
46 return NextResponse.json(
47 { success: false, error: 'Sale not found' },
48 { status: 404 }
49 );
50 }
51
52 return NextResponse.json({
53 success: true,
54 data: sale,
55 });
56
57 } catch (error) {
58 console.error('FD1 sale detail API error:', error);
59 return NextResponse.json(
60 {
61 success: false,
62 error: error instanceof Error ? error.message : 'Failed to fetch sale',
63 },
64 { status: 500 }
65 );
66 }
67}