import { NextResponse } from 'next/server'; import { requireRole } from '@/lib/auth'; import { db } from '@/lib/db'; export const dynamic = 'force-dynamic'; export async function GET() { try { const session = await requireRole('user'); const userId = session.id; const { rows: reservations } = await db.query( 'SELECT id, date, time, guests, table_name FROM reservations WHERE user_id = $1 ORDER BY date, time', [userId] ); const { rows: coupons } = await db.query( 'SELECT id, code, discount FROM coupons WHERE user_id = $1 ORDER BY created_at', [userId] ); const { rows: offers } = await db.query( 'SELECT id, title, description FROM offers WHERE active = TRUE ORDER BY created_at' ); const { rows: benefits } = await db.query( 'SELECT id, text FROM benefits ORDER BY id' ); const { rows: configRows } = await db.query( "SELECT value FROM config WHERE key = 'wifi_password'" ); const wifiPassword = configRows[0]?.value || ''; // Get user's trips (future and current) const { rows: trips } = await db.query( `SELECT t.id, t.room_id, t.check_in, t.check_out, t.notes, t.status, r.name as room_name FROM trips t LEFT JOIN rooms r ON t.room_id = r.id WHERE t.user_id = $1 AND t.check_out >= CURRENT_DATE ORDER BY t.check_in ASC`, [userId] ); // Get weather let weather = undefined; try { const weatherRes = await fetch('http://localhost:3000/api/weather'); if (weatherRes.ok) { const weatherData = await weatherRes.json(); weather = weatherData.data; } } catch { /* ignore */ } return NextResponse.json({ reservations, coupons, offers, benefits: benefits.map((b: { text: string }) => b.text), wifiPassword, trips, weather, }); } catch (error) { if ((error as Error).message === 'Unauthorized') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } console.error('User portal error:', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); } }