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::MailSlot;
use crate::domain::entity::AuditMetadata;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateMailSlotDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "scheduler_id")]
pub scheduler_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "slot_id")]
pub slot_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "scheduled_date")]
pub scheduled_date: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "mail_done")]
pub mail_done: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_registration_id")]
pub last_registration_id: Option<Uuid>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateMailSlotDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "scheduler_id")]
pub scheduler_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "slot_id")]
pub slot_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "scheduled_date")]
pub scheduled_date: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "mail_done")]
pub mail_done: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_registration_id")]
pub last_registration_id: Option<Uuid>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchMailSlotDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "scheduler_id")]
pub scheduler_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "slot_id")]
pub slot_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "scheduled_date")]
pub scheduled_date: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "mail_done")]
pub mail_done: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", alias = "last_registration_id")]
pub last_registration_id: Option<Uuid>,
}
impl PatchMailSlotDto {
pub fn has_changes(&self) -> bool {
self.scheduler_id.is_some() || self.slot_id.is_some() || self.scheduled_date.is_some() || self.mail_done.is_some() || self.last_registration_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailSlotResponseDto {
#[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 scheduler_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub slot_id: Uuid,
pub scheduled_date: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub mail_done: bool,
pub last_registration_id: Option<Uuid>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailSlotListResponseDto {
pub items: Vec<MailSlotResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl MailSlotListResponseDto {
pub fn new(items: Vec<MailSlotResponseDto>, 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 MailSlotSummaryDto {
pub id: Uuid,
pub scheduler_id: Uuid,
pub slot_id: Uuid,
pub scheduled_date: Option<DateTime<Utc>>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<MailSlot> for MailSlotResponseDto {
fn from(entity: MailSlot) -> Self {
Self {
id: entity.id,
scheduler_id: entity.scheduler_id,
slot_id: entity.slot_id,
scheduled_date: entity.scheduled_date,
mail_done: entity.mail_done,
last_registration_id: entity.last_registration_id,
metadata: entity.metadata,
}
}
}
impl From<MailSlot> for MailSlotSummaryDto {
fn from(entity: MailSlot) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
scheduler_id: entity.scheduler_id,
slot_id: entity.slot_id,
scheduled_date: entity.scheduled_date,
created_at,
}
}
}
impl From<CreateMailSlotDto> for MailSlot {
fn from(dto: CreateMailSlotDto) -> Self {
Self {
id: Uuid::new_v4(),
scheduler_id: dto.scheduler_id,
slot_id: dto.slot_id,
scheduled_date: dto.scheduled_date,
mail_done: dto.mail_done,
last_registration_id: dto.last_registration_id,
metadata: AuditMetadata::default(),
}
}
}
impl From<&MailSlot> for MailSlotResponseDto {
fn from(entity: &MailSlot) -> Self {
Self {
id: entity.id.clone(),
scheduler_id: entity.scheduler_id.clone(),
slot_id: entity.slot_id.clone(),
scheduled_date: entity.scheduled_date.clone(),
mail_done: entity.mail_done.clone(),
last_registration_id: entity.last_registration_id.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateMailSlotDto> for MailSlot {
fn from_create_dto(dto: CreateMailSlotDto) -> backbone_core::ServiceResult<Self> {
Ok(MailSlot::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateMailSlotDto> for MailSlot {
fn apply_update(mut self, dto: UpdateMailSlotDto) -> backbone_core::ServiceResult<Self> {
self.scheduler_id = dto.scheduler_id;
self.slot_id = dto.slot_id;
self.scheduled_date = dto.scheduled_date;
self.mail_done = dto.mail_done;
self.last_registration_id = dto.last_registration_id;
Ok(self)
}
}