1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * Contact Logs API Endpoint
7 * Fetches and creates contact log entries for customers
9export async function GET(request: NextRequest) {
11 // Verify authentication
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 // Parse query parameters
21 const { searchParams } = new URL(request.url);
23 const customerId = searchParams.get('customerId');
24 const accountId = searchParams.get('accountId');
25 const limit = searchParams.get('limit') || '10';
27 if (!customerId && !accountId) {
28 return NextResponse.json(
29 { error: 'customerId or accountId required' },
34 // Build BUCK API parameters
35 let buckParams: Record<string, string> = {
36 "3": "retailmax.elink.contactlog.list",
37 "111": "101", // Filter type
38 "99": Math.random().toString()
42 buckParams["113"] = customerId;
46 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
48 const logs = response?.DATS || [];
50 return NextResponse.json({
56 console.error('Contact logs API error:', error);
57 return NextResponse.json(
58 { error: 'Failed to fetch contact logs' },
64 console.error('Contact logs API error:', error);
65 return NextResponse.json(
66 { error: 'Failed to fetch contact logs' },
72export async function POST(request: NextRequest) {
74 // Verify authentication
75 const authData = await getStoredAuth();
76 if (!authData || !authData.authenticated) {
77 return NextResponse.json(
78 { error: 'Authentication required' },
83 const body = await request.json();
84 const { customerId, accountId, message, reminder } = body;
86 if (!customerId && !accountId) {
87 return NextResponse.json(
88 { error: 'customerId or accountId required' },
94 return NextResponse.json(
95 { error: 'message required' },
100 // Build XML for DATI endpoint
102 xml += "<f8_s>retailmax.elink.contactlog.edit</f8_s>";
103 xml += "<f111_s>customer</f111_s>";
106 xml += `<f113_E>${customerId}</f113_E>`;
109 xml += `<f120_s>${escapeXml(message)}</f120_s>`;
112 xml += `<f130_s>${escapeXml(reminder)}</f130_s>`;
118 const response = await fetch(`${process.env.FIELDPINE_BASE_URL}/DATI`, {
121 'Content-Type': 'application/xml',
122 'Cookie': authData.apiKey || ''
128 throw new Error('Failed to save contact log');
131 const result = await response.json();
133 return NextResponse.json({
139 console.error('Save contact log error:', error);
140 return NextResponse.json(
141 { error: 'Failed to save contact log' },
147 console.error('Contact logs POST error:', error);
148 return NextResponse.json(
149 { error: 'Failed to save contact log' },
155function escapeXml(unsafe: string): string {
156 return unsafe.replace(/[<>&'"]/g, (c) => {
158 case '<': return '<';
159 case '>': return '>';
160 case '&': return '&';
161 case "'": return ''';
162 case '"': return '"';