Harden machine deletion via Convex API
This commit is contained in:
parent
64e4e02a9a
commit
4d8b9a0e39
1 changed files with 16 additions and 7 deletions
|
|
@ -5,6 +5,7 @@ import { ConvexHttpClient } from "convex/browser"
|
||||||
import { assertAuthenticatedSession } from "@/lib/auth-server"
|
import { assertAuthenticatedSession } from "@/lib/auth-server"
|
||||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||||
import { api } from "@/convex/_generated/api"
|
import { api } from "@/convex/_generated/api"
|
||||||
|
import type { Id } from "@/convex/_generated/dataModel"
|
||||||
import { prisma } from "@/lib/prisma"
|
import { prisma } from "@/lib/prisma"
|
||||||
|
|
||||||
export const runtime = "nodejs"
|
export const runtime = "nodejs"
|
||||||
|
|
@ -42,16 +43,24 @@ export async function POST(request: Request) {
|
||||||
role: session.user.role.toUpperCase(),
|
role: session.user.role.toUpperCase(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const actorId = ensured?._id
|
const actorId = ensured?._id as Id<"users"> | undefined
|
||||||
if (!actorId) {
|
if (!actorId) {
|
||||||
return NextResponse.json({ error: "Falha ao obter ID do usuário no Convex" }, { status: 500 })
|
return NextResponse.json({ error: "Falha ao identificar o administrador" }, { status: 500 })
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = convex as unknown as { mutation: (name: string, args: unknown) => Promise<unknown> }
|
try {
|
||||||
await client.mutation("machines:remove", {
|
await convex.mutation(api.machines.remove, {
|
||||||
machineId: parsed.data.machineId,
|
machineId: parsed.data.machineId as Id<"machines">,
|
||||||
actorId,
|
actorId,
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : ""
|
||||||
|
if (message.includes("Máquina não encontrada")) {
|
||||||
|
return NextResponse.json({ error: "Máquina não encontrada" }, { status: 404 })
|
||||||
|
}
|
||||||
|
console.error("[machines.delete] Convex failure", error)
|
||||||
|
return NextResponse.json({ error: "Falha ao remover máquina no Convex" }, { status: 500 })
|
||||||
|
}
|
||||||
|
|
||||||
const machineEmail = `machine-${parsed.data.machineId}@machines.local`
|
const machineEmail = `machine-${parsed.data.machineId}@machines.local`
|
||||||
await prisma.authUser.deleteMany({ where: { email: machineEmail } })
|
await prisma.authUser.deleteMany({ where: { email: machineEmail } })
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue