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>
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
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 }
|
|
);
|
|
}
|
|
}
|