53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { verifySession } from '@/lib/session';
|
|
import { db } from '@/lib/db';
|
|
import { bookings, users, courts } from '@/lib/db/schema';
|
|
import { eq, desc, gte } from 'drizzle-orm';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await verifySession();
|
|
if (!session.isAuth || session.role !== 'admin') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const limit = parseInt(searchParams.get('limit') || '10');
|
|
|
|
// Get recent bookings with user and court details
|
|
const recentBookings = await db
|
|
.select({
|
|
id: bookings.id,
|
|
date: bookings.date,
|
|
startTime: bookings.startTime,
|
|
endTime: bookings.endTime,
|
|
status: bookings.status,
|
|
notes: bookings.notes,
|
|
createdAt: bookings.createdAt,
|
|
court: {
|
|
id: courts.id,
|
|
name: courts.name,
|
|
},
|
|
user: {
|
|
id: users.id,
|
|
name: users.name,
|
|
surname: users.surname,
|
|
email: users.email,
|
|
},
|
|
})
|
|
.from(bookings)
|
|
.innerJoin(courts, eq(bookings.courtId, courts.id))
|
|
.innerJoin(users, eq(bookings.userId, users.id))
|
|
.orderBy(desc(bookings.createdAt))
|
|
.limit(limit);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
bookings: recentBookings,
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching admin recent bookings:', error);
|
|
return NextResponse.json({ error: 'Failed to fetch recent bookings' }, { status: 500 });
|
|
}
|
|
}
|