fix: handle isOpenPlay and map form values to Prisma enums in court API
- Add isOpenPlay field to POST and PUT routes - Accept both hourlyRate and pricePerHour (form vs API naming) - Map lowercase type/status from form to uppercase Prisma enums Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -125,23 +125,34 @@ export async function PUT(
|
|||||||
type,
|
type,
|
||||||
status,
|
status,
|
||||||
pricePerHour,
|
pricePerHour,
|
||||||
|
hourlyRate,
|
||||||
description,
|
description,
|
||||||
features,
|
features,
|
||||||
displayOrder,
|
displayOrder,
|
||||||
isActive,
|
isActive,
|
||||||
|
isOpenPlay,
|
||||||
} = body;
|
} = body;
|
||||||
|
|
||||||
|
const price = pricePerHour ?? hourlyRate;
|
||||||
|
|
||||||
|
// Map lowercase form values to Prisma enum values
|
||||||
|
const typeMap: Record<string, string> = { indoor: 'INDOOR', outdoor: 'OUTDOOR', covered: 'COVERED' };
|
||||||
|
const statusMap: Record<string, string> = { active: 'AVAILABLE', maintenance: 'MAINTENANCE', inactive: 'CLOSED' };
|
||||||
|
const mappedType = type ? (typeMap[type.toLowerCase()] || type) : undefined;
|
||||||
|
const mappedStatus = status ? (statusMap[status.toLowerCase()] || status) : undefined;
|
||||||
|
|
||||||
const court = await db.court.update({
|
const court = await db.court.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
...(name !== undefined && { name }),
|
...(name !== undefined && { name }),
|
||||||
...(type !== undefined && { type }),
|
...(mappedType !== undefined && { type: mappedType }),
|
||||||
...(status !== undefined && { status }),
|
...(mappedStatus !== undefined && { status: mappedStatus }),
|
||||||
...(pricePerHour !== undefined && { pricePerHour }),
|
...(price !== undefined && { pricePerHour: price }),
|
||||||
...(description !== undefined && { description }),
|
...(description !== undefined && { description }),
|
||||||
...(features !== undefined && { features }),
|
...(features !== undefined && { features }),
|
||||||
...(displayOrder !== undefined && { displayOrder }),
|
...(displayOrder !== undefined && { displayOrder }),
|
||||||
...(isActive !== undefined && { isActive }),
|
...(isActive !== undefined && { isActive }),
|
||||||
|
...(isOpenPlay !== undefined && { isOpenPlay }),
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
site: {
|
site: {
|
||||||
|
|||||||
@@ -97,14 +97,24 @@ export async function POST(request: NextRequest) {
|
|||||||
type,
|
type,
|
||||||
status,
|
status,
|
||||||
pricePerHour,
|
pricePerHour,
|
||||||
|
hourlyRate,
|
||||||
description,
|
description,
|
||||||
features,
|
features,
|
||||||
displayOrder,
|
displayOrder,
|
||||||
isActive,
|
isActive,
|
||||||
|
isOpenPlay,
|
||||||
} = body;
|
} = body;
|
||||||
|
|
||||||
|
const price = pricePerHour ?? hourlyRate;
|
||||||
|
|
||||||
|
// Map lowercase form values to Prisma enum values
|
||||||
|
const typeMap: Record<string, string> = { indoor: 'INDOOR', outdoor: 'OUTDOOR', covered: 'COVERED' };
|
||||||
|
const statusMap: Record<string, string> = { active: 'AVAILABLE', maintenance: 'MAINTENANCE', inactive: 'CLOSED' };
|
||||||
|
const mappedType = typeMap[type?.toLowerCase()] || type || 'INDOOR';
|
||||||
|
const mappedStatus = statusMap[status?.toLowerCase()] || status || 'AVAILABLE';
|
||||||
|
|
||||||
// Validate required fields
|
// Validate required fields
|
||||||
if (!siteId || !name || pricePerHour === undefined) {
|
if (!siteId || !name || price === undefined) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Missing required fields: siteId, name, pricePerHour' },
|
{ error: 'Missing required fields: siteId, name, pricePerHour' },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
@@ -138,12 +148,13 @@ export async function POST(request: NextRequest) {
|
|||||||
data: {
|
data: {
|
||||||
siteId,
|
siteId,
|
||||||
name,
|
name,
|
||||||
type: type || 'INDOOR',
|
type: mappedType,
|
||||||
status: status || 'AVAILABLE',
|
status: mappedStatus,
|
||||||
pricePerHour,
|
pricePerHour: price,
|
||||||
description: description || null,
|
description: description || null,
|
||||||
features: features || [],
|
features: features || [],
|
||||||
displayOrder: displayOrder ?? 0,
|
displayOrder: displayOrder ?? 0,
|
||||||
|
isOpenPlay: isOpenPlay ?? false,
|
||||||
isActive: isActive ?? true,
|
isActive: isActive ?? true,
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
|
|||||||
Reference in New Issue
Block a user