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 * Account Customers Endpoint
7 * Gets customers linked to an account
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 accountId = searchParams.get('accountId');
22
23 if (!accountId) {
24 return NextResponse.json(
25 { success: false, error: 'Account ID required' },
26 { status: 400 }
27 );
28 }
29
30 try {
31 const buckParams = {
32 "3": "retailmax.elink.customers",
33 "9": `f132,0,${accountId}`
34 };
35
36 const response = await fieldpineServerApi.buckApiCall(buckParams, authData.apiKey);
37
38 if (response?.DATS && Array.isArray(response.DATS)) {
39 return NextResponse.json({
40 success: true,
41 data: response.DATS,
42 source: 'elink'
43 });
44 } else {
45 return NextResponse.json({
46 success: true,
47 data: [],
48 source: 'elink'
49 });
50 }
51
52 } catch (error) {
53 console.error('Account customers error:', error);
54 return NextResponse.json(
55 { success: false, error: 'Failed to fetch customers', source: 'elink' },
56 { status: 503 }
57 );
58 }
59
60 } catch (error) {
61 console.error('Customers API error:', error);
62 return NextResponse.json(
63 { success: false, error: 'Failed to fetch customers' },
64 { status: 500 }
65 );
66 }
67}
68
69export async function POST(request: NextRequest) {
70 try {
71 // Verify authentication
72 const authData = await getStoredAuth();
73 if (!authData || !authData.authenticated) {
74 return NextResponse.json(
75 { success: false, error: 'Authentication required' },
76 { status: 401 }
77 );
78 }
79
80 const body = await request.json();
81 const { action, customerId, accountId } = body;
82
83 try {
84 let xml = '<DATI><f8_s>retailmax.elink.customers.edit</f8_s>';
85
86 if (action === 'link') {
87 xml += `<f100>${customerId}</f100><f132>${accountId}</f132>`;
88 } else if (action === 'unlink') {
89 xml += `<f100>${customerId}</f100><f132>0</f132>`;
90 }
91
92 xml += '</DATI>';
93
94 // POST DATI XML to BUCK endpoint
95 const url = `${process.env.FIELDPINE_BASE_URL || "https://iig.cwanz.online"}/GNAP/j/buck`;
96 const headers: Record<string, string> = {
97 'Accept': 'application/json',
98 'Content-Type': 'text/xml',
99 'Cookie': `FieldpineApiKey=${authData.apiKey}`
100 };
101
102 const apiResponse = await fetch(url, {
103 method: 'POST',
104 headers,
105 body: xml,
106 });
107
108 if (!apiResponse.ok) {
109 throw new Error(`Customer update failed: ${apiResponse.status} ${apiResponse.statusText}`);
110 }
111
112 const responseData = await apiResponse.json();
113
114 return NextResponse.json({
115 success: true,
116 data: responseData,
117 source: 'elink'
118 });
119
120 } catch (error) {
121 console.error('Customer link/unlink error:', error);
122 return NextResponse.json(
123 { success: false, error: 'Failed to update customer link', source: 'elink' },
124 { status: 503 }
125 );
126 }
127
128 } catch (error) {
129 console.error('Customers API error:', error);
130 return NextResponse.json(
131 { success: false, error: 'Failed to process request' },
132 { status: 500 }
133 );
134 }
135}