backbone-mail 0.2.32

Odoo mail core port — message/notification/followers/activity/alias/sms queue (schema: messaging)
//! FetchmailServer 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::FetchmailServer;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::FetchmailState;

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

/// Request DTO for creating a new FetchmailServer
///
/// 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 CreateFetchmailServerDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub active: bool,
    pub state: FetchmailState,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "default_thread_model")]
    pub default_thread_model: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub priority: i32,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "token_hash")]
    pub token_hash: String,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "last_fetch_at")]
    pub last_fetch_at: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "error_at")]
    pub error_at: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "error_message")]
    pub error_message: Option<String>,
}

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

/// Request DTO for full update of a FetchmailServer
///
/// 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 UpdateFetchmailServerDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub active: bool,
    pub state: FetchmailState,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "default_thread_model")]
    pub default_thread_model: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub priority: i32,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "token_hash")]
    pub token_hash: String,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "last_fetch_at")]
    pub last_fetch_at: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "error_at")]
    pub error_at: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "error_message")]
    pub error_message: Option<String>,
}

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

/// Request DTO for partial update of a FetchmailServer
///
/// 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 PatchFetchmailServerDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<FetchmailState>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "default_thread_model")]
    pub default_thread_model: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<i32>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "token_hash")]
    pub token_hash: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "last_fetch_at")]
    pub last_fetch_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "error_at")]
    pub error_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "error_message")]
    pub error_message: Option<String>,
}

impl PatchFetchmailServerDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.name.is_some() || self.active.is_some() || self.state.is_some() || self.default_thread_model.is_some() || self.priority.is_some() || self.token_hash.is_some() || self.last_fetch_at.is_some() || self.error_at.is_some() || self.error_message.is_some()
    }
}

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

/// Response DTO for FetchmailServer 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 FetchmailServerResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub active: bool,
    pub state: FetchmailState,
    pub default_thread_model: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub priority: i32,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub token_hash: String,
    pub last_fetch_at: Option<DateTime<Utc>>,
    pub error_at: Option<DateTime<Utc>>,
    pub error_message: Option<String>,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for FetchmailServer 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 FetchmailServerListResponseDto {
    /// List of items
    pub items: Vec<FetchmailServerResponseDto>,
    /// 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 FetchmailServerListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<FetchmailServerResponseDto>, 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 FetchmailServerSummaryDto {
    pub id: Uuid,
    pub name: String,
    pub active: bool,
    pub state: FetchmailState,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<FetchmailServer> for FetchmailServerResponseDto {
    fn from(entity: FetchmailServer) -> Self {
        Self {
            id: entity.id,
            name: entity.name,
            active: entity.active,
            state: entity.state,
            default_thread_model: entity.default_thread_model,
            priority: entity.priority,
            token_hash: entity.token_hash,
            last_fetch_at: entity.last_fetch_at,
            error_at: entity.error_at,
            error_message: entity.error_message,
            metadata: entity.metadata,
        }
    }
}

impl From<FetchmailServer> for FetchmailServerSummaryDto {
    fn from(entity: FetchmailServer) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            name: entity.name,
            active: entity.active,
            state: entity.state,
            created_at,
        }
    }
}

impl From<CreateFetchmailServerDto> for FetchmailServer {
    fn from(dto: CreateFetchmailServerDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: dto.name,
            active: dto.active,
            state: dto.state,
            default_thread_model: dto.default_thread_model,
            priority: dto.priority,
            token_hash: dto.token_hash,
            last_fetch_at: dto.last_fetch_at,
            error_at: dto.error_at,
            error_message: dto.error_message,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&FetchmailServer> for FetchmailServerResponseDto {
    fn from(entity: &FetchmailServer) -> Self {
        Self {
            id: entity.id.clone(),
            name: entity.name.clone(),
            active: entity.active.clone(),
            state: entity.state.clone(),
            default_thread_model: entity.default_thread_model.clone(),
            priority: entity.priority.clone(),
            token_hash: entity.token_hash.clone(),
            last_fetch_at: entity.last_fetch_at.clone(),
            error_at: entity.error_at.clone(),
            error_message: entity.error_message.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateFetchmailServerDto> for FetchmailServer {
    fn apply_update(mut self, dto: UpdateFetchmailServerDto) -> backbone_core::ServiceResult<Self> {
        self.name = dto.name;
        self.active = dto.active;
        self.state = dto.state;
        self.default_thread_model = dto.default_thread_model;
        self.priority = dto.priority;
        self.token_hash = dto.token_hash;
        self.last_fetch_at = dto.last_fetch_at;
        self.error_at = dto.error_at;
        self.error_message = dto.error_message;
        Ok(self)
    }
}

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

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