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