backbone-integrations 0.6.0

Integration registry: connectors, integration accounts and an idempotent inbound event lane, with one OAuth flow (HMAC-bound state, PKCE)
Documentation
//! IntegrationAccount DTOs (Data Transfer Objects)
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! DTOs provide a clean separation between domain entities and API
//! representations, with validation and OpenAPI documentation support.

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;

// =============================================================================
// Create DTO
// =============================================================================

/// Request DTO for creating a new IntegrationAccount
///
/// Used in POST requests to create new entities.
/// Excludes auto-generated fields (id, created_at, updated_at, deleted_at).
#[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>>,
}

// =============================================================================
// Update DTO
// =============================================================================

/// Request DTO for full update of a IntegrationAccount
///
/// Used in PUT requests for full entity replacement.
/// All fields are required (except auto-generated ones).
#[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>>,
}

// =============================================================================
// Patch DTO
// =============================================================================

/// Request DTO for partial update of a IntegrationAccount
///
/// Used in PATCH requests for partial updates.
/// All fields are optional - only provided fields will be updated.
#[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 {
    /// Check if any field is set
    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()
    }
}

// =============================================================================
// Response DTO
// =============================================================================

/// Response DTO for IntegrationAccount entity
///
/// Used in API responses for single entity.
/// Includes all fields including metadata.
#[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,
}

// =============================================================================
// List Response DTO
// =============================================================================

/// Paginated list response for IntegrationAccount entities
///
/// Used in API responses for list endpoints.
/// Includes pagination metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationAccountListResponseDto {
    /// List of items
    pub items: Vec<IntegrationAccountResponseDto>,
    /// Total number of items (across all pages)
    pub total: u64,
    /// Current page number (1-indexed)
    pub page: u32,
    /// Number of items per page
    pub per_page: u32,
    /// Total number of pages
    pub total_pages: u32,
    /// Whether there is a next page
    pub has_next: bool,
    /// Whether there is a previous page
    pub has_prev: bool,
}

impl IntegrationAccountListResponseDto {
    /// Create a new list response from items and pagination info
    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,
        }
    }
}

/// Summary DTO for list views (compact version)
#[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>>,
}

// =============================================================================
// Conversions
// =============================================================================

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)
    }
}

// =============================================================================
// Custom DTOs
// =============================================================================

// <<< CUSTOM DTOs
// Add custom DTOs specific to IntegrationAccount here.
// This section will be preserved during regeneration.
// >>> END CUSTOM DTOs