1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * Customer Accounts Endpoint
7 * Uses Fieldpine's elink/BUCK API for account management
9export async function GET(request: NextRequest) {
11 // Verify authentication
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { success: false, error: 'Authentication required' },
20 const { searchParams } = new URL(request.url);
21 const type = searchParams.get('type') || 'list'; // list, summary, search
22 const search = searchParams.get('search');
25 let buckParams: Record<string, string> = {};
27 if (type === 'summary') {
28 // Get account summary statistics
30 "3": "retailmax.elink.account.summary",
33 } else if (type === 'search' && search) {
34 // Search accounts by name
36 "3": "retailmax.elink.account.detail",
37 "9": `f101,c,${encodeURIComponent(search)}`
42 "3": "retailmax.elink.account.detail",
44 "10": "1003,184,1100,1105,1107"
48 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
50 if (response?.DATS && Array.isArray(response.DATS)) {
51 return NextResponse.json({
57 return NextResponse.json({
65 console.error('Accounts error:', error);
66 return NextResponse.json(
67 { success: false, error: 'Failed to fetch accounts', source: 'elink' },
73 console.error('Accounts API error:', error);
74 return NextResponse.json(
75 { success: false, error: 'Failed to fetch accounts' },
81export async function POST(request: NextRequest) {
83 // Verify authentication
84 const authData = await getStoredAuth();
85 if (!authData || !authData.authenticated) {
86 return NextResponse.json(
87 { success: false, error: 'Authentication required' },
92 const body = await request.json();
93 const { action, data } = body;
98 if (action === 'create' || action === 'update') {
99 // Create or update account
100 xml = '<DATI><f8_s>retailmax.elink.account.edit</f8_s>';
102 if (data.id) xml += `<f100>${data.id}</f100>`;
103 if (data.name) xml += `<f101>${data.name}</f101>`;
104 if (data.creditLimit !== undefined) xml += `<f103>${data.creditLimit}</f103>`;
105 if (data.floorLimit !== undefined) xml += `<f105>${data.floorLimit}</f105>`;
106 if (data.email) xml += `<f1100>${data.email}</f1100>`;
107 if (data.receiptFormat) xml += `<f1105>${data.receiptFormat}</f1105>`;
108 if (data.externalId) xml += `<f184>${data.externalId}</f184>`;
112 // POST DATI XML to BUCK endpoint
113 const url = `${process.env.FIELDPINE_BASE_URL || "https://iig.cwanz.online"}/GNAP/j/buck`;
114 const headers: Record<string, string> = {
115 'Accept': 'application/json',
116 'Content-Type': 'text/xml',
117 'Cookie': `FieldpineApiKey=${authData.apiKey}`
120 const apiResponse = await fetch(url, {
126 if (!apiResponse.ok) {
127 throw new Error(`Account update failed: ${apiResponse.status} ${apiResponse.statusText}`);
130 const responseData = await apiResponse.json();
132 return NextResponse.json({
139 return NextResponse.json(
140 { success: false, error: 'Invalid action' },
145 console.error('Account update error:', error);
146 return NextResponse.json(
147 { success: false, error: 'Failed to update account', source: 'elink' },
153 console.error('Accounts API error:', error);
154 return NextResponse.json(
155 { success: false, error: 'Failed to process request' },