use crate::error::Result;
use crate::organizations::audit::OrgAuditEntry;
use async_trait::async_trait;
use std::future::Future;
#[async_trait]
pub trait OrgAuditStore: Send + Sync {
async fn record_audit(&self, entry: &OrgAuditEntry) -> Result<()>;
async fn get_org_audit_log(&self, org_id: &str, limit: usize) -> Result<Vec<OrgAuditEntry>>;
async fn get_user_audit_log(&self, user_id: &str, limit: usize) -> Result<Vec<OrgAuditEntry>>;
}
pub trait OptionalAuditStore: Send + Sync + Clone + 'static {
fn record(&self, entry: OrgAuditEntry) -> impl Future<Output = ()> + Send;
}
impl OptionalAuditStore for () {
async fn record(&self, _entry: OrgAuditEntry) {
}
}
#[derive(Clone)]
pub struct WithAuditStore<A: OrgAuditStore + Clone>(pub A);
impl<A: OrgAuditStore + Clone + 'static> OptionalAuditStore for WithAuditStore<A> {
async fn record(&self, entry: OrgAuditEntry) {
if let Err(e) = self.0.record_audit(&entry).await {
tracing::warn!(
error = %e,
event = %entry.event,
org_id = %entry.org_id,
"Failed to record audit entry"
);
}
}
}