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 { params }: { params: Promise<{ id: string }> }
8) {
9 try {
10 const customerId = parseInt(await params.then(p => p.id), 10);
11 if (isNaN(customerId)) {
12 return NextResponse.json(
13 { success: false, error: 'Invalid customer ID' },
14 { status: 400 }
15 );
16 }
17
18 const auth = await getStoredAuth();
19 if (!auth?.authenticated) {
20 return NextResponse.json(
21 { success: false, error: 'Not authenticated' },
22 { status: 401 }
23 );
24 }
25
26 const customer = await fieldpineServerApi.getCustomerById(customerId);
27
28 if (!customer) {
29 return NextResponse.json(
30 { success: false, error: 'Customer not found' },
31 { status: 404 }
32 );
33 }
34
35 return NextResponse.json({
36 success: true,
37 data: customer,
38 });
39 } catch (error: any) {
40 console.error('Error fetching customer:', error);
41 return NextResponse.json(
42 { success: false, error: error.message || 'Failed to fetch customer' },
43 { status: 500 }
44 );
45 }
46}
47
48export async function PUT(
49 request: NextRequest,
50 { params }: { params: Promise<{ id: string }> }
51) {
52 try {
53 const customerId = parseInt(await params.then(p => p.id), 10);
54 if (isNaN(customerId)) {
55 return NextResponse.json(
56 { success: false, error: 'Invalid customer ID' },
57 { status: 400 }
58 );
59 }
60
61 const auth = await getStoredAuth();
62 if (!auth?.authenticated) {
63 return NextResponse.json(
64 { success: false, error: 'Not authenticated' },
65 { status: 401 }
66 );
67 }
68
69 const body = await request.json();
70
71 // Build DATI XML for customer update
72 const datiFields: any = {
73 f8_s: 'retailmax.elink.customers.edit',
74 f11_B: 'E', // Edit operation
75 f100_E: customerId.toString(),
76 };
77
78 // Map fields to Fieldpine field codes
79 if (body.name !== undefined) datiFields.f101_s = body.name;
80 if (body.company !== undefined) datiFields.f154_s = body.company;
81 if (body.phone !== undefined) datiFields.f111_s = body.phone;
82 if (body.phone2 !== undefined) datiFields.f112_s = body.phone2;
83 if (body.mobile !== undefined) datiFields.f149_s = body.mobile;
84 if (body.email !== undefined) datiFields.f150_s = body.email;
85 if (body.division !== undefined) datiFields.f13_s = body.division;
86
87 // Build XML
88 const xmlLines = ['<DATI>'];
89 for (const [key, value] of Object.entries(datiFields)) {
90 xmlLines.push(` <${key}>${escapeXml(value as string)}</${key}>`);
91 }
92 xmlLines.push('</DATI>');
93 const xml = xmlLines.join('\n');
94
95 console.log('Updating customer with XML:', xml);
96
97 // Send DATI request to Fieldpine
98 const response = await fieldpineServerApi.datiApiCall(xml);
99
100 if (response.success) {
101 return NextResponse.json({
102 success: true,
103 data: response.data,
104 message: 'Customer updated successfully',
105 });
106 } else {
107 return NextResponse.json(
108 { success: false, error: response.error || 'Update failed' },
109 { status: 500 }
110 );
111 }
112 } catch (error: any) {
113 console.error('Error updating customer:', error);
114 return NextResponse.json(
115 { success: false, error: error.message || 'Failed to update customer' },
116 { status: 500 }
117 );
118 }
119}
120
121// Helper function to escape XML special characters
122function escapeXml(str: string): string {
123 if (!str) return '';
124 return str
125 .replace(/&/g, '&amp;')
126 .replace(/</g, '&lt;')
127 .replace(/>/g, '&gt;')
128 .replace(/"/g, '&quot;')
129 .replace(/'/g, '&apos;');
130}