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 * Base Configuration Settings
7 * BUCK: retailmax.elink.config.basesetting
8 * Retrieves system configuration settings (59 uses)
9 */
10export async function GET(request: NextRequest) {
11 try {
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 const { searchParams } = new URL(request.url);
21
22 // Setting key to retrieve (optional - if not provided, gets all)
23 const key = searchParams.get('key');
24
25 // Category/section filter
26 const section = searchParams.get('section');
27
28 // Build BUCK parameters
29 const params: string[] = [
30 '3=retailmax.elink.config.basesetting'
31 ];
32
33 // Add key filter if specified
34 if (key) {
35 params.push(`9=f100,0,${key}`);
36 }
37
38 // Common parameter patterns seen in HAR:
39 // 112=1 - Include system settings
40 // 112=999 - Include all settings
41 // 500=1 - Include descriptions
42 params.push('112=1');
43 params.push('112=999');
44 params.push('500=1');
45
46 // Add section filter if provided
47 if (section) {
48 params.push(`150=${section}`);
49 }
50
51 const query = params.join('&');
52 const url = `/GNAP/j/BUCK?${query}`;
53
54 console.log('[Base Settings] BUCK query:', url);
55
56 const result = await fieldpineServerApi.apiCall(url, {
57 cookie: authData.apiKey,
58 useOpenApi: false
59 });
60
61 // Process BUCK response
62 if (result && result.DATS && Array.isArray(result.DATS)) {
63 const settings = result.DATS.map((item: any) => ({
64 key: item.f100,
65 name: item.f101,
66 value: item.f110,
67 description: item.f120,
68 section: item.f150,
69 dataType: item.f160,
70 defaultValue: item.f170,
71 isSystem: item.f180,
72 lastModified: item.f200,
73 modifiedBy: item.f201
74 }));
75
76 // Group settings by section
77 const grouped = settings.reduce((acc: Record<string, any[]>, setting: any) => {
78 const sect = setting.section || 'General';
79 if (!acc[sect]) {
80 acc[sect] = [];
81 }
82 acc[sect].push(setting);
83 return acc;
84 }, {});
85
86 return NextResponse.json({
87 success: true,
88 data: settings,
89 grouped,
90 count: settings.length,
91 source: 'buck'
92 });
93 }
94
95 return NextResponse.json({
96 success: true,
97 data: [],
98 grouped: {},
99 count: 0,
100 source: 'buck'
101 });
102
103 } catch (error) {
104 console.error('[Base Settings] Error:', error);
105 return NextResponse.json(
106 { error: 'Failed to fetch configuration settings' },
107 { status: 500 }
108 );
109 }
110}
111
112/**
113 * Update a configuration setting
114 */
115export async function POST(request: NextRequest) {
116 try {
117 const authData = await getStoredAuth();
118 if (!authData || !authData.authenticated) {
119 return NextResponse.json(
120 { error: 'Authentication required' },
121 { status: 401 }
122 );
123 }
124
125 const body = await request.json();
126 const { key, value } = body;
127
128 if (!key) {
129 return NextResponse.json(
130 { error: 'Setting key is required' },
131 { status: 400 }
132 );
133 }
134
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 });
138
139 return NextResponse.json({
140 success: true,
141 message: 'Setting updated',
142 key,
143 value
144 });
145
146 } catch (error) {
147 console.error('[Base Settings] Update error:', error);
148 return NextResponse.json(
149 { error: 'Failed to update setting' },
150 { status: 500 }
151 );
152 }
153}