backbone-banking 0.7.1

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

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

/// Request DTO for creating a new Bank
///
/// 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 CreateBankDto {
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "validation", validate(length(max = 11)))]
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "swift_bic")]
    pub swift_bic: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 2)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub country: String,
    pub status: BankStatus,
}

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

/// Request DTO for full update of a Bank
///
/// 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 UpdateBankDto {
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "validation", validate(length(max = 11)))]
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "swift_bic")]
    pub swift_bic: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 2)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub country: String,
    pub status: BankStatus,
}

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

/// Request DTO for partial update of a Bank
///
/// 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 PatchBankDto {
    #[cfg_attr(feature = "validation", validate(length(max = 140)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 11)))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "swift_bic")]
    pub swift_bic: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 2)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<BankStatus>,
}

impl PatchBankDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.name.is_some() || self.swift_bic.is_some() || self.country.is_some() || self.status.is_some()
    }
}

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

/// Response DTO for Bank 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 BankResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    pub swift_bic: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub country: String,
    pub status: BankStatus,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for Bank 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 BankListResponseDto {
    /// List of items
    pub items: Vec<BankResponseDto>,
    /// 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 BankListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<BankResponseDto>, 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 BankSummaryDto {
    pub id: Uuid,
    pub name: String,
    pub swift_bic: Option<String>,
    pub country: String,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<Bank> for BankResponseDto {
    fn from(entity: Bank) -> Self {
        Self {
            id: entity.id,
            name: entity.name,
            swift_bic: entity.swift_bic,
            country: entity.country,
            status: entity.status,
            metadata: entity.metadata,
        }
    }
}

impl From<Bank> for BankSummaryDto {
    fn from(entity: Bank) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            name: entity.name,
            swift_bic: entity.swift_bic,
            country: entity.country,
            created_at,
        }
    }
}

impl From<CreateBankDto> for Bank {
    fn from(dto: CreateBankDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: dto.name,
            swift_bic: dto.swift_bic,
            country: dto.country,
            status: dto.status,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&Bank> for BankResponseDto {
    fn from(entity: &Bank) -> Self {
        Self {
            id: entity.id.clone(),
            name: entity.name.clone(),
            swift_bic: entity.swift_bic.clone(),
            country: entity.country.clone(),
            status: entity.status.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateBankDto> for Bank {
    fn apply_update(mut self, dto: UpdateBankDto) -> backbone_core::ServiceResult<Self> {
        self.name = dto.name;
        self.swift_bic = dto.swift_bic;
        self.country = dto.country;
        self.status = dto.status;
        Ok(self)
    }
}

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

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