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:
BIN
prisma/dev.db
BIN
prisma/dev.db
Binary file not shown.
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user