import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/lib/auth/authOptions'; import { query } from '@/lib/db-postgres'; export interface PreviewApp { name: string; url: string | null; status: 'live' | 'deploying' | 'failed' | 'unknown'; } export async function GET( _req: Request, { params }: { params: Promise<{ projectId: string }> } ) { const { projectId } = await params; const session = await getServerSession(authOptions); if (!session?.user?.email) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const rows = await query<{ data: Record }>( `SELECT p.data FROM fs_projects p JOIN fs_users u ON u.id = p.user_id WHERE p.id = $1 AND u.data->>'email' = $2 LIMIT 1`, [projectId, session.user.email] ); if (rows.length === 0) { return NextResponse.json({ error: 'Project not found' }, { status: 404 }); } const data = rows[0].data ?? {}; // Gather apps with their deployment URLs const storedApps = (data.apps as Array<{ name: string; fqdn?: string; coolifyServiceUuid?: string }>) ?? []; const lastDeployment = (data as any).contextSnapshot?.lastDeployment ?? null; const apps: PreviewApp[] = storedApps.map(app => ({ name: app.name, url: app.fqdn ? (app.fqdn.startsWith('http') ? app.fqdn : `https://${app.fqdn}`) : null, status: app.fqdn ? 'live' : 'unknown', })); // If no stored apps but we have a last deployment URL, surface it if (apps.length === 0 && lastDeployment?.url) { apps.push({ name: 'app', url: lastDeployment.url.startsWith('http') ? lastDeployment.url : `https://${lastDeployment.url}`, status: lastDeployment.status === 'finished' ? 'live' : lastDeployment.status === 'in_progress' ? 'deploying' : 'unknown', }); } // Also expose the raw last deployment for the panel header return NextResponse.json({ apps, lastDeployment }); }