use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::IntegrationAccount;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::IntegrationAccountStatus;
use crate::domain::entity::OAuthProvider;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateIntegrationAccountDto {
pub provider: OAuthProvider,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "account_ref")]
pub account_ref: String,
pub status: IntegrationAccountStatus,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub scopes: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "pkce_verifier")]
pub pkce_verifier: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_refreshed_at")]
pub last_refreshed_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateIntegrationAccountDto {
pub provider: OAuthProvider,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "account_ref")]
pub account_ref: String,
pub status: IntegrationAccountStatus,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub scopes: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "pkce_verifier")]
pub pkce_verifier: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_refreshed_at")]
pub last_refreshed_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchIntegrationAccountDto {
#[serde(skip_serializing_if = "Option::is_none")]
pub provider: Option<OAuthProvider>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "account_ref")]
pub account_ref: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<IntegrationAccountStatus>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub scopes: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "pkce_verifier")]
pub pkce_verifier: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "last_refreshed_at")]
pub last_refreshed_at: Option<DateTime<Utc>>,
}
impl PatchIntegrationAccountDto {
pub fn has_changes(&self) -> bool {
self.provider.is_some() || self.account_ref.is_some() || self.status.is_some() || self.scopes.is_some() || self.pkce_verifier.is_some() || self.expires_at.is_some() || self.last_refreshed_at.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationAccountResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
pub provider: OAuthProvider,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub account_ref: String,
pub status: IntegrationAccountStatus,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub scopes: String,
pub pkce_verifier: Option<String>,
pub expires_at: Option<DateTime<Utc>>,
pub last_refreshed_at: Option<DateTime<Utc>>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationAccountListResponseDto {
pub items: Vec<IntegrationAccountResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl IntegrationAccountListResponseDto {
pub fn new(items: Vec<IntegrationAccountResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
let total_pages = if per_page > 0 {
((total as f64) / (per_page as f64)).ceil() as u32
} else {
0
};
Self {
items,
total,
page,
per_page,
total_pages,
has_next: page < total_pages,
has_prev: page > 1,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationAccountSummaryDto {
pub id: Uuid,
pub provider: OAuthProvider,
pub account_ref: String,
pub status: IntegrationAccountStatus,
pub created_at: Option<DateTime<Utc>>,
}
impl From<IntegrationAccount> for IntegrationAccountResponseDto {
fn from(entity: IntegrationAccount) -> Self {
Self {
id: entity.id,
provider: entity.provider,
account_ref: entity.account_ref,
status: entity.status,
scopes: entity.scopes,
pkce_verifier: entity.pkce_verifier,
expires_at: entity.expires_at,
last_refreshed_at: entity.last_refreshed_at,
metadata: entity.metadata,
}
}
}
impl From<IntegrationAccount> for IntegrationAccountSummaryDto {
fn from(entity: IntegrationAccount) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
provider: entity.provider,
account_ref: entity.account_ref,
status: entity.status,
created_at,
}
}
}
impl From<CreateIntegrationAccountDto> for IntegrationAccount {
fn from(dto: CreateIntegrationAccountDto) -> Self {
Self {
id: Uuid::new_v4(),
provider: dto.provider,
account_ref: dto.account_ref,
status: dto.status,
scopes: dto.scopes,
pkce_verifier: dto.pkce_verifier,
expires_at: dto.expires_at,
last_refreshed_at: dto.last_refreshed_at,
metadata: AuditMetadata::default(),
}
}
}
impl From<&IntegrationAccount> for IntegrationAccountResponseDto {
fn from(entity: &IntegrationAccount) -> Self {
Self {
id: entity.id.clone(),
provider: entity.provider.clone(),
account_ref: entity.account_ref.clone(),
status: entity.status.clone(),
scopes: entity.scopes.clone(),
pkce_verifier: entity.pkce_verifier.clone(),
expires_at: entity.expires_at.clone(),
last_refreshed_at: entity.last_refreshed_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateIntegrationAccountDto> for IntegrationAccount {
fn from_create_dto(dto: CreateIntegrationAccountDto) -> backbone_core::ServiceResult<Self> {
Ok(IntegrationAccount::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateIntegrationAccountDto> for IntegrationAccount {
fn apply_update(mut self, dto: UpdateIntegrationAccountDto) -> backbone_core::ServiceResult<Self> {
self.provider = dto.provider;
self.account_ref = dto.account_ref;
self.status = dto.status;
self.scopes = dto.scopes;
self.pkce_verifier = dto.pkce_verifier;
self.expires_at = dto.expires_at;
self.last_refreshed_at = dto.last_refreshed_at;
Ok(self)
}
}