theming, date, time localisation, additional features, seeding initial cleanup

This commit is contained in:
mikicvi
2025-09-26 21:12:59 +01:00
parent b89d91ade2
commit 22c462c61c
43 changed files with 2647 additions and 550 deletions
+4 -4
View File
@@ -5,14 +5,14 @@ import { timeSlots } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
import { logActivity, ACTIONS, ENTITY_TYPES } from '@/lib/activity-logger';
export async function PUT(request: NextRequest, { params }: { params: { id: string } }) {
export async function PUT(request: NextRequest, context: { params: Promise<{ id: string }> }) {
try {
const session = await getSession();
if (!session || session.role !== 'admin') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = params;
const { id } = await context.params;
const { dayOfWeek, startTime, endTime, isActive } = await request.json();
// Check if time slot exists
@@ -68,14 +68,14 @@ export async function PUT(request: NextRequest, { params }: { params: { id: stri
}
}
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
export async function DELETE(request: NextRequest, context: { params: Promise<{ id: string }> }) {
try {
const session = await getSession();
if (!session || session.role !== 'admin') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = params;
const { id } = await context.params;
// Check if time slot exists
const existingTimeSlot = await db.select().from(timeSlots).where(eq(timeSlots.id, id)).limit(1);
+53 -7
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/session';
import { db } from '@/lib/db';
import { bookings } from '@/lib/db/schema';
import { bookings, settings } from '@/lib/db/schema';
import { eq, and } from 'drizzle-orm';
import { logActivity, ACTIONS, ENTITY_TYPES } from '@/lib/activity-logger';
@@ -28,15 +28,38 @@ export async function PATCH(request: NextRequest, { params }: { params: { id: st
const booking = existingBooking[0];
// Check if booking can be modified (more than 2 hours before start time)
// Check if booking modifications are allowed
const modificationSetting = await db
.select()
.from(settings)
.where(eq(settings.key, 'allow_booking_modifications'))
.limit(1);
if (modificationSetting.length > 0 && modificationSetting[0].value !== 'true') {
return NextResponse.json(
{ error: 'Booking modifications are currently disabled by administrator' },
{ status: 400 }
);
}
// Get the time restriction setting
const timeSetting = await db
.select()
.from(settings)
.where(eq(settings.key, 'booking_modification_hours_before'))
.limit(1);
const requiredHours = timeSetting.length > 0 ? parseFloat(timeSetting[0].value) : 2;
// Check if booking can be modified (more than required 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) {
if (hoursDiff <= requiredHours) {
return NextResponse.json(
{ error: 'Booking can only be modified more than 2 hours before the session' },
{ error: `Booking can only be modified more than ${requiredHours} hours before the session` },
{ status: 400 }
);
}
@@ -97,15 +120,38 @@ export async function DELETE(request: NextRequest, { params }: { params: { id: s
const booking = existingBooking[0];
// Check if booking can be cancelled (more than 2 hours before start time)
// Check if booking modifications are allowed
const modificationSetting = await db
.select()
.from(settings)
.where(eq(settings.key, 'allow_booking_modifications'))
.limit(1);
if (modificationSetting.length > 0 && modificationSetting[0].value !== 'true') {
return NextResponse.json(
{ error: 'Booking modifications are currently disabled by administrator' },
{ status: 400 }
);
}
// Get the time restriction setting
const timeSetting = await db
.select()
.from(settings)
.where(eq(settings.key, 'booking_modification_hours_before'))
.limit(1);
const requiredHours = timeSetting.length > 0 ? parseFloat(timeSetting[0].value) : 2;
// Check if booking can be cancelled (more than required 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) {
if (hoursDiff <= requiredHours) {
return NextResponse.json(
{ error: 'Booking can only be cancelled more than 2 hours before the session' },
{ error: `Booking can only be cancelled more than ${requiredHours} hours before the session` },
{ status: 400 }
);
}
+1
View File
@@ -20,6 +20,7 @@ export async function GET(request: NextRequest) {
name: users.name,
surname: users.surname,
role: users.role,
themePreference: users.themePreference,
createdAt: users.createdAt,
})
.from(users)
+64
View File
@@ -0,0 +1,64 @@
import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/session';
import { db } from '@/lib/db';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
export async function GET(request: NextRequest) {
try {
const session = await getSession();
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const user = await db
.select({
themePreference: users.themePreference,
})
.from(users)
.where(eq(users.id, session.userId))
.limit(1);
if (user.length === 0) {
return NextResponse.json({ error: 'User not found' }, { status: 404 });
}
return NextResponse.json({
themePreference: user[0].themePreference,
});
} catch (error) {
console.error('Error fetching theme preference:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
export async function PUT(request: NextRequest) {
try {
const session = await getSession();
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { themePreference } = await request.json();
if (!themePreference || !['light', 'dark', 'system'].includes(themePreference)) {
return NextResponse.json({ error: 'Invalid theme preference' }, { status: 400 });
}
await db
.update(users)
.set({
themePreference,
updatedAt: new Date(),
})
.where(eq(users.id, session.userId));
return NextResponse.json({
message: 'Theme preference updated successfully',
themePreference,
});
} catch (error) {
console.error('Error updating theme preference:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}