backbone-mail 0.2.32

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

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

/// Request DTO for creating a new MailAttachment
///
/// 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 CreateMailAttachmentDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mimetype: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size: Option<i32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub datas: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checksum: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "access_token")]
    pub access_token: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "owner_party_id")]
    pub owner_party_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "owner_guest_id")]
    pub owner_guest_id: Option<Uuid>,
}

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

/// Request DTO for full update of a MailAttachment
///
/// 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 UpdateMailAttachmentDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mimetype: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size: Option<i32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub datas: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checksum: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "access_token")]
    pub access_token: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "owner_party_id")]
    pub owner_party_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "owner_guest_id")]
    pub owner_guest_id: Option<Uuid>,
}

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

/// Request DTO for partial update of a MailAttachment
///
/// 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 PatchMailAttachmentDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mimetype: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub datas: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub checksum: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "access_token")]
    pub access_token: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "owner_party_id")]
    pub owner_party_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "owner_guest_id")]
    pub owner_guest_id: Option<Uuid>,
}

impl PatchMailAttachmentDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.name.is_some() || self.mimetype.is_some() || self.size.is_some() || self.datas.is_some() || self.checksum.is_some() || self.access_token.is_some() || self.owner_party_id.is_some() || self.owner_guest_id.is_some()
    }
}

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

/// Response DTO for MailAttachment 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 MailAttachmentResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub name: String,
    pub mimetype: Option<String>,
    pub size: Option<i32>,
    pub datas: Option<String>,
    pub checksum: Option<String>,
    pub access_token: Option<Uuid>,
    pub owner_party_id: Option<Uuid>,
    pub owner_guest_id: Option<Uuid>,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for MailAttachment 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 MailAttachmentListResponseDto {
    /// List of items
    pub items: Vec<MailAttachmentResponseDto>,
    /// 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 MailAttachmentListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<MailAttachmentResponseDto>, 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 MailAttachmentSummaryDto {
    pub id: Uuid,
    pub name: String,
    pub mimetype: Option<String>,
    pub size: Option<i32>,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<MailAttachment> for MailAttachmentResponseDto {
    fn from(entity: MailAttachment) -> Self {
        Self {
            id: entity.id,
            name: entity.name,
            mimetype: entity.mimetype,
            size: entity.size,
            datas: entity.datas,
            checksum: entity.checksum,
            access_token: entity.access_token,
            owner_party_id: entity.owner_party_id,
            owner_guest_id: entity.owner_guest_id,
            metadata: entity.metadata,
        }
    }
}

impl From<MailAttachment> for MailAttachmentSummaryDto {
    fn from(entity: MailAttachment) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            name: entity.name,
            mimetype: entity.mimetype,
            size: entity.size,
            created_at,
        }
    }
}

impl From<CreateMailAttachmentDto> for MailAttachment {
    fn from(dto: CreateMailAttachmentDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: dto.name,
            mimetype: dto.mimetype,
            size: dto.size,
            datas: dto.datas,
            checksum: dto.checksum,
            access_token: dto.access_token,
            owner_party_id: dto.owner_party_id,
            owner_guest_id: dto.owner_guest_id,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&MailAttachment> for MailAttachmentResponseDto {
    fn from(entity: &MailAttachment) -> Self {
        Self {
            id: entity.id.clone(),
            name: entity.name.clone(),
            mimetype: entity.mimetype.clone(),
            size: entity.size.clone(),
            datas: entity.datas.clone(),
            checksum: entity.checksum.clone(),
            access_token: entity.access_token.clone(),
            owner_party_id: entity.owner_party_id.clone(),
            owner_guest_id: entity.owner_guest_id.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateMailAttachmentDto> for MailAttachment {
    fn apply_update(mut self, dto: UpdateMailAttachmentDto) -> backbone_core::ServiceResult<Self> {
        self.name = dto.name;
        self.mimetype = dto.mimetype;
        self.size = dto.size;
        self.datas = dto.datas;
        self.checksum = dto.checksum;
        self.access_token = dto.access_token;
        self.owner_party_id = dto.owner_party_id;
        self.owner_guest_id = dto.owner_guest_id;
        Ok(self)
    }
}

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

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