use async_trait::async_trait;
use anyhow::Result;
use crate::domain::entity::{IntegrationAccount, IntegrationAccountStatus, OAuthProvider};
#[derive(Debug, Clone, Default)]
pub struct IntegrationAccountPaginationParams {
pub page: u32,
pub per_page: u32,
}
impl IntegrationAccountPaginationParams {
pub fn new(page: u32, per_page: u32) -> Self {
Self { page, per_page }
}
pub fn offset(&self) -> u64 {
((self.page.saturating_sub(1)) * self.per_page) as u64
}
pub fn limit(&self) -> u64 {
self.per_page as u64
}
}
#[derive(Debug, Clone)]
pub struct IntegrationAccountPaginatedResult {
pub data: Vec<IntegrationAccount>,
pub total: u64,
pub page: u32,
pub per_page: u32,
}
#[derive(Debug, Clone, Default)]
pub struct IntegrationAccountFilter {
pub provider: Option<OAuthProvider>,
pub account_ref: Option<String>,
pub status: Option<IntegrationAccountStatus>,
pub scopes: Option<String>,
pub pkce_verifier: Option<String>,
}
impl IntegrationAccountFilter {
pub fn has_filters(&self) -> bool {
self.provider.is_some() || self.account_ref.is_some() || self.status.is_some() || self.scopes.is_some() || self.pkce_verifier.is_some()
}
}
#[async_trait]
pub trait IntegrationAccountRepository: Send + Sync {
async fn save(&self, entity: &IntegrationAccount) -> Result<IntegrationAccount>;
async fn find_by_id(&self, id: &str) -> Result<Option<IntegrationAccount>>;
async fn find_all(&self) -> Result<Vec<IntegrationAccount>>;
async fn update(&self, id: &str, entity: &IntegrationAccount) -> Result<Option<IntegrationAccount>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn list(&self, params: IntegrationAccountPaginationParams) -> Result<IntegrationAccountPaginatedResult>;
async fn list_with_filters(&self, params: IntegrationAccountPaginationParams, filters: IntegrationAccountFilter) -> Result<IntegrationAccountPaginatedResult>;
async fn count(&self) -> Result<u64>;
async fn count_with_filters(&self, filters: IntegrationAccountFilter) -> Result<u64>;
async fn exists(&self, id: &str) -> Result<bool>;
async fn soft_delete(&self, id: &str) -> Result<bool>;
async fn restore(&self, id: &str) -> Result<Option<IntegrationAccount>>;
async fn list_deleted(&self, params: IntegrationAccountPaginationParams) -> Result<IntegrationAccountPaginatedResult>;
async fn empty_trash(&self) -> Result<u64>;
async fn bulk_save(&self, entities: &[IntegrationAccount]) -> Result<Vec<IntegrationAccount>>;
async fn bulk_delete(&self, ids: &[&str]) -> Result<u64>;
}