- PostgreSQL schema: resources + resources_bookings tables with indexes - Server-side client with PG pool + in-memory fallback for dev - API routes: list, get, availability check, book, cancel - Resource store (Zustand) for client-side state - ResourcePicker component: type filter, search, availability dots - Integrated into event-modal: auto-book on save, auto-cancel on delete - Integrated into free-busy-view: resource availability rows
41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { logger } from '@/lib/logger';
|
|
import { getStalwartCredentials } from '@/lib/stalwart/credentials';
|
|
import { bookResource, checkAvailability, getResource } from '@/lib/resources/client';
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
try {
|
|
const creds = await getStalwartCredentials(request);
|
|
if (!creds) {
|
|
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
const { start, end, eventId } = body;
|
|
|
|
if (!start || !end) {
|
|
return NextResponse.json({ error: 'start and end are required' }, { status: 400 });
|
|
}
|
|
|
|
const resource = await getResource(id);
|
|
if (!resource) {
|
|
return NextResponse.json({ error: 'Resource not found' }, { status: 404 });
|
|
}
|
|
|
|
const { available, conflicts } = await checkAvailability(id, start, end);
|
|
if (!available) {
|
|
return NextResponse.json({ error: 'Resource is not available for the requested time', conflicts }, { status: 409 });
|
|
}
|
|
|
|
const booking = await bookResource(id, start, end, creds.username, eventId);
|
|
return NextResponse.json({ booking }, { status: 201 });
|
|
} catch (error) {
|
|
logger.error('Resource booking error', { error: error instanceof Error ? error.message : 'Unknown' });
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|