2026-11-21 22:15:32 +06:00
|
|
|
import { listAdminAuditLogsWithStatusAsync } from "@/core/auth/auditLog";
|
2026-11-20 18:48:54 +06:00
|
|
|
import { requireAdminApiSession } from "@/lib/adminAuth";
|
|
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
|
|
|
|
|
export async function GET(req: NextRequest) {
|
|
|
|
|
const auth = await requireAdminApiSession(req, { minRole: "viewer" });
|
|
|
|
|
if (!auth.valid) {
|
|
|
|
|
return auth.response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { searchParams } = new URL(req.url);
|
|
|
|
|
const rawLimit = Number(searchParams.get("limit") || "100");
|
|
|
|
|
const limit = Number.isFinite(rawLimit)
|
|
|
|
|
? Math.min(Math.max(Math.floor(rawLimit), 1), 500)
|
|
|
|
|
: 100;
|
|
|
|
|
const action = searchParams.get("action") || undefined;
|
|
|
|
|
const actorEmail =
|
|
|
|
|
searchParams.get("actor") ||
|
|
|
|
|
searchParams.get("actorEmail") ||
|
|
|
|
|
undefined;
|
|
|
|
|
const search =
|
|
|
|
|
searchParams.get("q") || searchParams.get("search") || undefined;
|
|
|
|
|
|
2026-11-21 22:15:32 +06:00
|
|
|
const result = await listAdminAuditLogsWithStatusAsync({
|
2026-11-20 18:48:54 +06:00
|
|
|
limit,
|
|
|
|
|
action,
|
|
|
|
|
actorEmail,
|
|
|
|
|
search,
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-21 22:15:32 +06:00
|
|
|
const response = NextResponse.json({
|
|
|
|
|
logs: result.logs,
|
|
|
|
|
total: result.logs.length,
|
|
|
|
|
storageMode: result.storageMode,
|
|
|
|
|
degraded: result.degraded,
|
|
|
|
|
...(result.code ? { code: result.code } : {}),
|
|
|
|
|
...(result.warning ? { warning: result.warning } : {}),
|
2026-11-20 18:48:54 +06:00
|
|
|
});
|
2026-11-21 22:15:32 +06:00
|
|
|
if (result.degraded) {
|
|
|
|
|
response.headers.set("X-DataStack-Audit-Degraded", "1");
|
|
|
|
|
}
|
|
|
|
|
return response;
|
2026-11-20 18:48:54 +06:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const message =
|
|
|
|
|
err instanceof Error ? err.message : "Unable to fetch audit logs.";
|
|
|
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|