admin/companies: evitar 500 ao excluir — pré-checar vínculos (users/tickets) e retornar 409 com detalhes
This commit is contained in:
parent
f60a48e7b3
commit
a9caf36b01
1 changed files with 15 additions and 0 deletions
|
|
@ -77,6 +77,21 @@ export async function DELETE(_: Request, { params }: { params: Promise<{ id: str
|
|||
}
|
||||
|
||||
try {
|
||||
// Pré‑checagem para evitar 500 por FK: conta vínculos antes
|
||||
const [usersCount, ticketsCount] = await Promise.all([
|
||||
prisma.user.count({ where: { companyId: company.id } }),
|
||||
prisma.ticket.count({ where: { companyId: company.id } }),
|
||||
])
|
||||
if (usersCount > 0 || ticketsCount > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Não é possível remover esta empresa pois existem registros vinculados.",
|
||||
details: { users: usersCount, tickets: ticketsCount },
|
||||
},
|
||||
{ status: 409 }
|
||||
)
|
||||
}
|
||||
|
||||
await prisma.company.delete({ where: { id: company.id } })
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue