feat(api): add courts and availability endpoints

Add REST API endpoints for court management and availability:
- GET/POST /api/courts for listing and creating courts
- GET/PUT/DELETE /api/courts/[id] for single court operations
- GET /api/courts/[id]/availability for time slot availability
- GET /api/sites for listing organization sites

Features include organization-based filtering, role-based access control,
premium pricing for evening/weekend slots, and booking conflict detection.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ivan
2026-02-01 06:38:13 +00:00
parent fd28bf67d8
commit d3413c727f
4 changed files with 667 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { db } from '@/lib/db';
// GET /api/sites - List sites for user's organization
export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
}
const sites = await db.site.findMany({
where: {
organizationId: session.user.organizationId,
isActive: true,
},
select: {
id: true,
name: true,
slug: true,
address: true,
phone: true,
email: true,
timezone: true,
openTime: true,
closeTime: true,
_count: {
select: {
courts: {
where: {
isActive: true,
},
},
},
},
},
orderBy: {
name: 'asc',
},
});
// Transform to include court count at top level
const transformedSites = sites.map((site) => ({
id: site.id,
name: site.name,
slug: site.slug,
address: site.address,
phone: site.phone,
email: site.email,
timezone: site.timezone,
openTime: site.openTime,
closeTime: site.closeTime,
courtCount: site._count.courts,
}));
return NextResponse.json(transformedSites);
} catch (error) {
console.error('Error fetching sites:', error);
return NextResponse.json(
{ error: 'Failed to fetch sites' },
{ status: 500 }
);
}
}