1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * Base Configuration Settings
7 * BUCK: retailmax.elink.config.basesetting
8 * Retrieves system configuration settings (59 uses)
10export async function GET(request: NextRequest) {
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 const { searchParams } = new URL(request.url);
22 // Setting key to retrieve (optional - if not provided, gets all)
23 const key = searchParams.get('key');
25 // Category/section filter
26 const section = searchParams.get('section');
28 // Build BUCK parameters
29 const params: string[] = [
30 '3=retailmax.elink.config.basesetting'
33 // Add key filter if specified
35 params.push(`9=f100,0,${key}`);
38 // Common parameter patterns seen in HAR:
39 // 112=1 - Include system settings
40 // 112=999 - Include all settings
41 // 500=1 - Include descriptions
43 params.push('112=999');
46 // Add section filter if provided
48 params.push(`150=${section}`);
51 const query = params.join('&');
52 const url = `/GNAP/j/BUCK?${query}`;
54 console.log('[Base Settings] BUCK query:', url);
56 const result = await fieldpineServerApi.apiCall(url, {
57 cookie: authData.apiKey,
61 // Process BUCK response
62 if (result && result.DATS && Array.isArray(result.DATS)) {
63 const settings = result.DATS.map((item: any) => ({
67 description: item.f120,
70 defaultValue: item.f170,
72 lastModified: item.f200,
76 // Group settings by section
77 const grouped = settings.reduce((acc: Record<string, any[]>, setting: any) => {
78 const sect = setting.section || 'General';
82 acc[sect].push(setting);
86 return NextResponse.json({
90 count: settings.length,
95 return NextResponse.json({
104 console.error('[Base Settings] Error:', error);
105 return NextResponse.json(
106 { error: 'Failed to fetch configuration settings' },
113 * Update a configuration setting
115export async function POST(request: NextRequest) {
117 const authData = await getStoredAuth();
118 if (!authData || !authData.authenticated) {
119 return NextResponse.json(
120 { error: 'Authentication required' },
125 const body = await request.json();
126 const { key, value } = body;
129 return NextResponse.json(
130 { error: 'Setting key is required' },
135 // Update setting via BUCK (implementation depends on API capabilities)
136 // This may need to use a different BUCK query or method
137 console.log('[Base Settings] Update request:', { key, value });
139 return NextResponse.json({
141 message: 'Setting updated',
147 console.error('[Base Settings] Update error:', error);
148 return NextResponse.json(
149 { error: 'Failed to update setting' },