From a9caf36b01840118e10d5ae52ace447649efe228 Mon Sep 17 00:00:00 2001 From: Esdras Renan Date: Thu, 16 Oct 2025 17:40:39 -0300 Subject: [PATCH] =?UTF-8?q?admin/companies:=20evitar=20500=20ao=20excluir?= =?UTF-8?q?=20=E2=80=94=20pr=C3=A9-checar=20v=C3=ADnculos=20(users/tickets?= =?UTF-8?q?)=20e=20retornar=20409=20com=20detalhes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/admin/companies/[id]/route.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/app/api/admin/companies/[id]/route.ts b/src/app/api/admin/companies/[id]/route.ts index 7357065..0f5653f 100644 --- a/src/app/api/admin/companies/[id]/route.ts +++ b/src/app/api/admin/companies/[id]/route.ts @@ -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) {