initial version of the app

This commit is contained in:
mikicvi
2025-09-21 17:11:02 +01:00
commit c8062cf96b
101 changed files with 23061 additions and 0 deletions
+145
View File
@@ -0,0 +1,145 @@
import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/session';
import { db } from '@/lib/db';
import { bookings } from '@/lib/db/schema';
import { eq, and } from 'drizzle-orm';
import { logActivity, ACTIONS, ENTITY_TYPES } from '@/lib/activity-logger';
export async function PATCH(request: NextRequest, { params }: { params: { id: string } }) {
try {
const session = await getSession();
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { notes } = await request.json();
const bookingId = params.id;
// Check if booking exists and belongs to user
const existingBooking = await db
.select()
.from(bookings)
.where(and(eq(bookings.id, bookingId), eq(bookings.userId, session.userId), eq(bookings.status, 'active')))
.limit(1);
if (existingBooking.length === 0) {
return NextResponse.json({ error: 'Booking not found or access denied' }, { status: 404 });
}
const booking = existingBooking[0];
// Check if booking can be modified (more than 2 hours before start time)
const bookingDateTime = new Date(`${booking.date}T${booking.startTime}`);
const now = new Date();
const timeDiff = bookingDateTime.getTime() - now.getTime();
const hoursDiff = timeDiff / (1000 * 60 * 60);
if (hoursDiff <= 2) {
return NextResponse.json(
{ error: 'Booking can only be modified more than 2 hours before the session' },
{ status: 400 }
);
}
// Update booking notes
await db
.update(bookings)
.set({
notes: notes || null,
updatedAt: new Date(),
})
.where(eq(bookings.id, bookingId));
// Log the activity
await logActivity({
userId: session.userId,
action: ACTIONS.BOOKING_UPDATE,
entityType: ENTITY_TYPES.BOOKING,
entityId: bookingId,
details: {
oldNotes: booking.notes,
newNotes: notes,
date: booking.date,
startTime: booking.startTime,
},
request,
});
return NextResponse.json({
success: true,
message: 'Booking updated successfully',
});
} catch (error) {
console.error('Error updating booking:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
try {
const session = await getSession();
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const bookingId = params.id;
// Check if booking exists and belongs to user
const existingBooking = await db
.select()
.from(bookings)
.where(and(eq(bookings.id, bookingId), eq(bookings.userId, session.userId), eq(bookings.status, 'active')))
.limit(1);
if (existingBooking.length === 0) {
return NextResponse.json({ error: 'Booking not found or access denied' }, { status: 404 });
}
const booking = existingBooking[0];
// Check if booking can be cancelled (more than 2 hours before start time)
const bookingDateTime = new Date(`${booking.date}T${booking.startTime}`);
const now = new Date();
const timeDiff = bookingDateTime.getTime() - now.getTime();
const hoursDiff = timeDiff / (1000 * 60 * 60);
if (hoursDiff <= 2) {
return NextResponse.json(
{ error: 'Booking can only be cancelled more than 2 hours before the session' },
{ status: 400 }
);
}
// Cancel booking (set status to cancelled)
await db
.update(bookings)
.set({
status: 'cancelled',
updatedAt: new Date(),
})
.where(eq(bookings.id, bookingId));
// Log the activity
await logActivity({
userId: session.userId,
action: ACTIONS.BOOKING_CANCEL,
entityType: ENTITY_TYPES.BOOKING,
entityId: bookingId,
details: {
date: booking.date,
startTime: booking.startTime,
endTime: booking.endTime,
courtId: booking.courtId,
},
request,
});
return NextResponse.json({
success: true,
message: 'Booking cancelled successfully',
});
} catch (error) {
console.error('Error cancelling booking:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}