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::MailFollowers;
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 CreateMailFollowersDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "res_model")]
pub res_model: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "res_id")]
pub res_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "partner_id")]
pub partner_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "channel_id")]
pub channel_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "subtype_ids")]
pub subtype_ids: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateMailFollowersDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "res_model")]
pub res_model: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "res_id")]
pub res_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "partner_id")]
pub partner_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "channel_id")]
pub channel_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "subtype_ids")]
pub subtype_ids: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchMailFollowersDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "res_model")]
pub res_model: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "res_id")]
pub res_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "partner_id")]
pub partner_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "channel_id")]
pub channel_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "subtype_ids")]
pub subtype_ids: Option<serde_json::Value>,
}
impl PatchMailFollowersDto {
pub fn has_changes(&self) -> bool {
self.res_model.is_some() || self.res_id.is_some() || self.partner_id.is_some() || self.channel_id.is_some() || self.subtype_ids.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailFollowersResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub res_model: String,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub res_id: Uuid,
pub partner_id: Option<Uuid>,
pub channel_id: Option<Uuid>,
pub subtype_ids: Option<serde_json::Value>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailFollowersListResponseDto {
pub items: Vec<MailFollowersResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl MailFollowersListResponseDto {
pub fn new(items: Vec<MailFollowersResponseDto>, 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 MailFollowersSummaryDto {
pub id: Uuid,
pub res_model: String,
pub res_id: Uuid,
pub partner_id: Option<Uuid>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<MailFollowers> for MailFollowersResponseDto {
fn from(entity: MailFollowers) -> Self {
Self {
id: entity.id,
res_model: entity.res_model,
res_id: entity.res_id,
partner_id: entity.partner_id,
channel_id: entity.channel_id,
subtype_ids: entity.subtype_ids,
metadata: entity.metadata,
}
}
}
impl From<MailFollowers> for MailFollowersSummaryDto {
fn from(entity: MailFollowers) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
res_model: entity.res_model,
res_id: entity.res_id,
partner_id: entity.partner_id,
created_at,
}
}
}
impl From<CreateMailFollowersDto> for MailFollowers {
fn from(dto: CreateMailFollowersDto) -> Self {
Self {
id: Uuid::new_v4(),
res_model: dto.res_model,
res_id: dto.res_id,
partner_id: dto.partner_id,
channel_id: dto.channel_id,
subtype_ids: dto.subtype_ids,
metadata: AuditMetadata::default(),
}
}
}
impl From<&MailFollowers> for MailFollowersResponseDto {
fn from(entity: &MailFollowers) -> Self {
Self {
id: entity.id.clone(),
res_model: entity.res_model.clone(),
res_id: entity.res_id.clone(),
partner_id: entity.partner_id.clone(),
channel_id: entity.channel_id.clone(),
subtype_ids: entity.subtype_ids.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateMailFollowersDto> for MailFollowers {
fn from_create_dto(dto: CreateMailFollowersDto) -> backbone_core::ServiceResult<Self> {
Ok(MailFollowers::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateMailFollowersDto> for MailFollowers {
fn apply_update(mut self, dto: UpdateMailFollowersDto) -> backbone_core::ServiceResult<Self> {
self.res_model = dto.res_model;
self.res_id = dto.res_id;
self.partner_id = dto.partner_id;
self.channel_id = dto.channel_id;
self.subtype_ids = dto.subtype_ids;
Ok(self)
}
}