1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * Messaging Configuration Endpoint
7 * GET /api/v1/messaging/config - Get all messaging accounts
8 * POST /api/v1/messaging/config - Create new account
9 * PUT /api/v1/messaging/config - Update existing account
12export async function GET(request: NextRequest) {
14 const authData = await getStoredAuth();
15 if (!authData || !authData.authenticated) {
16 return NextResponse.json(
17 { error: 'Authentication required' },
22 console.log('[Messaging Config API] Fetching accounts');
24 const response = await fieldpineServerApi.apiCall('/buck', {
26 '3': 'messaging.config'
28 cookie: authData.apiKey,
32 return NextResponse.json({
38 console.error('[Messaging Config API] Error:', error);
39 return NextResponse.json(
40 { error: 'Failed to fetch messaging config' },
46export async function POST(request: NextRequest) {
48 const authData = await getStoredAuth();
49 if (!authData || !authData.authenticated) {
50 return NextResponse.json(
51 { error: 'Authentication required' },
56 const body = await request.json();
57 console.log('[Messaging Config API] Creating account:', body);
59 // Build DATI packet for creating new account
60 const datiData: any = {
61 f8_s: 'messaging.config.edit',
65 // Map fields based on account type
66 if (body.f109) datiData.f109_s = body.f109; // Account name
67 if (body.f110) datiData.f110_s = body.f110; // Type
70 if (body.f101) datiData.f101_s = body.f101; // Server
71 if (body.f102) datiData.f102_s = body.f102; // Port
72 if (body.f103) datiData.f103_s = body.f103; // User
73 if (body.f104) datiData.f104_s = body.f104; // Password
74 if (body.f128) datiData.f128_E = body.f128; // Well known server
77 if (body.f112) datiData.f112_s = body.f112; // From name
78 if (body.f118) datiData.f118_s = body.f118; // Reply to
79 if (body.f190 !== undefined) datiData.f190_E = body.f190;
80 if (body.f191 !== undefined) datiData.f191_E = body.f191;
81 if (body.f192 !== undefined) datiData.f192_E = body.f192;
82 if (body.f193 !== undefined) datiData.f193_E = body.f193;
85 if (body.f130) datiData.f130_s = body.f130; // Provider
86 if (body.f133) datiData.f133_s = body.f133; // API Key
87 if (body.f134) datiData.f134_s = body.f134; // API Secret
90 if (body.f131) datiData.f131_s = body.f131; // File types
91 if (body.f132) datiData.f132_s = body.f132; // Output folder
94 if (body.f140) datiData.f140_s = body.f140; // Bucket/Country
95 if (body.f141) datiData.f141_s = body.f141; // Access key
96 if (body.f142) datiData.f142_s = body.f142; // Secret key
97 if (body.f143) datiData.f143_s = body.f143; // Region
100 if (body.f300 !== undefined) datiData.f300_E = body.f300;
101 if (body.f301 !== undefined) datiData.f301_E = body.f301;
103 const response = await fieldpineServerApi.apiCall('/DATI', {
106 cookie: authData.apiKey,
110 return NextResponse.json({
116 console.error('[Messaging Config API] Create error:', error);
117 return NextResponse.json(
118 { error: 'Failed to create account' },
124export async function PUT(request: NextRequest) {
126 const authData = await getStoredAuth();
127 if (!authData || !authData.authenticated) {
128 return NextResponse.json(
129 { error: 'Authentication required' },
134 const body = await request.json();
135 console.log('[Messaging Config API] Updating account:', body);
137 // Build DATI packet for updating account
138 const datiData: any = {
139 f8_s: 'messaging.config.edit',
141 f100_s: body.f100, // Account ID (required for edit)
144 // Map all possible fields
145 if (body.f109) datiData.f109_s = body.f109; // Account name
146 if (body.f101) datiData.f101_s = body.f101; // Server
147 if (body.f102) datiData.f102_s = body.f102; // Port
148 if (body.f103) datiData.f103_s = body.f103; // User
149 if (body.f104) datiData.f104_s = body.f104; // Password
150 if (body.f107) datiData.f107_s = body.f107; // Delete control
151 if (body.f111) datiData.f111_s = body.f111; // Auth schemes
152 if (body.f112) datiData.f112_s = body.f112; // From name
153 if (body.f114) datiData.f114_s = body.f114; // Day poll rate
154 if (body.f115) datiData.f115_s = body.f115; // Night poll rate
155 if (body.f118) datiData.f118_s = body.f118; // Reply to
156 if (body.f120 !== undefined) datiData.f120_E = body.f120; // Status flags
157 if (body.f125 !== undefined) datiData.f125_E = body.f125; // SMS flags
158 if (body.f126 !== undefined) datiData.f126_E = body.f126; // Save in database
159 if (body.f128) datiData.f128_E = body.f128; // Well known server
160 if (body.f129) datiData.f129_s = body.f129; // Account headers
161 if (body.f130) datiData.f130_s = body.f130; // Provider/folder
162 if (body.f131) datiData.f131_s = body.f131; // File types
163 if (body.f132) datiData.f132_s = body.f132; // Output folder
164 if (body.f133) datiData.f133_s = body.f133; // API Key
165 if (body.f134) datiData.f134_s = body.f134; // API Secret
166 if (body.f140) datiData.f140_s = body.f140; // Bucket/Country
167 if (body.f141) datiData.f141_s = body.f141; // Access key
168 if (body.f142) datiData.f142_s = body.f142; // Secret key
169 if (body.f143) datiData.f143_s = body.f143; // Region
170 if (body.f160) datiData.f160_s = body.f160; // Max per hour
171 if (body.f180 !== undefined) datiData.f180_E = body.f180; // IMAP flags
172 if (body.f190 !== undefined) datiData.f190_E = body.f190; // Transactional email
173 if (body.f191 !== undefined) datiData.f191_E = body.f191; // Bulk email
174 if (body.f192 !== undefined) datiData.f192_E = body.f192; // Confirm TX
175 if (body.f193 !== undefined) datiData.f193_E = body.f193; // Save message
176 if (body.f280) datiData.f280_s = body.f280; // Network password
177 if (body.f300 !== undefined) datiData.f300_E = body.f300; // Upload transactions
178 if (body.f301 !== undefined) datiData.f301_E = body.f301; // Upload images
179 if (body.f310) datiData.f310_s = body.f310; // Allow list
180 if (body.f311) datiData.f311_s = body.f311; // Block list
182 const response = await fieldpineServerApi.apiCall('/DATI', {
185 cookie: authData.apiKey,
189 return NextResponse.json({
195 console.error('[Messaging Config API] Update error:', error);
196 return NextResponse.json(
197 { error: 'Failed to update account' },