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::MailBlacklist;
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 CreateMailBlacklistDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub email: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "opt_out_reason_id")]
pub opt_out_reason_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateMailBlacklistDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub email: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "opt_out_reason_id")]
pub opt_out_reason_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchMailBlacklistDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "opt_out_reason_id")]
pub opt_out_reason_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
}
impl PatchMailBlacklistDto {
pub fn has_changes(&self) -> bool {
self.email.is_some() || self.opt_out_reason_id.is_some() || self.active.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailBlacklistResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub email: String,
pub opt_out_reason_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailBlacklistListResponseDto {
pub items: Vec<MailBlacklistResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl MailBlacklistListResponseDto {
pub fn new(items: Vec<MailBlacklistResponseDto>, 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 MailBlacklistSummaryDto {
pub id: Uuid,
pub email: String,
pub opt_out_reason_id: Option<Uuid>,
pub active: bool,
pub created_at: Option<DateTime<Utc>>,
}
impl From<MailBlacklist> for MailBlacklistResponseDto {
fn from(entity: MailBlacklist) -> Self {
Self {
id: entity.id,
email: entity.email,
opt_out_reason_id: entity.opt_out_reason_id,
active: entity.active,
metadata: entity.metadata,
}
}
}
impl From<MailBlacklist> for MailBlacklistSummaryDto {
fn from(entity: MailBlacklist) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
email: entity.email,
opt_out_reason_id: entity.opt_out_reason_id,
active: entity.active,
created_at,
}
}
}
impl From<CreateMailBlacklistDto> for MailBlacklist {
fn from(dto: CreateMailBlacklistDto) -> Self {
Self {
id: Uuid::new_v4(),
email: dto.email,
opt_out_reason_id: dto.opt_out_reason_id,
active: dto.active,
metadata: AuditMetadata::default(),
}
}
}
impl From<&MailBlacklist> for MailBlacklistResponseDto {
fn from(entity: &MailBlacklist) -> Self {
Self {
id: entity.id.clone(),
email: entity.email.clone(),
opt_out_reason_id: entity.opt_out_reason_id.clone(),
active: entity.active.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateMailBlacklistDto> for MailBlacklist {
fn from_create_dto(dto: CreateMailBlacklistDto) -> backbone_core::ServiceResult<Self> {
Ok(MailBlacklist::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateMailBlacklistDto> for MailBlacklist {
fn apply_update(mut self, dto: UpdateMailBlacklistDto) -> backbone_core::ServiceResult<Self> {
self.email = dto.email;
self.opt_out_reason_id = dto.opt_out_reason_id;
self.active = dto.active;
Ok(self)
}
}