monacousa-portal/server/api/admin/nocodb-config.post.ts

86 lines
2.8 KiB
TypeScript

import type { NocoDBSettings } from '~/utils/types';
export default defineEventHandler(async (event) => {
console.log('[api/admin/nocodb-config.post] =========================');
console.log('[api/admin/nocodb-config.post] POST /api/admin/nocodb-config');
console.log('[api/admin/nocodb-config.post] Request from:', getClientIP(event));
try {
// Check admin authorization
const sessionManager = createSessionManager();
const cookieHeader = getHeader(event, 'cookie');
const session = sessionManager.getSession(cookieHeader);
if (!session?.user) {
throw createError({
statusCode: 401,
statusMessage: 'Authentication required'
});
}
// Check if user is admin
if (session.user.tier !== 'admin') {
throw createError({
statusCode: 403,
statusMessage: 'Admin access required'
});
}
console.log('[api/admin/nocodb-config.post] Admin access confirmed for:', session.user.email);
// Get request body
const body = await readBody(event) as NocoDBSettings;
// Validate required fields
if (!body.url || !body.apiKey || !body.baseId || !body.tableId) {
throw createError({
statusCode: 400,
statusMessage: 'All fields are required: url, apiKey, baseId, tableId'
});
}
// Validate URL format
if (!body.url.startsWith('http://') && !body.url.startsWith('https://')) {
throw createError({
statusCode: 400,
statusMessage: 'URL must start with http:// or https://'
});
}
console.log('[api/admin/nocodb-config.post] Saving NocoDB configuration...');
console.log('[api/admin/nocodb-config.post] URL:', body.url);
console.log('[api/admin/nocodb-config.post] Base ID:', body.baseId);
console.log('[api/admin/nocodb-config.post] Table ID:', body.tableId);
console.log('[api/admin/nocodb-config.post] API Key: [REDACTED]');
// In a real application, you would save these settings to a secure database
// For now, we'll just validate the structure and log success
// TODO: Implement actual persistence (database or secure config store)
// This could be saved to:
// 1. A separate admin_settings table in the database
// 2. Environment variable overrides
// 3. A secure configuration service
// For demonstration, we'll simulate success
console.log('[api/admin/nocodb-config.post] ✅ Configuration saved successfully');
return {
success: true,
message: 'NocoDB configuration saved successfully'
};
} catch (error: any) {
console.error('[api/admin/nocodb-config.post] ❌ Error:', error);
if (error.statusCode) {
throw error; // Re-throw HTTP errors
}
throw createError({
statusCode: 500,
statusMessage: 'Failed to save NocoDB configuration'
});
}
});