All files / services audit.service.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 3/3
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                            2x 37x 31x           2x 4x 3x           2x 5x           5x 5x 5x 5x 5x 5x 5x 5x    
/**
 * Audit logs service
 */
 
import { apiClient } from '@/lib/api';
 
export interface AuditLogItem {
    id: number;
    account_id: string;
    operation: 'CREATE' | 'UPDATE' | 'DELETE';
    table_name: string;
    record_id: string;
    column_name: string;
    old_value: string | null;
    new_value: string | null;
    user_id: string;
    user_email: string;
    user_name: string;
    es_username: string | null;
    es_reason: string | null;
    es_timestamp: string | null;
    ip_address: string | null;
    user_agent: string | null;
    request_id: string | null;
    occurred_at: string;
    checksum: string | null;
    previous_hash: string | null;
    extra_metadata: Record<string, unknown> | null;
}
 
export interface AuditLogListResponse {
    items: AuditLogItem[];
    total: number;
    skip: number;
    limit: number;
}
 
export interface AuditLogFilters {
    account_id?: string;
    table_name?: string;
    record_id?: string;
    user_id?: string;
    operation?: string;
    from_date?: string;
    to_date?: string;
    skip?: number;
    limit?: number;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
}
 
/**
 * Get audit logs with filtering and pagination
 */
export const getAuditLogs = async (filters: AuditLogFilters): Promise<AuditLogListResponse> => {
    const response = await apiClient.get<AuditLogListResponse>('/audit-logs/', { params: filters });
    return response.data;
};
 
/**
 * Get a single audit log entry
 */
export const getAuditLog = async (id: number): Promise<AuditLogItem> => {
    const response = await apiClient.get<AuditLogItem>(`/audit-logs/${id}`);
    return response.data;
};
 
/**
 * Export audit logs
 */
export const exportAuditLogs = async (format: 'csv' | 'json', filters: AuditLogFilters) => {
    const response = await apiClient.get('/audit-logs/export', {
        params: { ...filters, format },
        responseType: 'blob',
    });
    
    // Create download link
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    const filename = `audit_logs_${new Date().toISOString().split('T')[0]}.${format}`;
    link.setAttribute('download', filename);
    document.body.appendChild(link);
    link.click();
    link.remove();
};