backbone-banking 0.7.1

Cash & bank — bank/account master, statement import, reconciliation, and clearing postings (5th GL producer)
Documentation
//! BankTransaction 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::BankTransaction;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::TxnStatus;

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

/// Request DTO for creating a new BankTransaction
///
/// 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 CreateBankTransactionDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "bank_account_id")]
    pub bank_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "import_id")]
    pub import_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(alias = "txn_date")]
    pub txn_date: NaiveDate,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "value_date")]
    pub value_date: Option<NaiveDate>,
    #[cfg_attr(feature = "validation", validate(length(max = 300)))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "reference_no")]
    pub reference_no: Option<String>,
    pub deposit: Decimal,
    pub withdrawal: Decimal,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    pub status: TxnStatus,
    #[serde(alias = "allocated_amount")]
    pub allocated_amount: Decimal,
}

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

/// Request DTO for full update of a BankTransaction
///
/// 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 UpdateBankTransactionDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "bank_account_id")]
    pub bank_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "import_id")]
    pub import_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(alias = "txn_date")]
    pub txn_date: NaiveDate,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "value_date")]
    pub value_date: Option<NaiveDate>,
    #[cfg_attr(feature = "validation", validate(length(max = 300)))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "reference_no")]
    pub reference_no: Option<String>,
    pub deposit: Decimal,
    pub withdrawal: Decimal,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    pub status: TxnStatus,
    #[serde(alias = "allocated_amount")]
    pub allocated_amount: Decimal,
}

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

/// Request DTO for partial update of a BankTransaction
///
/// 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 PatchBankTransactionDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "bank_account_id")]
    pub bank_account_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "import_id")]
    pub import_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "txn_date")]
    pub txn_date: Option<NaiveDate>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "value_date")]
    pub value_date: Option<NaiveDate>,
    #[cfg_attr(feature = "validation", validate(length(max = 300)))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "reference_no")]
    pub reference_no: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deposit: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub withdrawal: Option<Decimal>,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<TxnStatus>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "allocated_amount")]
    pub allocated_amount: Option<Decimal>,
}

impl PatchBankTransactionDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.bank_account_id.is_some() || self.import_id.is_some() || self.txn_date.is_some() || self.value_date.is_some() || self.description.is_some() || self.reference_no.is_some() || self.deposit.is_some() || self.withdrawal.is_some() || self.currency.is_some() || self.status.is_some() || self.allocated_amount.is_some()
    }
}

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

/// Response DTO for BankTransaction 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 BankTransactionResponseDto {
    #[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_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub import_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
    pub txn_date: NaiveDate,
    pub value_date: Option<NaiveDate>,
    pub description: Option<String>,
    pub reference_no: Option<String>,
    pub deposit: Decimal,
    pub withdrawal: Decimal,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    pub status: TxnStatus,
    pub allocated_amount: Decimal,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for BankTransaction 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 BankTransactionListResponseDto {
    /// List of items
    pub items: Vec<BankTransactionResponseDto>,
    /// 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 BankTransactionListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<BankTransactionResponseDto>, 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 BankTransactionSummaryDto {
    pub id: Uuid,
    pub bank_account_id: Uuid,
    pub import_id: Uuid,
    pub txn_date: NaiveDate,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<BankTransaction> for BankTransactionResponseDto {
    fn from(entity: BankTransaction) -> Self {
        Self {
            id: entity.id,
            bank_account_id: entity.bank_account_id,
            import_id: entity.import_id,
            txn_date: entity.txn_date,
            value_date: entity.value_date,
            description: entity.description,
            reference_no: entity.reference_no,
            deposit: entity.deposit,
            withdrawal: entity.withdrawal,
            currency: entity.currency,
            status: entity.status,
            allocated_amount: entity.allocated_amount,
            metadata: entity.metadata,
        }
    }
}

impl From<BankTransaction> for BankTransactionSummaryDto {
    fn from(entity: BankTransaction) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            bank_account_id: entity.bank_account_id,
            import_id: entity.import_id,
            txn_date: entity.txn_date,
            created_at,
        }
    }
}

impl From<CreateBankTransactionDto> for BankTransaction {
    fn from(dto: CreateBankTransactionDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            bank_account_id: dto.bank_account_id,
            import_id: dto.import_id,
            txn_date: dto.txn_date,
            value_date: dto.value_date,
            description: dto.description,
            reference_no: dto.reference_no,
            deposit: dto.deposit,
            withdrawal: dto.withdrawal,
            currency: dto.currency,
            status: dto.status,
            allocated_amount: dto.allocated_amount,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&BankTransaction> for BankTransactionResponseDto {
    fn from(entity: &BankTransaction) -> Self {
        Self {
            id: entity.id.clone(),
            bank_account_id: entity.bank_account_id.clone(),
            import_id: entity.import_id.clone(),
            txn_date: entity.txn_date.clone(),
            value_date: entity.value_date.clone(),
            description: entity.description.clone(),
            reference_no: entity.reference_no.clone(),
            deposit: entity.deposit.clone(),
            withdrawal: entity.withdrawal.clone(),
            currency: entity.currency.clone(),
            status: entity.status.clone(),
            allocated_amount: entity.allocated_amount.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateBankTransactionDto> for BankTransaction {
    fn apply_update(mut self, dto: UpdateBankTransactionDto) -> backbone_core::ServiceResult<Self> {
        self.bank_account_id = dto.bank_account_id;
        self.import_id = dto.import_id;
        self.txn_date = dto.txn_date;
        self.value_date = dto.value_date;
        self.description = dto.description;
        self.reference_no = dto.reference_no;
        self.deposit = dto.deposit;
        self.withdrawal = dto.withdrawal;
        self.currency = dto.currency;
        self.status = dto.status;
        self.allocated_amount = dto.allocated_amount;
        Ok(self)
    }
}

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

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