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
5/**
6 * Contact Logs API Endpoint
7 * Fetches and creates contact log entries for customers
8 */
9export async function GET(request: NextRequest) {
10 try {
11 // Verify authentication
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
16 { status: 401 }
17 );
18 }
19
20 // Parse query parameters
21 const { searchParams } = new URL(request.url);
22
23 const customerId = searchParams.get('customerId');
24 const accountId = searchParams.get('accountId');
25 const limit = searchParams.get('limit') || '10';
26
27 if (!customerId && !accountId) {
28 return NextResponse.json(
29 { error: 'customerId or accountId required' },
30 { status: 400 }
31 );
32 }
33
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()
39 };
40
41 if (customerId) {
42 buckParams["113"] = customerId;
43 }
44
45 try {
46 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
47
48 const logs = response?.DATS || [];
49
50 return NextResponse.json({
51 success: true,
52 data: logs
53 });
54
55 } catch (error) {
56 console.error('Contact logs API error:', error);
57 return NextResponse.json(
58 { error: 'Failed to fetch contact logs' },
59 { status: 503 }
60 );
61 }
62
63 } catch (error) {
64 console.error('Contact logs API error:', error);
65 return NextResponse.json(
66 { error: 'Failed to fetch contact logs' },
67 { status: 500 }
68 );
69 }
70}
71
72export async function POST(request: NextRequest) {
73 try {
74 // Verify authentication
75 const authData = await getStoredAuth();
76 if (!authData || !authData.authenticated) {
77 return NextResponse.json(
78 { error: 'Authentication required' },
79 { status: 401 }
80 );
81 }
82
83 const body = await request.json();
84 const { customerId, accountId, message, reminder } = body;
85
86 if (!customerId && !accountId) {
87 return NextResponse.json(
88 { error: 'customerId or accountId required' },
89 { status: 400 }
90 );
91 }
92
93 if (!message) {
94 return NextResponse.json(
95 { error: 'message required' },
96 { status: 400 }
97 );
98 }
99
100 // Build XML for DATI endpoint
101 let xml = "<DATI>";
102 xml += "<f8_s>retailmax.elink.contactlog.edit</f8_s>";
103 xml += "<f111_s>customer</f111_s>";
104
105 if (customerId) {
106 xml += `<f113_E>${customerId}</f113_E>`;
107 }
108
109 xml += `<f120_s>${escapeXml(message)}</f120_s>`;
110
111 if (reminder) {
112 xml += `<f130_s>${escapeXml(reminder)}</f130_s>`;
113 }
114
115 xml += "</DATI>";
116
117 try {
118 const response = await fetch(`${process.env.FIELDPINE_BASE_URL}/DATI`, {
119 method: 'POST',
120 headers: {
121 'Content-Type': 'application/xml',
122 'Cookie': authData.apiKey || ''
123 },
124 body: xml
125 });
126
127 if (!response.ok) {
128 throw new Error('Failed to save contact log');
129 }
130
131 const result = await response.json();
132
133 return NextResponse.json({
134 success: true,
135 data: result
136 });
137
138 } catch (error) {
139 console.error('Save contact log error:', error);
140 return NextResponse.json(
141 { error: 'Failed to save contact log' },
142 { status: 503 }
143 );
144 }
145
146 } catch (error) {
147 console.error('Contact logs POST error:', error);
148 return NextResponse.json(
149 { error: 'Failed to save contact log' },
150 { status: 500 }
151 );
152 }
153}
154
155function escapeXml(unsafe: string): string {
156 return unsafe.replace(/[<>&'"]/g, (c) => {
157 switch (c) {
158 case '<': return '&lt;';
159 case '>': return '&gt;';
160 case '&': return '&amp;';
161 case "'": return '&apos;';
162 case '"': return '&quot;';
163 default: return c;
164 }
165 });
166}