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