refactors, specific day playtime controls
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { bookings, courts } from '@/lib/db/schema';
|
||||
import { bookings, courts, timeSlots } from '@/lib/db/schema';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { getSession } from '@/lib/session';
|
||||
import { logActivity, ACTIONS, ENTITY_TYPES } from '@/lib/activity-logger';
|
||||
@@ -86,6 +86,46 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// CRITICAL: Validate that booking is allowed for this day and time
|
||||
const dayOfWeek = bookingDate.getDay();
|
||||
const availableTimeSlots = await db
|
||||
.select()
|
||||
.from(timeSlots)
|
||||
.where(
|
||||
and(
|
||||
eq(timeSlots.dayOfWeek, dayOfWeek),
|
||||
eq(timeSlots.isActive, true)
|
||||
)
|
||||
);
|
||||
|
||||
// Check if any time slots are configured for this day
|
||||
if (availableTimeSlots.length === 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `No bookings are allowed on ${['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek]}s. The facility is closed on this day.`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if the requested time slot is within any of the allowed time ranges
|
||||
const requestedHour = parseInt(startTime.split(':')[0]);
|
||||
const isTimeSlotValid = availableTimeSlots.some(slot => {
|
||||
const slotStartHour = parseInt(slot.startTime.split(':')[0]);
|
||||
const slotEndHour = parseInt(slot.endTime.split(':')[0]);
|
||||
return requestedHour >= slotStartHour && requestedHour < slotEndHour;
|
||||
});
|
||||
|
||||
if (!isTimeSlotValid) {
|
||||
const allowedRanges = availableTimeSlots.map(slot => `${slot.startTime}-${slot.endTime}`).join(', ');
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Time slot ${startTime} is not available on ${['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek]}s. Available times: ${allowedRanges}`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if slot is already booked
|
||||
const existingBooking = await db
|
||||
.select()
|
||||
|
||||
Reference in New Issue
Block a user