initial version of the app
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
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, and, desc, gte } from 'drizzle-orm';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const session = await verifySession();
|
||||
if (!session.isAuth || !session.userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const today = new Date();
|
||||
const todayStr = today.toISOString().split('T')[0];
|
||||
|
||||
// Get recent bookings for the current user with court and user 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))
|
||||
.where(
|
||||
and(eq(bookings.userId, session.userId), eq(bookings.status, 'active'), gte(bookings.date, todayStr))
|
||||
)
|
||||
.orderBy(desc(bookings.createdAt))
|
||||
.limit(5);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
bookings: recentBookings,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching recent bookings:', error);
|
||||
return NextResponse.json({ error: 'Failed to fetch recent bookings' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user