- Unify API: lib/api.ts uses /api/v1, inbox uses /api/inbox (rewrites) - Remove localhost refs: openapi, inbox page - Add rewrites: /api/inbox|tmc -> inbox-server, /api/v1 -> FastAPI - Add stub routes: knowledge/insights, recommendations, search, log-error - Transfer from PAPA: prompts (inspection, tmc), scripts, supabase, data/tmc-requests - Fix inbox-server: ORDER BY created_at, package.json - Remove redundant app/api/inbox/files route (rewrites handle it) - knowledge/ in gitignore (large PDFs) Co-authored-by: Cursor <cursoragent@cursor.com>
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
export const dynamic = "force-dynamic";
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { searchAuditLogs, exportAuditLogs, AuditSearchFilters } from '@/lib/audit/audit-service';
|
|
import { handleError } from '@/lib/error-handler';
|
|
import { rateLimit, getRateLimitIdentifier } from '@/lib/rate-limit';
|
|
|
|
/**
|
|
* GET /api/audit - Поиск записей аудита
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// Rate limiting
|
|
const identifier = getRateLimitIdentifier(request);
|
|
const rateLimitResult = rateLimit(identifier);
|
|
if (!rateLimitResult.allowed) {
|
|
return NextResponse.json(
|
|
{ error: 'Слишком много запросов' },
|
|
{ status: 429 }
|
|
);
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
const filters: AuditSearchFilters = {
|
|
userId: searchParams.get('userId') || undefined,
|
|
action: searchParams.get('action') || undefined,
|
|
resourceType: searchParams.get('resourceType') || undefined,
|
|
resourceId: searchParams.get('resourceId') || undefined,
|
|
startDate: searchParams.get('startDate') ? new Date(searchParams.get('startDate')!) : undefined,
|
|
endDate: searchParams.get('endDate') ? new Date(searchParams.get('endDate')!) : undefined,
|
|
ipAddress: searchParams.get('ipAddress') || undefined,
|
|
search: searchParams.get('search') || undefined,
|
|
};
|
|
|
|
const limit = parseInt(searchParams.get('limit') || '100');
|
|
const offset = parseInt(searchParams.get('offset') || '0');
|
|
const format = searchParams.get('format') as 'json' | 'csv' | null;
|
|
|
|
// Если запрошен экспорт
|
|
if (format) {
|
|
const exported = await exportAuditLogs(filters, format);
|
|
const contentType = format === 'json' ? 'application/json' : 'text/csv';
|
|
const filename = `audit_logs_${new Date().toISOString().split('T')[0]}.${format}`;
|
|
|
|
return new NextResponse(exported, {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Content-Disposition': `attachment; filename="${filename}"`,
|
|
},
|
|
});
|
|
}
|
|
|
|
// Обычный поиск
|
|
const result = await searchAuditLogs(filters, limit, offset);
|
|
|
|
return NextResponse.json({
|
|
logs: result.logs,
|
|
total: result.total,
|
|
limit,
|
|
offset,
|
|
});
|
|
} catch (error) {
|
|
return handleError(error, {
|
|
path: '/api/audit',
|
|
method: 'GET',
|
|
});
|
|
}
|
|
}
|