koprogo_api/application/ports/
audit_log_repository.rs

1use crate::application::dto::PageRequest;
2use crate::infrastructure::audit::{AuditEventType, AuditLogEntry};
3use async_trait::async_trait;
4use chrono::{DateTime, Utc};
5use uuid::Uuid;
6
7/// Filters for querying audit logs
8#[derive(Debug, Clone, Default)]
9pub struct AuditLogFilters {
10    pub user_id: Option<Uuid>,
11    pub organization_id: Option<Uuid>,
12    pub event_type: Option<AuditEventType>,
13    pub success: Option<bool>,
14    pub start_date: Option<DateTime<Utc>>,
15    pub end_date: Option<DateTime<Utc>>,
16    pub resource_type: Option<String>,
17    pub resource_id: Option<Uuid>,
18}
19
20/// Port (interface) for audit log repository
21/// Handles persistence of audit events for security, compliance, and debugging
22#[async_trait]
23pub trait AuditLogRepository: Send + Sync {
24    /// Create a new audit log entry
25    async fn create(&self, entry: &AuditLogEntry) -> Result<AuditLogEntry, String>;
26
27    /// Find audit log by ID
28    async fn find_by_id(&self, id: Uuid) -> Result<Option<AuditLogEntry>, String>;
29
30    /// Find all audit logs with pagination and filters
31    /// Returns tuple of (logs, total_count)
32    async fn find_all_paginated(
33        &self,
34        page_request: &PageRequest,
35        filters: &AuditLogFilters,
36    ) -> Result<(Vec<AuditLogEntry>, i64), String>;
37
38    /// Find recent audit logs (last N entries)
39    async fn find_recent(&self, limit: i64) -> Result<Vec<AuditLogEntry>, String>;
40
41    /// Find failed operations (for security monitoring)
42    async fn find_failed_operations(
43        &self,
44        page_request: &PageRequest,
45        organization_id: Option<Uuid>,
46    ) -> Result<(Vec<AuditLogEntry>, i64), String>;
47
48    /// Delete old audit logs (for data retention policies)
49    /// Deletes logs older than the specified timestamp
50    /// Returns number of deleted entries
51    async fn delete_older_than(&self, timestamp: DateTime<Utc>) -> Result<i64, String>;
52
53    /// Count audit logs by filters
54    async fn count_by_filters(&self, filters: &AuditLogFilters) -> Result<i64, String>;
55}