refactor: improve branch update logic with explicit field selection and robust hallCount parsing, and enhance error handling in branch API endpoints

This commit is contained in:
kertenkerem
2026-01-08 02:09:34 +03:00
parent dbca26f3ad
commit 255ec6fd25
3 changed files with 11 additions and 6 deletions

Binary file not shown.

View File

@@ -23,15 +23,20 @@ export async function PUT(
try {
const { id } = await params;
const json = await request.json();
// Explicitly selecting fields to update to avoid spreading unwanted fields (like id)
const branch = await prisma.branch.update({
where: { id },
data: {
...json,
hallCount: json.hallCount ? parseInt(json.hallCount) : undefined
name: json.name,
address: json.address,
phone: json.phone,
hallCount: (json.hallCount !== undefined && json.hallCount !== '') ? parseInt(json.hallCount.toString()) : undefined
},
});
return NextResponse.json(branch);
} catch (error) {
return NextResponse.json({ error: 'Failed' }, { status: 500 });
} catch (error: any) {
console.error('Branch update error:', error);
return NextResponse.json({ error: 'Failed to update branch', details: error.message }, { status: 500 });
}
}

View File

@@ -21,7 +21,7 @@ export async function POST(request: Request) {
}
});
return NextResponse.json(branch);
} catch (error) {
return NextResponse.json({ error: 'Failed to create branch' }, { status: 500 });
} catch (error: any) {
return NextResponse.json({ error: 'Failed to create branch', details: error.message }, { status: 500 });
}
}