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