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 Customer Detail API Route
3 *
4 * GET /api/v1/fd1/customers/[id] - Get single customer by ID
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 customerId = parseInt(id, 10);
27
28 if (isNaN(customerId)) {
29 return NextResponse.json(
30 { success: false, error: 'Invalid customer 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 customer
43 const customer = await fd1Client.getCustomer(customerId);
44
45 if (!customer) {
46 return NextResponse.json(
47 { success: false, error: 'Customer not found' },
48 { status: 404 }
49 );
50 }
51
52 return NextResponse.json({
53 success: true,
54 data: customer,
55 });
56
57 } catch (error) {
58 console.error('FD1 customer detail API error:', error);
59 return NextResponse.json(
60 {
61 success: false,
62 error: error instanceof Error ? error.message : 'Failed to fetch customer',
63 },
64 { status: 500 }
65 );
66 }
67}