50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { requireRole } from '@/lib/auth';
|
|
import { db } from '@/lib/db';
|
|
|
|
export const dynamic = 'force-dynamic'; // Prevents static generation
|
|
|
|
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 || '';
|
|
|
|
return NextResponse.json({
|
|
reservations,
|
|
coupons,
|
|
offers,
|
|
benefits: benefits.map((b: { text: string }) => b.text),
|
|
wifiPassword,
|
|
});
|
|
} 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 });
|
|
}
|
|
}
|