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::LeadProvenance;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::EventLeadGrouping;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateLeadProvenanceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "rule_id")]
pub rule_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "event_id")]
pub event_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "lead_id")]
pub lead_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 160)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "group_key")]
pub group_key: String,
pub grouping: EventLeadGrouping,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateLeadProvenanceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "rule_id")]
pub rule_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "event_id")]
pub event_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "lead_id")]
pub lead_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 160)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "group_key")]
pub group_key: String,
pub grouping: EventLeadGrouping,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchLeadProvenanceDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "rule_id")]
pub rule_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "event_id")]
pub event_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "lead_id")]
pub lead_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 160)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "group_key")]
pub group_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub grouping: Option<EventLeadGrouping>,
}
impl PatchLeadProvenanceDto {
pub fn has_changes(&self) -> bool {
self.rule_id.is_some() || self.event_id.is_some() || self.lead_id.is_some() || self.group_key.is_some() || self.grouping.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LeadProvenanceResponseDto {
#[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 rule_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub event_id: Uuid,
pub lead_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub group_key: String,
pub grouping: EventLeadGrouping,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LeadProvenanceListResponseDto {
pub items: Vec<LeadProvenanceResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl LeadProvenanceListResponseDto {
pub fn new(items: Vec<LeadProvenanceResponseDto>, 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 LeadProvenanceSummaryDto {
pub id: Uuid,
pub rule_id: Uuid,
pub event_id: Uuid,
pub lead_id: Option<Uuid>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<LeadProvenance> for LeadProvenanceResponseDto {
fn from(entity: LeadProvenance) -> Self {
Self {
id: entity.id,
rule_id: entity.rule_id,
event_id: entity.event_id,
lead_id: entity.lead_id,
group_key: entity.group_key,
grouping: entity.grouping,
metadata: entity.metadata,
}
}
}
impl From<LeadProvenance> for LeadProvenanceSummaryDto {
fn from(entity: LeadProvenance) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
rule_id: entity.rule_id,
event_id: entity.event_id,
lead_id: entity.lead_id,
created_at,
}
}
}
impl From<CreateLeadProvenanceDto> for LeadProvenance {
fn from(dto: CreateLeadProvenanceDto) -> Self {
Self {
id: Uuid::new_v4(),
rule_id: dto.rule_id,
event_id: dto.event_id,
lead_id: dto.lead_id,
group_key: dto.group_key,
grouping: dto.grouping,
metadata: AuditMetadata::default(),
}
}
}
impl From<&LeadProvenance> for LeadProvenanceResponseDto {
fn from(entity: &LeadProvenance) -> Self {
Self {
id: entity.id.clone(),
rule_id: entity.rule_id.clone(),
event_id: entity.event_id.clone(),
lead_id: entity.lead_id.clone(),
group_key: entity.group_key.clone(),
grouping: entity.grouping.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateLeadProvenanceDto> for LeadProvenance {
fn from_create_dto(dto: CreateLeadProvenanceDto) -> backbone_core::ServiceResult<Self> {
Ok(LeadProvenance::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateLeadProvenanceDto> for LeadProvenance {
fn apply_update(mut self, dto: UpdateLeadProvenanceDto) -> backbone_core::ServiceResult<Self> {
self.rule_id = dto.rule_id;
self.event_id = dto.event_id;
self.lead_id = dto.lead_id;
self.group_key = dto.group_key;
self.grouping = dto.grouping;
Ok(self)
}
}