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 {
|
try {
|
||||||
const { id } = await params;
|
const { id } = await params;
|
||||||
const json = await request.json();
|
const json = await request.json();
|
||||||
|
|
||||||
|
// Explicitly selecting fields to update to avoid spreading unwanted fields (like id)
|
||||||
const branch = await prisma.branch.update({
|
const branch = await prisma.branch.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
...json,
|
name: json.name,
|
||||||
hallCount: json.hallCount ? parseInt(json.hallCount) : undefined
|
address: json.address,
|
||||||
|
phone: json.phone,
|
||||||
|
hallCount: (json.hallCount !== undefined && json.hallCount !== '') ? parseInt(json.hallCount.toString()) : undefined
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return NextResponse.json(branch);
|
return NextResponse.json(branch);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
return NextResponse.json({ error: 'Failed' }, { status: 500 });
|
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);
|
return NextResponse.json(branch);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
return NextResponse.json({ error: 'Failed to create branch' }, { status: 500 });
|
return NextResponse.json({ error: 'Failed to create branch', details: error.message }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user