import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/db"; import { requireActor } from "@/lib/agent-auth"; export async function GET(request: NextRequest) { try { const auth = await requireActor(request, { adminOnly: true, recursoTipo: "audit_log" }); if (!auth.ok) return auth.response; const sp = request.nextUrl.searchParams; const accion = sp.get("accion")?.trim(); const actorNombre = sp.get("actorNombre")?.trim(); const recursoId = sp.get("recursoId")?.trim(); const page = Math.max(1, parseInt(sp.get("page") || "1", 10) || 1); const limit = Math.min(200, Math.max(1, parseInt(sp.get("limit") || "50", 10) || 50)); const where = { ...(accion && { accion: { contains: accion, mode: "insensitive" as const } }), ...(actorNombre && { actorNombre: { contains: actorNombre, mode: "insensitive" as const } }), ...(recursoId && { recursoId }), }; const [total, data] = await Promise.all([ prisma.auditLog.count({ where }), prisma.auditLog.findMany({ where, orderBy: { createdAt: "desc" }, skip: (page - 1) * limit, take: limit, }), ]); return NextResponse.json({ data, meta: { total, page, limit, pages: Math.ceil(total / limit), }, }); } catch (error: unknown) { const msg = error instanceof Error ? error.message : "Error interno"; return NextResponse.json({ error: msg }, { status: 500 }); } }