backbone-banking 0.7.1

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

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

/// Request DTO for creating a new BankAccount
///
/// 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 CreateBankAccountDto {
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
    pub branch_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "bank_id")]
    pub bank_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "account_name")]
    pub account_name: String,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "account_number")]
    pub account_number: String,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "gl_account_id")]
    pub gl_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "clearing_account_id")]
    pub clearing_account_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    #[serde(alias = "account_type")]
    pub account_type: BankAccountType,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "is_default")]
    pub is_default: bool,
    pub status: BankAccountStatus,
}

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

/// Request DTO for full update of a BankAccount
///
/// 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 UpdateBankAccountDto {
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "branch_id")]
    pub branch_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "bank_id")]
    pub bank_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "account_name")]
    pub account_name: String,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "account_number")]
    pub account_number: String,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "gl_account_id")]
    pub gl_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "clearing_account_id")]
    pub clearing_account_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    #[serde(alias = "account_type")]
    pub account_type: BankAccountType,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "is_default")]
    pub is_default: bool,
    pub status: BankAccountStatus,
}

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

/// Request DTO for partial update of a BankAccount
///
/// 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 PatchBankAccountDto {
    #[serde(skip_serializing_if = "Option::is_none", alias = "branch_id")]
    pub branch_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "bank_id")]
    pub bank_id: Option<Uuid>,
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "account_name")]
    pub account_name: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "account_number")]
    pub account_number: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "gl_account_id")]
    pub gl_account_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "clearing_account_id")]
    pub clearing_account_id: Option<Uuid>,
    #[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", alias = "account_type")]
    pub account_type: Option<BankAccountType>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "is_default")]
    pub is_default: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<BankAccountStatus>,
}

impl PatchBankAccountDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.branch_id.is_some() || self.bank_id.is_some() || self.account_name.is_some() || self.account_number.is_some() || self.gl_account_id.is_some() || self.clearing_account_id.is_some() || self.currency.is_some() || self.account_type.is_some() || self.is_default.is_some() || self.status.is_some()
    }
}

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

/// Response DTO for BankAccount 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 BankAccountResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    pub branch_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub bank_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub account_name: String,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub account_number: String,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub gl_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub clearing_account_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    pub account_type: BankAccountType,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub is_default: bool,
    pub status: BankAccountStatus,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for BankAccount 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 BankAccountListResponseDto {
    /// List of items
    pub items: Vec<BankAccountResponseDto>,
    /// 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 BankAccountListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<BankAccountResponseDto>, 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 BankAccountSummaryDto {
    pub id: Uuid,
    pub branch_id: Option<Uuid>,
    pub bank_id: Uuid,
    pub account_name: String,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<BankAccount> for BankAccountResponseDto {
    fn from(entity: BankAccount) -> Self {
        Self {
            id: entity.id,
            branch_id: entity.branch_id,
            bank_id: entity.bank_id,
            account_name: entity.account_name,
            account_number: entity.account_number,
            gl_account_id: entity.gl_account_id,
            clearing_account_id: entity.clearing_account_id,
            currency: entity.currency,
            account_type: entity.account_type,
            is_default: entity.is_default,
            status: entity.status,
            metadata: entity.metadata,
        }
    }
}

impl From<BankAccount> for BankAccountSummaryDto {
    fn from(entity: BankAccount) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            branch_id: entity.branch_id,
            bank_id: entity.bank_id,
            account_name: entity.account_name,
            created_at,
        }
    }
}

impl From<CreateBankAccountDto> for BankAccount {
    fn from(dto: CreateBankAccountDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            branch_id: dto.branch_id,
            bank_id: dto.bank_id,
            account_name: dto.account_name,
            account_number: dto.account_number,
            gl_account_id: dto.gl_account_id,
            clearing_account_id: dto.clearing_account_id,
            currency: dto.currency,
            account_type: dto.account_type,
            is_default: dto.is_default,
            status: dto.status,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&BankAccount> for BankAccountResponseDto {
    fn from(entity: &BankAccount) -> Self {
        Self {
            id: entity.id.clone(),
            branch_id: entity.branch_id.clone(),
            bank_id: entity.bank_id.clone(),
            account_name: entity.account_name.clone(),
            account_number: entity.account_number.clone(),
            gl_account_id: entity.gl_account_id.clone(),
            clearing_account_id: entity.clearing_account_id.clone(),
            currency: entity.currency.clone(),
            account_type: entity.account_type.clone(),
            is_default: entity.is_default.clone(),
            status: entity.status.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateBankAccountDto> for BankAccount {
    fn apply_update(mut self, dto: UpdateBankAccountDto) -> backbone_core::ServiceResult<Self> {
        self.branch_id = dto.branch_id;
        self.bank_id = dto.bank_id;
        self.account_name = dto.account_name;
        self.account_number = dto.account_number;
        self.gl_account_id = dto.gl_account_id;
        self.clearing_account_id = dto.clearing_account_id;
        self.currency = dto.currency;
        self.account_type = dto.account_type;
        self.is_default = dto.is_default;
        self.status = dto.status;
        Ok(self)
    }
}

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

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