use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::ServiceLevelAgreement;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::ServiceLevelAgreementStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateServiceLevelAgreementDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_default")]
pub is_default: bool,
pub status: ServiceLevelAgreementStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateServiceLevelAgreementDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_default")]
pub is_default: bool,
pub status: ServiceLevelAgreementStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchServiceLevelAgreementDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_default")]
pub is_default: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<ServiceLevelAgreementStatus>,
}
impl PatchServiceLevelAgreementDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.is_default.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ServiceLevelAgreementResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_default: bool,
pub status: ServiceLevelAgreementStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ServiceLevelAgreementListResponseDto {
pub items: Vec<ServiceLevelAgreementResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl ServiceLevelAgreementListResponseDto {
pub fn new(items: Vec<ServiceLevelAgreementResponseDto>, 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 ServiceLevelAgreementSummaryDto {
pub id: Uuid,
pub name: String,
pub is_default: bool,
pub status: ServiceLevelAgreementStatus,
pub created_at: Option<DateTime<Utc>>,
}
impl From<ServiceLevelAgreement> for ServiceLevelAgreementResponseDto {
fn from(entity: ServiceLevelAgreement) -> Self {
Self {
id: entity.id,
name: entity.name,
is_default: entity.is_default,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<ServiceLevelAgreement> for ServiceLevelAgreementSummaryDto {
fn from(entity: ServiceLevelAgreement) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
is_default: entity.is_default,
status: entity.status,
created_at,
}
}
}
impl From<CreateServiceLevelAgreementDto> for ServiceLevelAgreement {
fn from(dto: CreateServiceLevelAgreementDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
is_default: dto.is_default,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&ServiceLevelAgreement> for ServiceLevelAgreementResponseDto {
fn from(entity: &ServiceLevelAgreement) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
is_default: entity.is_default.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateServiceLevelAgreementDto> for ServiceLevelAgreement {
fn from_create_dto(dto: CreateServiceLevelAgreementDto) -> backbone_core::ServiceResult<Self> {
Ok(ServiceLevelAgreement::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateServiceLevelAgreementDto> for ServiceLevelAgreement {
fn apply_update(mut self, dto: UpdateServiceLevelAgreementDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.is_default = dto.is_default;
self.status = dto.status;
Ok(self)
}
}