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;
#[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,
}
#[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,
}
#[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 {
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()
}
}
#[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,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FxGainLossListResponseDto {
pub items: Vec<FxGainLossResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl FxGainLossListResponseDto {
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,
}
}
}
#[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>>,
}
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)
}
}