1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
5export async function GET(
7 { params }: { params: Promise<{ id: string }> }
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' },
18 const auth = await getStoredAuth();
19 if (!auth?.authenticated) {
20 return NextResponse.json(
21 { success: false, error: 'Not authenticated' },
26 const customer = await fieldpineServerApi.getCustomerById(customerId);
29 return NextResponse.json(
30 { success: false, error: 'Customer not found' },
35 return NextResponse.json({
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' },
48export async function PUT(
50 { params }: { params: Promise<{ id: string }> }
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' },
61 const auth = await getStoredAuth();
62 if (!auth?.authenticated) {
63 return NextResponse.json(
64 { success: false, error: 'Not authenticated' },
69 const body = await request.json();
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(),
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;
88 const xmlLines = ['<DATI>'];
89 for (const [key, value] of Object.entries(datiFields)) {
90 xmlLines.push(` <${key}>${escapeXml(value as string)}</${key}>`);
92 xmlLines.push('</DATI>');
93 const xml = xmlLines.join('\n');
95 console.log('Updating customer with XML:', xml);
97 // Send DATI request to Fieldpine
98 const response = await fieldpineServerApi.datiApiCall(xml);
100 if (response.success) {
101 return NextResponse.json({
104 message: 'Customer updated successfully',
107 return NextResponse.json(
108 { success: false, error: response.error || 'Update failed' },
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' },
121// Helper function to escape XML special characters
122function escapeXml(str: string): string {
125 .replace(/&/g, '&')
126 .replace(/</g, '<')
127 .replace(/>/g, '>')
128 .replace(/"/g, '"')
129 .replace(/'/g, ''');