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 * Customer Accounts Endpoint
7 * Uses Fieldpine's elink/BUCK API for account management
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 { success: false, error: 'Authentication required' },
16 { status: 401 }
17 );
18 }
19
20 const { searchParams } = new URL(request.url);
21 const type = searchParams.get('type') || 'list'; // list, summary, search
22 const search = searchParams.get('search');
23
24 try {
25 let buckParams: Record<string, string> = {};
26
27 if (type === 'summary') {
28 // Get account summary statistics
29 buckParams = {
30 "3": "retailmax.elink.account.summary",
31 "10": "600"
32 };
33 } else if (type === 'search' && search) {
34 // Search accounts by name
35 buckParams = {
36 "3": "retailmax.elink.account.detail",
37 "9": `f101,c,${encodeURIComponent(search)}`
38 };
39 } else {
40 // List all accounts
41 buckParams = {
42 "3": "retailmax.elink.account.detail",
43 "9": "f100,4,0",
44 "10": "1003,184,1100,1105,1107"
45 };
46 }
47
48 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
49
50 if (response?.DATS && Array.isArray(response.DATS)) {
51 return NextResponse.json({
52 success: true,
53 data: response.DATS,
54 source: 'elink'
55 });
56 } else {
57 return NextResponse.json({
58 success: true,
59 data: [],
60 source: 'elink'
61 });
62 }
63
64 } catch (error) {
65 console.error('Accounts error:', error);
66 return NextResponse.json(
67 { success: false, error: 'Failed to fetch accounts', source: 'elink' },
68 { status: 503 }
69 );
70 }
71
72 } catch (error) {
73 console.error('Accounts API error:', error);
74 return NextResponse.json(
75 { success: false, error: 'Failed to fetch accounts' },
76 { status: 500 }
77 );
78 }
79}
80
81export async function POST(request: NextRequest) {
82 try {
83 // Verify authentication
84 const authData = await getStoredAuth();
85 if (!authData || !authData.authenticated) {
86 return NextResponse.json(
87 { success: false, error: 'Authentication required' },
88 { status: 401 }
89 );
90 }
91
92 const body = await request.json();
93 const { action, data } = body;
94
95 try {
96 let xml = '';
97
98 if (action === 'create' || action === 'update') {
99 // Create or update account
100 xml = '<DATI><f8_s>retailmax.elink.account.edit</f8_s>';
101
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>`;
109
110 xml += '</DATI>';
111
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}`
118 };
119
120 const apiResponse = await fetch(url, {
121 method: 'POST',
122 headers,
123 body: xml,
124 });
125
126 if (!apiResponse.ok) {
127 throw new Error(`Account update failed: ${apiResponse.status} ${apiResponse.statusText}`);
128 }
129
130 const responseData = await apiResponse.json();
131
132 return NextResponse.json({
133 success: true,
134 data: responseData,
135 source: 'elink'
136 });
137 }
138
139 return NextResponse.json(
140 { success: false, error: 'Invalid action' },
141 { status: 400 }
142 );
143
144 } catch (error) {
145 console.error('Account update error:', error);
146 return NextResponse.json(
147 { success: false, error: 'Failed to update account', source: 'elink' },
148 { status: 503 }
149 );
150 }
151
152 } catch (error) {
153 console.error('Accounts API error:', error);
154 return NextResponse.json(
155 { success: false, error: 'Failed to process request' },
156 { status: 500 }
157 );
158 }
159}