backbone-banking 0.7.1

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

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

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

use crate::domain::entity::FxGainLoss;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::FxDirection;

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

/// Request DTO for creating a new FxGainLoss
///
/// 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 CreateFxGainLossDto {
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "bank_clearance_id")]
    pub bank_clearance_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "matched_source_id")]
    pub matched_source_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    #[serde(alias = "original_rate")]
    pub original_rate: Decimal,
    #[serde(alias = "realised_rate")]
    pub realised_rate: Decimal,
    #[serde(alias = "base_amount_delta")]
    pub base_amount_delta: Decimal,
    pub direction: FxDirection,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "fx_account_id")]
    pub fx_account_id: Uuid,
}

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

/// Request DTO for full update of a FxGainLoss
///
/// 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 UpdateFxGainLossDto {
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "bank_clearance_id")]
    pub bank_clearance_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "matched_source_id")]
    pub matched_source_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 3)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    #[serde(alias = "original_rate")]
    pub original_rate: Decimal,
    #[serde(alias = "realised_rate")]
    pub realised_rate: Decimal,
    #[serde(alias = "base_amount_delta")]
    pub base_amount_delta: Decimal,
    pub direction: FxDirection,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "fx_account_id")]
    pub fx_account_id: Uuid,
}

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

/// Request DTO for partial update of a FxGainLoss
///
/// 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 PatchFxGainLossDto {
    #[serde(skip_serializing_if = "Option::is_none", alias = "bank_clearance_id")]
    pub bank_clearance_id: Option<Uuid>,
    #[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>,
    #[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 = "original_rate")]
    pub original_rate: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "realised_rate")]
    pub realised_rate: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "base_amount_delta")]
    pub base_amount_delta: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direction: Option<FxDirection>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "fx_account_id")]
    pub fx_account_id: Option<Uuid>,
}

impl PatchFxGainLossDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.bank_clearance_id.is_some() || self.matched_source_id.is_some() || self.currency.is_some() || self.original_rate.is_some() || self.realised_rate.is_some() || self.base_amount_delta.is_some() || self.direction.is_some() || self.fx_account_id.is_some()
    }
}

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

/// Response DTO for FxGainLoss 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 FxGainLossResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    pub bank_clearance_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub matched_source_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub currency: String,
    pub original_rate: Decimal,
    pub realised_rate: Decimal,
    pub base_amount_delta: Decimal,
    pub direction: FxDirection,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub fx_account_id: Uuid,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for FxGainLoss 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 FxGainLossListResponseDto {
    /// List of items
    pub items: Vec<FxGainLossResponseDto>,
    /// 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 FxGainLossListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<FxGainLossResponseDto>, 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 FxGainLossSummaryDto {
    pub id: Uuid,
    pub bank_clearance_id: Option<Uuid>,
    pub matched_source_id: Uuid,
    pub currency: String,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<FxGainLoss> for FxGainLossResponseDto {
    fn from(entity: FxGainLoss) -> Self {
        Self {
            id: entity.id,
            bank_clearance_id: entity.bank_clearance_id,
            matched_source_id: entity.matched_source_id,
            currency: entity.currency,
            original_rate: entity.original_rate,
            realised_rate: entity.realised_rate,
            base_amount_delta: entity.base_amount_delta,
            direction: entity.direction,
            fx_account_id: entity.fx_account_id,
            metadata: entity.metadata,
        }
    }
}

impl From<FxGainLoss> for FxGainLossSummaryDto {
    fn from(entity: FxGainLoss) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            bank_clearance_id: entity.bank_clearance_id,
            matched_source_id: entity.matched_source_id,
            currency: entity.currency,
            created_at,
        }
    }
}

impl From<CreateFxGainLossDto> for FxGainLoss {
    fn from(dto: CreateFxGainLossDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            bank_clearance_id: dto.bank_clearance_id,
            matched_source_id: dto.matched_source_id,
            currency: dto.currency,
            original_rate: dto.original_rate,
            realised_rate: dto.realised_rate,
            base_amount_delta: dto.base_amount_delta,
            direction: dto.direction,
            fx_account_id: dto.fx_account_id,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&FxGainLoss> for FxGainLossResponseDto {
    fn from(entity: &FxGainLoss) -> Self {
        Self {
            id: entity.id.clone(),
            bank_clearance_id: entity.bank_clearance_id.clone(),
            matched_source_id: entity.matched_source_id.clone(),
            currency: entity.currency.clone(),
            original_rate: entity.original_rate.clone(),
            realised_rate: entity.realised_rate.clone(),
            base_amount_delta: entity.base_amount_delta.clone(),
            direction: entity.direction.clone(),
            fx_account_id: entity.fx_account_id.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateFxGainLossDto> for FxGainLoss {
    fn apply_update(mut self, dto: UpdateFxGainLossDto) -> backbone_core::ServiceResult<Self> {
        self.bank_clearance_id = dto.bank_clearance_id;
        self.matched_source_id = dto.matched_source_id;
        self.currency = dto.currency;
        self.original_rate = dto.original_rate;
        self.realised_rate = dto.realised_rate;
        self.base_amount_delta = dto.base_amount_delta;
        self.direction = dto.direction;
        self.fx_account_id = dto.fx_account_id;
        Ok(self)
    }
}

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

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