backbone-banking 0.7.1

Cash & bank — bank/account master, statement import, reconciliation, and clearing postings (5th GL producer)
Documentation
//! BankClearance 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, NaiveDate};
use rust_decimal::Decimal;

#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

#[cfg(feature = "validation")]
use validator::Validate;

use crate::domain::entity::BankClearance;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::MatchMethod;
use crate::domain::entity::MatchedSourceType;

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

/// Request DTO for creating a new BankClearance
///
/// 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 CreateBankClearanceDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "bank_transaction_id")]
    pub bank_transaction_id: Uuid,
    #[serde(alias = "matched_source_type")]
    pub matched_source_type: MatchedSourceType,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "matched_source_id")]
    pub matched_source_id: Uuid,
    #[serde(alias = "matched_amount")]
    pub matched_amount: Decimal,
    #[serde(alias = "match_method")]
    pub match_method: MatchMethod,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(alias = "clearance_date")]
    pub clearance_date: NaiveDate,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
    pub accounting_post_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "journal_id")]
    pub journal_id: Option<Uuid>,
}

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

/// Request DTO for full update of a BankClearance
///
/// 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 UpdateBankClearanceDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "bank_transaction_id")]
    pub bank_transaction_id: Uuid,
    #[serde(alias = "matched_source_type")]
    pub matched_source_type: MatchedSourceType,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "matched_source_id")]
    pub matched_source_id: Uuid,
    #[serde(alias = "matched_amount")]
    pub matched_amount: Decimal,
    #[serde(alias = "match_method")]
    pub match_method: MatchMethod,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(alias = "clearance_date")]
    pub clearance_date: NaiveDate,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
    pub accounting_post_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "journal_id")]
    pub journal_id: Option<Uuid>,
}

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

/// Request DTO for partial update of a BankClearance
///
/// 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 PatchBankClearanceDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "bank_transaction_id")]
    pub bank_transaction_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "matched_source_type")]
    pub matched_source_type: Option<MatchedSourceType>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "matched_source_id")]
    pub matched_source_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "matched_amount")]
    pub matched_amount: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "match_method")]
    pub match_method: Option<MatchMethod>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "clearance_date")]
    pub clearance_date: Option<NaiveDate>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "accounting_post_id")]
    pub accounting_post_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "journal_id")]
    pub journal_id: Option<Uuid>,
}

impl PatchBankClearanceDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.bank_transaction_id.is_some() || self.matched_source_type.is_some() || self.matched_source_id.is_some() || self.matched_amount.is_some() || self.match_method.is_some() || self.clearance_date.is_some() || self.accounting_post_id.is_some() || self.journal_id.is_some()
    }
}

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

/// Response DTO for BankClearance 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 BankClearanceResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub bank_transaction_id: Uuid,
    pub matched_source_type: MatchedSourceType,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub matched_source_id: Uuid,
    pub matched_amount: Decimal,
    pub match_method: MatchMethod,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    pub clearance_date: NaiveDate,
    pub accounting_post_id: Option<Uuid>,
    pub journal_id: Option<Uuid>,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for BankClearance 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 BankClearanceListResponseDto {
    /// List of items
    pub items: Vec<BankClearanceResponseDto>,
    /// 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 BankClearanceListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<BankClearanceResponseDto>, 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 BankClearanceSummaryDto {
    pub id: Uuid,
    pub bank_transaction_id: Uuid,
    pub matched_source_type: MatchedSourceType,
    pub matched_source_id: Uuid,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<BankClearance> for BankClearanceResponseDto {
    fn from(entity: BankClearance) -> Self {
        Self {
            id: entity.id,
            bank_transaction_id: entity.bank_transaction_id,
            matched_source_type: entity.matched_source_type,
            matched_source_id: entity.matched_source_id,
            matched_amount: entity.matched_amount,
            match_method: entity.match_method,
            clearance_date: entity.clearance_date,
            accounting_post_id: entity.accounting_post_id,
            journal_id: entity.journal_id,
            metadata: entity.metadata,
        }
    }
}

impl From<BankClearance> for BankClearanceSummaryDto {
    fn from(entity: BankClearance) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            bank_transaction_id: entity.bank_transaction_id,
            matched_source_type: entity.matched_source_type,
            matched_source_id: entity.matched_source_id,
            created_at,
        }
    }
}

impl From<CreateBankClearanceDto> for BankClearance {
    fn from(dto: CreateBankClearanceDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            bank_transaction_id: dto.bank_transaction_id,
            matched_source_type: dto.matched_source_type,
            matched_source_id: dto.matched_source_id,
            matched_amount: dto.matched_amount,
            match_method: dto.match_method,
            clearance_date: dto.clearance_date,
            accounting_post_id: dto.accounting_post_id,
            journal_id: dto.journal_id,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&BankClearance> for BankClearanceResponseDto {
    fn from(entity: &BankClearance) -> Self {
        Self {
            id: entity.id.clone(),
            bank_transaction_id: entity.bank_transaction_id.clone(),
            matched_source_type: entity.matched_source_type.clone(),
            matched_source_id: entity.matched_source_id.clone(),
            matched_amount: entity.matched_amount.clone(),
            match_method: entity.match_method.clone(),
            clearance_date: entity.clearance_date.clone(),
            accounting_post_id: entity.accounting_post_id.clone(),
            journal_id: entity.journal_id.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

impl backbone_core::FromCreateDto<CreateBankClearanceDto> for BankClearance {
    fn from_create_dto(dto: CreateBankClearanceDto) -> backbone_core::ServiceResult<Self> {
        Ok(BankClearance::from(dto))
    }
}

impl backbone_core::ApplyUpdateDto<UpdateBankClearanceDto> for BankClearance {
    fn apply_update(mut self, dto: UpdateBankClearanceDto) -> backbone_core::ServiceResult<Self> {
        self.bank_transaction_id = dto.bank_transaction_id;
        self.matched_source_type = dto.matched_source_type;
        self.matched_source_id = dto.matched_source_id;
        self.matched_amount = dto.matched_amount;
        self.match_method = dto.match_method;
        self.clearance_date = dto.clearance_date;
        self.accounting_post_id = dto.accounting_post_id;
        self.journal_id = dto.journal_id;
        Ok(self)
    }
}

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

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