feat: implement admin blocks management feature
- Added AdminBlocksManagement component for managing court blocks. - Implemented functionality to create, edit, and delete blocks. - Integrated fetching of courts and blocks from the API. - Added validation for block creation and editing forms. - Enhanced UI with responsive design for mobile and desktop views. - Created database migration for court_blocks table and updated users table with theme_preference.
This commit is contained in:
+92
-45
@@ -1,7 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { bookings, courts, timeSlots, settings, metrics } from '@/lib/db/schema';
|
||||
import { eq, and, gte, asc } from 'drizzle-orm';
|
||||
import { bookings, courts, timeSlots, settings, metrics, courtBlocks } from '@/lib/db/schema';
|
||||
import { eq, and, gte, asc, or, isNull } from 'drizzle-orm';
|
||||
import { getSession } from '@/lib/session';
|
||||
import { logActivity, ACTIONS, ENTITY_TYPES } from '@/lib/activity-logger';
|
||||
|
||||
@@ -65,6 +65,9 @@ export async function POST(request: NextRequest) {
|
||||
const [hours, minutes] = timeSlot.split(':').map(Number);
|
||||
const endTime = `${String(hours + 1).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
|
||||
|
||||
// Check if user is admin (for bypassing certain restrictions)
|
||||
const isAdmin = session.role === 'admin';
|
||||
|
||||
// Validate booking date is not in the past
|
||||
const bookingDate = new Date(date);
|
||||
const today = new Date();
|
||||
@@ -90,6 +93,45 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// CHECK FOR BLOCKS - applies to everyone including admins
|
||||
// A block prevents any booking on that court/time (admins should remove the block first)
|
||||
const requestedHour = parseInt(startTime.split(':')[0]);
|
||||
const activeBlocks = await db
|
||||
.select()
|
||||
.from(courtBlocks)
|
||||
.where(
|
||||
and(
|
||||
eq(courtBlocks.date, date),
|
||||
or(
|
||||
eq(courtBlocks.courtId, courtId), // Block for this specific court
|
||||
isNull(courtBlocks.courtId) // Block for all courts
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Check if any block covers this time slot
|
||||
const isBlockedSlot = activeBlocks.some((block) => {
|
||||
const blockStartHour = parseInt(block.startTime.split(':')[0]);
|
||||
const blockEndHour = parseInt(block.endTime.split(':')[0]);
|
||||
return requestedHour >= blockStartHour && requestedHour < blockEndHour;
|
||||
});
|
||||
|
||||
if (isBlockedSlot) {
|
||||
const blockingBlock = activeBlocks.find((block) => {
|
||||
const blockStartHour = parseInt(block.startTime.split(':')[0]);
|
||||
const blockEndHour = parseInt(block.endTime.split(':')[0]);
|
||||
return requestedHour >= blockStartHour && requestedHour < blockEndHour;
|
||||
});
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `This slot is blocked: ${
|
||||
blockingBlock?.reason || 'Court unavailable'
|
||||
}. Please choose a different time.`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// CRITICAL: Validate that booking is allowed for this day and time
|
||||
const dayOfWeek = bookingDate.getDay();
|
||||
const availableTimeSlots = await db
|
||||
@@ -97,8 +139,8 @@ export async function POST(request: NextRequest) {
|
||||
.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) {
|
||||
// Check if any time slots are configured for this day (admins can bypass if needed)
|
||||
if (availableTimeSlots.length === 0 && !isAdmin) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `No bookings are allowed on ${
|
||||
@@ -110,54 +152,59 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
// 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;
|
||||
});
|
||||
// Admins can bypass time slot restrictions
|
||||
if (!isAdmin) {
|
||||
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 }
|
||||
);
|
||||
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 booking restrictions per user per hour per day
|
||||
const maxBookingsSetting = await db
|
||||
.select()
|
||||
.from(settings)
|
||||
.where(eq(settings.key, 'max_bookings_per_user_per_hour_per_day'))
|
||||
.limit(1);
|
||||
// Admins bypass this restriction
|
||||
if (!isAdmin) {
|
||||
const maxBookingsSetting = await db
|
||||
.select()
|
||||
.from(settings)
|
||||
.where(eq(settings.key, 'max_bookings_per_user_per_hour_per_day'))
|
||||
.limit(1);
|
||||
|
||||
const maxBookingsPerHour = maxBookingsSetting.length > 0 ? parseInt(maxBookingsSetting[0].value) : 1; // Default to 1 if setting not found
|
||||
const maxBookingsPerHour = maxBookingsSetting.length > 0 ? parseInt(maxBookingsSetting[0].value) : 1;
|
||||
|
||||
// Count user's existing bookings for this hour on this day
|
||||
const userBookingsThisHour = await db
|
||||
.select()
|
||||
.from(bookings)
|
||||
.where(
|
||||
and(
|
||||
eq(bookings.userId, session.userId),
|
||||
eq(bookings.date, date),
|
||||
eq(bookings.startTime, startTime),
|
||||
eq(bookings.status, 'active')
|
||||
)
|
||||
);
|
||||
// Count user's existing bookings for this hour on this day
|
||||
const userBookingsThisHour = await db
|
||||
.select()
|
||||
.from(bookings)
|
||||
.where(
|
||||
and(
|
||||
eq(bookings.userId, session.userId),
|
||||
eq(bookings.date, date),
|
||||
eq(bookings.startTime, startTime),
|
||||
eq(bookings.status, 'active')
|
||||
)
|
||||
);
|
||||
|
||||
if (userBookingsThisHour.length >= maxBookingsPerHour) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `You have reached the maximum limit of ${maxBookingsPerHour} booking(s) per hour. You already have ${userBookingsThisHour.length} booking(s) at ${startTime} on this date.`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
if (userBookingsThisHour.length >= maxBookingsPerHour) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `You have reached the maximum limit of ${maxBookingsPerHour} booking(s) per hour. You already have ${userBookingsThisHour.length} booking(s) at ${startTime} on this date.`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if slot is already booked
|
||||
|
||||
Reference in New Issue
Block a user