Add database integration, auth API, admin/user portals, and deploy-db.sh
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user