backbone_support/presentation/dto/
service_level_agreement_dto.rs1use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10use chrono::{DateTime, Utc};
11
12#[cfg(feature = "openapi")]
13#[cfg(feature = "openapi")]
14use utoipa::ToSchema;
15
16use crate::domain::entity::ServiceLevelAgreement;
17use crate::domain::entity::AuditMetadata;
18use crate::domain::entity::ServiceLevelAgreementStatus;
19
20#[derive(Debug, Clone, Deserialize)]
29#[cfg_attr(feature = "openapi", derive(ToSchema))]
30#[serde(rename_all = "camelCase")]
31pub struct CreateServiceLevelAgreementDto {
32 #[cfg_attr(feature = "openapi", schema(example = "example"))]
33 pub name: String,
34 #[cfg_attr(feature = "openapi", schema(example = true))]
35 #[serde(alias = "is_default")]
36 pub is_default: bool,
37 pub status: ServiceLevelAgreementStatus,
38}
39
40#[derive(Debug, Clone, Deserialize)]
49#[cfg_attr(feature = "openapi", derive(ToSchema))]
50#[serde(rename_all = "camelCase")]
51pub struct UpdateServiceLevelAgreementDto {
52 #[cfg_attr(feature = "openapi", schema(example = "example"))]
53 pub name: String,
54 #[cfg_attr(feature = "openapi", schema(example = true))]
55 #[serde(alias = "is_default")]
56 pub is_default: bool,
57 pub status: ServiceLevelAgreementStatus,
58}
59
60#[derive(Debug, Clone, Default, Deserialize)]
69#[cfg_attr(feature = "openapi", derive(ToSchema))]
70#[serde(rename_all = "camelCase")]
71pub struct PatchServiceLevelAgreementDto {
72 #[cfg_attr(feature = "openapi", schema(example = "example"))]
73 #[serde(skip_serializing_if = "Option::is_none")]
74 pub name: Option<String>,
75 #[cfg_attr(feature = "openapi", schema(example = true))]
76 #[serde(skip_serializing_if = "Option::is_none", alias = "is_default")]
77 pub is_default: Option<bool>,
78 #[serde(skip_serializing_if = "Option::is_none")]
79 pub status: Option<ServiceLevelAgreementStatus>,
80}
81
82impl PatchServiceLevelAgreementDto {
83 pub fn has_changes(&self) -> bool {
85 self.name.is_some() || self.is_default.is_some() || self.status.is_some()
86 }
87}
88
89#[derive(Debug, Clone, Serialize)]
98#[cfg_attr(feature = "openapi", derive(ToSchema))]
99#[serde(rename_all = "camelCase")]
100pub struct ServiceLevelAgreementResponseDto {
101 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
102 pub id: Uuid,
103 #[cfg_attr(feature = "openapi", schema(example = "example"))]
104 pub name: String,
105 #[cfg_attr(feature = "openapi", schema(example = true))]
106 pub is_default: bool,
107 pub status: ServiceLevelAgreementStatus,
108 pub metadata: AuditMetadata,
109}
110
111#[derive(Debug, Clone, Serialize)]
120#[cfg_attr(feature = "openapi", derive(ToSchema))]
121#[serde(rename_all = "camelCase")]
122pub struct ServiceLevelAgreementListResponseDto {
123 pub items: Vec<ServiceLevelAgreementResponseDto>,
125 pub total: u64,
127 pub page: u32,
129 pub per_page: u32,
131 pub total_pages: u32,
133 pub has_next: bool,
135 pub has_prev: bool,
137}
138
139impl ServiceLevelAgreementListResponseDto {
140 pub fn new(items: Vec<ServiceLevelAgreementResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
142 let total_pages = if per_page > 0 {
143 ((total as f64) / (per_page as f64)).ceil() as u32
144 } else {
145 0
146 };
147 Self {
148 items,
149 total,
150 page,
151 per_page,
152 total_pages,
153 has_next: page < total_pages,
154 has_prev: page > 1,
155 }
156 }
157}
158
159#[derive(Debug, Clone, Serialize)]
161#[cfg_attr(feature = "openapi", derive(ToSchema))]
162#[serde(rename_all = "camelCase")]
163pub struct ServiceLevelAgreementSummaryDto {
164 pub id: Uuid,
165 pub name: String,
166 pub is_default: bool,
167 pub status: ServiceLevelAgreementStatus,
168 pub created_at: Option<DateTime<Utc>>,
169}
170
171impl From<ServiceLevelAgreement> for ServiceLevelAgreementResponseDto {
176 fn from(entity: ServiceLevelAgreement) -> Self {
177 Self {
178 id: entity.id,
179 name: entity.name,
180 is_default: entity.is_default,
181 status: entity.status,
182 metadata: entity.metadata,
183 }
184 }
185}
186
187impl From<ServiceLevelAgreement> for ServiceLevelAgreementSummaryDto {
188 fn from(entity: ServiceLevelAgreement) -> Self {
189 let created_at = backbone_core::PersistentEntity::created_at(&entity);
190 Self {
191 id: entity.id,
192 name: entity.name,
193 is_default: entity.is_default,
194 status: entity.status,
195 created_at,
196 }
197 }
198}
199
200impl From<CreateServiceLevelAgreementDto> for ServiceLevelAgreement {
201 fn from(dto: CreateServiceLevelAgreementDto) -> Self {
202 Self {
203 id: Uuid::new_v4(),
204 name: dto.name,
205 is_default: dto.is_default,
206 status: dto.status,
207 metadata: AuditMetadata::default(),
208 }
209 }
210}
211
212impl From<&ServiceLevelAgreement> for ServiceLevelAgreementResponseDto {
213 fn from(entity: &ServiceLevelAgreement) -> Self {
214 Self {
215 id: entity.id.clone(),
216 name: entity.name.clone(),
217 is_default: entity.is_default.clone(),
218 status: entity.status.clone(),
219 metadata: entity.metadata.clone(),
220 }
221 }
222}
223
224impl backbone_core::FromCreateDto<CreateServiceLevelAgreementDto> for ServiceLevelAgreement {
225 fn from_create_dto(dto: CreateServiceLevelAgreementDto) -> backbone_core::ServiceResult<Self> {
226 Ok(ServiceLevelAgreement::from(dto))
227 }
228}
229
230impl backbone_core::ApplyUpdateDto<UpdateServiceLevelAgreementDto> for ServiceLevelAgreement {
231 fn apply_update(mut self, dto: UpdateServiceLevelAgreementDto) -> backbone_core::ServiceResult<Self> {
232 self.name = dto.name;
233 self.is_default = dto.is_default;
234 self.status = dto.status;
235 Ok(self)
236 }
237}
238
239