|
| 1 | +/** |
| 2 | + * GET /api/v1/admin/audit-logs |
| 3 | + * |
| 4 | + * List all audit logs with pagination and filtering. |
| 5 | + * |
| 6 | + * Query Parameters: |
| 7 | + * - limit: number (default: 50, max: 250) |
| 8 | + * - offset: number (default: 0) |
| 9 | + * - action: string (optional) - Filter by action (e.g., "workflow.created") |
| 10 | + * - resourceType: string (optional) - Filter by resource type (e.g., "workflow") |
| 11 | + * - resourceId: string (optional) - Filter by resource ID |
| 12 | + * - workspaceId: string (optional) - Filter by workspace ID |
| 13 | + * - actorId: string (optional) - Filter by actor user ID |
| 14 | + * - actorEmail: string (optional) - Filter by actor email |
| 15 | + * - startDate: string (optional) - ISO 8601 date, filter createdAt >= startDate |
| 16 | + * - endDate: string (optional) - ISO 8601 date, filter createdAt <= endDate |
| 17 | + * |
| 18 | + * Response: AdminListResponse<AdminAuditLog> |
| 19 | + */ |
| 20 | + |
| 21 | +import { db } from '@sim/db' |
| 22 | +import { auditLog } from '@sim/db/schema' |
| 23 | +import { createLogger } from '@sim/logger' |
| 24 | +import { and, count, desc, eq, gte, lte, type SQL } from 'drizzle-orm' |
| 25 | +import { withAdminAuth } from '@/app/api/v1/admin/middleware' |
| 26 | +import { |
| 27 | + badRequestResponse, |
| 28 | + internalErrorResponse, |
| 29 | + listResponse, |
| 30 | +} from '@/app/api/v1/admin/responses' |
| 31 | +import { |
| 32 | + type AdminAuditLog, |
| 33 | + createPaginationMeta, |
| 34 | + parsePaginationParams, |
| 35 | + toAdminAuditLog, |
| 36 | +} from '@/app/api/v1/admin/types' |
| 37 | + |
| 38 | +const logger = createLogger('AdminAuditLogsAPI') |
| 39 | + |
| 40 | +export const GET = withAdminAuth(async (request) => { |
| 41 | + const url = new URL(request.url) |
| 42 | + const { limit, offset } = parsePaginationParams(url) |
| 43 | + |
| 44 | + const actionFilter = url.searchParams.get('action') |
| 45 | + const resourceTypeFilter = url.searchParams.get('resourceType') |
| 46 | + const resourceIdFilter = url.searchParams.get('resourceId') |
| 47 | + const workspaceIdFilter = url.searchParams.get('workspaceId') |
| 48 | + const actorIdFilter = url.searchParams.get('actorId') |
| 49 | + const actorEmailFilter = url.searchParams.get('actorEmail') |
| 50 | + const startDateFilter = url.searchParams.get('startDate') |
| 51 | + const endDateFilter = url.searchParams.get('endDate') |
| 52 | + |
| 53 | + if (startDateFilter && Number.isNaN(Date.parse(startDateFilter))) { |
| 54 | + return badRequestResponse('Invalid startDate format. Use ISO 8601.') |
| 55 | + } |
| 56 | + if (endDateFilter && Number.isNaN(Date.parse(endDateFilter))) { |
| 57 | + return badRequestResponse('Invalid endDate format. Use ISO 8601.') |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + const conditions: SQL<unknown>[] = [] |
| 62 | + |
| 63 | + if (actionFilter) conditions.push(eq(auditLog.action, actionFilter)) |
| 64 | + if (resourceTypeFilter) conditions.push(eq(auditLog.resourceType, resourceTypeFilter)) |
| 65 | + if (resourceIdFilter) conditions.push(eq(auditLog.resourceId, resourceIdFilter)) |
| 66 | + if (workspaceIdFilter) conditions.push(eq(auditLog.workspaceId, workspaceIdFilter)) |
| 67 | + if (actorIdFilter) conditions.push(eq(auditLog.actorId, actorIdFilter)) |
| 68 | + if (actorEmailFilter) conditions.push(eq(auditLog.actorEmail, actorEmailFilter)) |
| 69 | + if (startDateFilter) conditions.push(gte(auditLog.createdAt, new Date(startDateFilter))) |
| 70 | + if (endDateFilter) conditions.push(lte(auditLog.createdAt, new Date(endDateFilter))) |
| 71 | + |
| 72 | + const whereClause = conditions.length > 0 ? and(...conditions) : undefined |
| 73 | + |
| 74 | + const [countResult, logs] = await Promise.all([ |
| 75 | + db.select({ total: count() }).from(auditLog).where(whereClause), |
| 76 | + db |
| 77 | + .select() |
| 78 | + .from(auditLog) |
| 79 | + .where(whereClause) |
| 80 | + .orderBy(desc(auditLog.createdAt)) |
| 81 | + .limit(limit) |
| 82 | + .offset(offset), |
| 83 | + ]) |
| 84 | + |
| 85 | + const total = countResult[0].total |
| 86 | + const data: AdminAuditLog[] = logs.map(toAdminAuditLog) |
| 87 | + const pagination = createPaginationMeta(total, limit, offset) |
| 88 | + |
| 89 | + logger.info(`Admin API: Listed ${data.length} audit logs (total: ${total})`) |
| 90 | + |
| 91 | + return listResponse(data, pagination) |
| 92 | + } catch (error) { |
| 93 | + logger.error('Admin API: Failed to list audit logs', { error }) |
| 94 | + return internalErrorResponse('Failed to list audit logs') |
| 95 | + } |
| 96 | +}) |
0 commit comments