2025-03-20 09:22:31 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use crate::domain::entities::session::Session;
|
|
|
|
|
use crate::common::errors::DomainError;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum SessionRepositoryError {
|
2026-02-12 09:41:25 +01:00
|
|
|
#[error("Session not found: {0}")]
|
2025-03-20 09:22:31 +01:00
|
|
|
NotFound(String),
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
#[error("Database error: {0}")]
|
2025-03-20 09:22:31 +01:00
|
|
|
DatabaseError(String),
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
#[error("Timeout error: {0}")]
|
2025-03-20 09:22:31 +01:00
|
|
|
Timeout(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type SessionRepositoryResult<T> = Result<T, SessionRepositoryError>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Conversion from SessionRepositoryError to DomainError
|
2025-03-20 09:22:31 +01:00
|
|
|
impl From<SessionRepositoryError> for DomainError {
|
|
|
|
|
fn from(err: SessionRepositoryError) -> Self {
|
|
|
|
|
match err {
|
|
|
|
|
SessionRepositoryError::NotFound(msg) => {
|
|
|
|
|
DomainError::not_found("Session", msg)
|
|
|
|
|
},
|
|
|
|
|
SessionRepositoryError::DatabaseError(msg) => {
|
|
|
|
|
DomainError::internal_error("Database", msg)
|
|
|
|
|
},
|
|
|
|
|
SessionRepositoryError::Timeout(msg) => {
|
|
|
|
|
DomainError::timeout("Database", msg)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
pub trait SessionRepository: Send + Sync + 'static {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Creates a new session
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn create_session(&self, session: Session) -> SessionRepositoryResult<Session>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Gets a session by ID
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn get_session_by_id(&self, id: &str) -> SessionRepositoryResult<Session>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Gets a session by refresh token
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn get_session_by_refresh_token(&self, refresh_token: &str) -> SessionRepositoryResult<Session>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Gets all sessions for a user
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn get_sessions_by_user_id(&self, user_id: &str) -> SessionRepositoryResult<Vec<Session>>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Revokes a specific session
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn revoke_session(&self, session_id: &str) -> SessionRepositoryResult<()>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Revokes all sessions for a user
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn revoke_all_user_sessions(&self, user_id: &str) -> SessionRepositoryResult<u64>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Deletes expired sessions
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn delete_expired_sessions(&self) -> SessionRepositoryResult<u64>;
|
|
|
|
|
}
|