102 lines
2.3 KiB
TypeScript
102 lines
2.3 KiB
TypeScript
import apiClient from './api/client'
|
|
|
|
export interface AuditLog {
|
|
id: string
|
|
userId: string
|
|
action: string
|
|
resourceType: string
|
|
resourceId: string
|
|
changes?: Record<string, any>
|
|
ipAddress: string
|
|
userAgent: string
|
|
timestamp: string
|
|
}
|
|
|
|
export interface GetAuditLogsParams {
|
|
page?: number
|
|
limit?: number
|
|
userId?: string
|
|
action?: string
|
|
resourceType?: string
|
|
resourceId?: string
|
|
startDate?: string
|
|
endDate?: string
|
|
search?: string
|
|
}
|
|
|
|
export interface AuditStats {
|
|
totalActions: number
|
|
uniqueUsers: number
|
|
topActions: Array<{ action: string; count: number }>
|
|
topUsers: Array<{ userId: string; count: number }>
|
|
}
|
|
|
|
class AuditService {
|
|
/**
|
|
* Get audit logs with pagination and filters
|
|
*/
|
|
async getAuditLogs(params?: GetAuditLogsParams): Promise<{
|
|
data: AuditLog[]
|
|
total: number
|
|
page: number
|
|
limit: number
|
|
totalPages: number
|
|
}> {
|
|
const response = await apiClient.get('/admin/audit/logs', { params })
|
|
return response.data
|
|
}
|
|
|
|
/**
|
|
* Get audit log by ID
|
|
*/
|
|
async getAuditLog(id: string): Promise<AuditLog> {
|
|
const response = await apiClient.get<AuditLog>(`/admin/audit/logs/${id}`)
|
|
return response.data
|
|
}
|
|
|
|
/**
|
|
* Get user audit activity
|
|
*/
|
|
async getUserAuditActivity(userId: string, days: number = 30): Promise<AuditLog[]> {
|
|
const response = await apiClient.get<AuditLog[]>(`/admin/audit/users/${userId}`, {
|
|
params: { days },
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
/**
|
|
* Get resource history
|
|
*/
|
|
async getResourceHistory(type: string, id: string): Promise<AuditLog[]> {
|
|
const response = await apiClient.get<AuditLog[]>(`/admin/audit/resource/${type}/${id}`)
|
|
return response.data
|
|
}
|
|
|
|
/**
|
|
* Get audit statistics
|
|
*/
|
|
async getAuditStats(startDate?: string, endDate?: string): Promise<AuditStats> {
|
|
const response = await apiClient.get<AuditStats>('/admin/audit/stats', {
|
|
params: { startDate, endDate },
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
/**
|
|
* Export audit logs
|
|
*/
|
|
async exportAuditLogs(params?: {
|
|
format?: 'csv' | 'json'
|
|
startDate?: string
|
|
endDate?: string
|
|
}): Promise<Blob> {
|
|
const response = await apiClient.get('/admin/audit/export', {
|
|
params,
|
|
responseType: 'blob',
|
|
})
|
|
return response.data
|
|
}
|
|
}
|
|
|
|
export const auditService = new AuditService()
|