import { NextResponse } from 'next/server'; export async function GET() { try { // Check if we have the required environment variables const token = process.env.ROCKET_CHAT_TOKEN; const userId = process.env.ROCKET_CHAT_USER_ID; const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0]; if (!token || !userId) { return NextResponse.json({ error: 'Missing Rocket.Chat admin credentials', hasToken: !!token, hasUserId: !!userId }, { status: 500 }); } if (!baseUrl) { return NextResponse.json({ error: 'Missing Rocket.Chat base URL', iframeUrl: process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL }, { status: 500 }); } // Test a simple API call to verify credentials const adminHeaders = { 'X-Auth-Token': token, 'X-User-Id': userId, 'Content-Type': 'application/json' }; // Get server info (public endpoint that still requires admin auth) const infoResponse = await fetch(`${baseUrl}/api/v1/info`, { method: 'GET', headers: adminHeaders }); if (!infoResponse.ok) { return NextResponse.json({ error: 'Failed to connect to Rocket.Chat API', status: infoResponse.status, statusText: infoResponse.statusText, baseUrl }, { status: 500 }); } const infoData = await infoResponse.json(); // Try to list users (needs admin permissions) const usersResponse = await fetch(`${baseUrl}/api/v1/users.list?count=5`, { method: 'GET', headers: adminHeaders }); const usersResult = await usersResponse.json(); return NextResponse.json({ success: true, serverInfo: { version: infoData.version, serverRunning: infoData.success }, usersCount: usersResult.users?.length || 0, baseUrl }); } catch (error) { return NextResponse.json({ error: 'Error testing Rocket.Chat connection', message: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 }); } }