use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::BankCheckSequence;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateBankCheckSequenceDto {
#[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 = "example"))]
#[serde(alias = "numbering_mode")]
pub numbering_mode: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "next_number")]
pub next_number: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateBankCheckSequenceDto {
#[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 = "example"))]
#[serde(alias = "numbering_mode")]
pub numbering_mode: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "next_number")]
pub next_number: i64,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchBankCheckSequenceDto {
#[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 = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "numbering_mode")]
pub numbering_mode: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "next_number")]
pub next_number: Option<i64>,
}
impl PatchBankCheckSequenceDto {
pub fn has_changes(&self) -> bool {
self.bank_account_id.is_some() || self.numbering_mode.is_some() || self.next_number.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankCheckSequenceResponseDto {
#[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 = "example"))]
pub numbering_mode: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub next_number: i64,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub created_at: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub updated_at: DateTime<Utc>,
pub metadata: serde_json::Value,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankCheckSequenceListResponseDto {
pub items: Vec<BankCheckSequenceResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl BankCheckSequenceListResponseDto {
pub fn new(items: Vec<BankCheckSequenceResponseDto>, 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 BankCheckSequenceSummaryDto {
pub id: Uuid,
pub bank_account_id: Uuid,
pub numbering_mode: String,
pub next_number: i64,
pub created_at: Option<DateTime<Utc>>,
}
impl From<BankCheckSequence> for BankCheckSequenceResponseDto {
fn from(entity: BankCheckSequence) -> Self {
Self {
id: entity.id,
bank_account_id: entity.bank_account_id,
numbering_mode: entity.numbering_mode,
next_number: entity.next_number,
created_at: entity.created_at,
updated_at: entity.updated_at,
metadata: entity.metadata,
}
}
}
impl From<BankCheckSequence> for BankCheckSequenceSummaryDto {
fn from(entity: BankCheckSequence) -> Self {
Self {
id: entity.id,
bank_account_id: entity.bank_account_id,
numbering_mode: entity.numbering_mode,
next_number: entity.next_number,
created_at: Some(entity.created_at),
}
}
}
impl From<CreateBankCheckSequenceDto> for BankCheckSequence {
fn from(dto: CreateBankCheckSequenceDto) -> Self {
Self {
id: Uuid::new_v4(),
bank_account_id: dto.bank_account_id,
numbering_mode: dto.numbering_mode,
next_number: dto.next_number,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: serde_json::json!({}),
}
}
}
impl From<&BankCheckSequence> for BankCheckSequenceResponseDto {
fn from(entity: &BankCheckSequence) -> Self {
Self {
id: entity.id.clone(),
bank_account_id: entity.bank_account_id.clone(),
numbering_mode: entity.numbering_mode.clone(),
next_number: entity.next_number.clone(),
created_at: entity.created_at.clone(),
updated_at: entity.updated_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateBankCheckSequenceDto> for BankCheckSequence {
fn from_create_dto(dto: CreateBankCheckSequenceDto) -> backbone_core::ServiceResult<Self> {
Ok(BankCheckSequence::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateBankCheckSequenceDto> for BankCheckSequence {
fn apply_update(mut self, dto: UpdateBankCheckSequenceDto) -> backbone_core::ServiceResult<Self> {
self.updated_at = Utc::now();
self.bank_account_id = dto.bank_account_id;
self.numbering_mode = dto.numbering_mode;
self.next_number = dto.next_number;
Ok(self)
}
}