-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathroute.ts
More file actions
71 lines (61 loc) · 2.46 KB
/
Copy pathroute.ts
File metadata and controls
71 lines (61 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { createLogger } from '@sim/logger'
import { NextResponse } from 'next/server'
import { getSession } from '@/lib/auth'
import { validateEnterpriseAuditAccess } from '@/app/api/v1/audit-logs/auth'
import { formatAuditLogEntry } from '@/app/api/v1/audit-logs/format'
import {
buildFilterConditions,
buildOrgScopeCondition,
queryAuditLogs,
} from '@/app/api/v1/audit-logs/query'
const logger = createLogger('AuditLogsAPI')
export const dynamic = 'force-dynamic'
export async function GET(request: Request) {
try {
const session = await getSession()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const authResult = await validateEnterpriseAuditAccess(session.user.id)
if (!authResult.success) {
return authResult.response
}
const { orgMemberIds } = authResult.context
const { searchParams } = new URL(request.url)
const search = searchParams.get('search')?.trim() || undefined
const startDate = searchParams.get('startDate') || undefined
const endDate = searchParams.get('endDate') || undefined
const includeDeparted = searchParams.get('includeDeparted') === 'true'
const limit = Math.min(Math.max(Number(searchParams.get('limit')) || 50, 1), 100)
const cursor = searchParams.get('cursor') || undefined
if (startDate && Number.isNaN(Date.parse(startDate))) {
return NextResponse.json({ error: 'Invalid startDate format' }, { status: 400 })
}
if (endDate && Number.isNaN(Date.parse(endDate))) {
return NextResponse.json({ error: 'Invalid endDate format' }, { status: 400 })
}
const scopeCondition = await buildOrgScopeCondition(orgMemberIds, includeDeparted)
const filterConditions = buildFilterConditions({
action: searchParams.get('action') || undefined,
resourceType: searchParams.get('resourceType') || undefined,
actorId: searchParams.get('actorId') || undefined,
search,
startDate,
endDate,
})
const { data, nextCursor } = await queryAuditLogs(
[scopeCondition, ...filterConditions],
limit,
cursor
)
return NextResponse.json({
success: true,
data: data.map(formatAuditLogEntry),
nextCursor,
})
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error'
logger.error('Audit logs fetch error', { error: message })
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}