use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::ServiceLevelPriority;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::IssuePriority;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateServiceLevelPriorityDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "sla_id")]
pub sla_id: Uuid,
pub priority: IssuePriority,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "response_time_mins")]
pub response_time_mins: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "resolution_time_mins")]
pub resolution_time_mins: i32,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateServiceLevelPriorityDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "sla_id")]
pub sla_id: Uuid,
pub priority: IssuePriority,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "response_time_mins")]
pub response_time_mins: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "resolution_time_mins")]
pub resolution_time_mins: i32,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchServiceLevelPriorityDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "sla_id")]
pub sla_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<IssuePriority>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "response_time_mins")]
pub response_time_mins: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "resolution_time_mins")]
pub resolution_time_mins: Option<i32>,
}
impl PatchServiceLevelPriorityDto {
pub fn has_changes(&self) -> bool {
self.sla_id.is_some() || self.priority.is_some() || self.response_time_mins.is_some() || self.resolution_time_mins.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ServiceLevelPriorityResponseDto {
#[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 sla_id: Uuid,
pub priority: IssuePriority,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub response_time_mins: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub resolution_time_mins: i32,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ServiceLevelPriorityListResponseDto {
pub items: Vec<ServiceLevelPriorityResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl ServiceLevelPriorityListResponseDto {
pub fn new(items: Vec<ServiceLevelPriorityResponseDto>, 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 ServiceLevelPrioritySummaryDto {
pub id: Uuid,
pub sla_id: Uuid,
pub priority: IssuePriority,
pub response_time_mins: i32,
pub created_at: Option<DateTime<Utc>>,
}
impl From<ServiceLevelPriority> for ServiceLevelPriorityResponseDto {
fn from(entity: ServiceLevelPriority) -> Self {
Self {
id: entity.id,
sla_id: entity.sla_id,
priority: entity.priority,
response_time_mins: entity.response_time_mins,
resolution_time_mins: entity.resolution_time_mins,
metadata: entity.metadata,
}
}
}
impl From<ServiceLevelPriority> for ServiceLevelPrioritySummaryDto {
fn from(entity: ServiceLevelPriority) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
sla_id: entity.sla_id,
priority: entity.priority,
response_time_mins: entity.response_time_mins,
created_at,
}
}
}
impl From<CreateServiceLevelPriorityDto> for ServiceLevelPriority {
fn from(dto: CreateServiceLevelPriorityDto) -> Self {
Self {
id: Uuid::new_v4(),
sla_id: dto.sla_id,
priority: dto.priority,
response_time_mins: dto.response_time_mins,
resolution_time_mins: dto.resolution_time_mins,
metadata: AuditMetadata::default(),
}
}
}
impl From<&ServiceLevelPriority> for ServiceLevelPriorityResponseDto {
fn from(entity: &ServiceLevelPriority) -> Self {
Self {
id: entity.id.clone(),
sla_id: entity.sla_id.clone(),
priority: entity.priority.clone(),
response_time_mins: entity.response_time_mins.clone(),
resolution_time_mins: entity.resolution_time_mins.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateServiceLevelPriorityDto> for ServiceLevelPriority {
fn from_create_dto(dto: CreateServiceLevelPriorityDto) -> backbone_core::ServiceResult<Self> {
Ok(ServiceLevelPriority::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateServiceLevelPriorityDto> for ServiceLevelPriority {
fn apply_update(mut self, dto: UpdateServiceLevelPriorityDto) -> backbone_core::ServiceResult<Self> {
self.sla_id = dto.sla_id;
self.priority = dto.priority;
self.response_time_mins = dto.response_time_mins;
self.resolution_time_mins = dto.resolution_time_mins;
Ok(self)
}
}