backbone_support/domain/entity/
service_level_priority.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::IssuePriority;
7use super::AuditMetadata;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct ServiceLevelPriorityId(pub Uuid);
13
14impl ServiceLevelPriorityId {
15 pub fn new(id: Uuid) -> Self { Self(id) }
16 pub fn generate() -> Self { Self(Uuid::new_v4()) }
17 pub fn into_inner(self) -> Uuid { self.0 }
18}
19
20impl std::fmt::Display for ServiceLevelPriorityId {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 write!(f, "{}", self.0)
23 }
24}
25
26impl std::str::FromStr for ServiceLevelPriorityId {
27 type Err = uuid::Error;
28 fn from_str(s: &str) -> Result<Self, Self::Err> {
29 Ok(Self(Uuid::parse_str(s)?))
30 }
31}
32
33impl From<Uuid> for ServiceLevelPriorityId {
34 fn from(id: Uuid) -> Self { Self(id) }
35}
36
37impl From<ServiceLevelPriorityId> for Uuid {
38 fn from(id: ServiceLevelPriorityId) -> Self { id.0 }
39}
40
41impl AsRef<Uuid> for ServiceLevelPriorityId {
42 fn as_ref(&self) -> &Uuid { &self.0 }
43}
44
45impl std::ops::Deref for ServiceLevelPriorityId {
46 type Target = Uuid;
47 fn deref(&self) -> &Self::Target { &self.0 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
51pub struct ServiceLevelPriority {
52 pub id: Uuid,
53 pub sla_id: Uuid,
54 pub priority: IssuePriority,
55 pub response_time_mins: i32,
56 pub resolution_time_mins: i32,
57 #[serde(default)]
58 #[sqlx(json)]
59 pub metadata: AuditMetadata,
60}
61
62impl ServiceLevelPriority {
63 pub fn builder() -> ServiceLevelPriorityBuilder {
65 <ServiceLevelPriorityBuilder as Default>::default()
66 }
67
68 pub fn new(sla_id: Uuid, priority: IssuePriority, response_time_mins: i32, resolution_time_mins: i32) -> Self {
70 Self {
71 id: Uuid::new_v4(),
72 sla_id,
73 priority,
74 response_time_mins,
75 resolution_time_mins,
76 metadata: AuditMetadata::default(),
77 }
78 }
79
80 pub fn id(&self) -> &Uuid {
82 &self.id
83 }
84
85 pub fn typed_id(&self) -> ServiceLevelPriorityId {
87 ServiceLevelPriorityId(self.id)
88 }
89
90 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
92 self.metadata.created_at.as_ref()
93 }
94
95 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
97 self.metadata.updated_at.as_ref()
98 }
99
100 pub fn is_deleted(&self) -> bool {
102 self.metadata.deleted_at.is_some()
103 }
104
105 pub fn is_active(&self) -> bool {
107 self.metadata.deleted_at.is_none()
108 }
109
110 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
112 self.metadata.deleted_at.as_ref()
113 }
114
115 pub fn created_by(&self) -> Option<&Uuid> {
117 self.metadata.created_by.as_ref()
118 }
119
120 pub fn updated_by(&self) -> Option<&Uuid> {
122 self.metadata.updated_by.as_ref()
123 }
124
125 pub fn deleted_by(&self) -> Option<&Uuid> {
127 self.metadata.deleted_by.as_ref()
128 }
129
130
131 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
137 for (key, value) in fields {
138 match key.as_str() {
139 "sla_id" => {
140 if let Ok(v) = serde_json::from_value(value) { self.sla_id = v; }
141 }
142 "priority" => {
143 if let Ok(v) = serde_json::from_value(value) { self.priority = v; }
144 }
145 "response_time_mins" => {
146 if let Ok(v) = serde_json::from_value(value) { self.response_time_mins = v; }
147 }
148 "resolution_time_mins" => {
149 if let Ok(v) = serde_json::from_value(value) { self.resolution_time_mins = v; }
150 }
151 _ => {} }
153 }
154 }
155
156}
157
158impl super::Entity for ServiceLevelPriority {
159 type Id = Uuid;
160
161 fn entity_id(&self) -> &Self::Id {
162 &self.id
163 }
164
165 fn entity_type() -> &'static str {
166 "ServiceLevelPriority"
167 }
168}
169
170impl backbone_core::PersistentEntity for ServiceLevelPriority {
171 fn entity_id(&self) -> String {
172 self.id.to_string()
173 }
174 fn set_entity_id(&mut self, id: String) {
175 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
176 self.id = uuid;
177 }
178 }
179 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
180 self.metadata.created_at
181 }
182 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
183 self.metadata.created_at = Some(ts);
184 }
185 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
186 self.metadata.updated_at
187 }
188 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
189 self.metadata.updated_at = Some(ts);
190 }
191 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
192 self.metadata.deleted_at
193 }
194 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
195 self.metadata.deleted_at = ts;
196 }
197}
198
199impl backbone_orm::EntityRepoMeta for ServiceLevelPriority {
200 fn column_types() -> std::collections::HashMap<String, String> {
201 let mut m = std::collections::HashMap::new();
202 m.insert("id".to_string(), "uuid".to_string());
203 m.insert("sla_id".to_string(), "uuid".to_string());
204 m.insert("priority".to_string(), "issue_priority".to_string());
205 m
206 }
207 fn search_fields() -> &'static [&'static str] {
208 &[]
209 }
210 fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
211 &[("sla", "service_level_agreements", "slaId")]
212 }
213}
214
215#[derive(Debug, Clone, Default)]
220pub struct ServiceLevelPriorityBuilder {
221 sla_id: Option<Uuid>,
222 priority: Option<IssuePriority>,
223 response_time_mins: Option<i32>,
224 resolution_time_mins: Option<i32>,
225}
226
227impl ServiceLevelPriorityBuilder {
228 pub fn sla_id(mut self, value: Uuid) -> Self {
230 self.sla_id = Some(value);
231 self
232 }
233
234 pub fn priority(mut self, value: IssuePriority) -> Self {
236 self.priority = Some(value);
237 self
238 }
239
240 pub fn response_time_mins(mut self, value: i32) -> Self {
242 self.response_time_mins = Some(value);
243 self
244 }
245
246 pub fn resolution_time_mins(mut self, value: i32) -> Self {
248 self.resolution_time_mins = Some(value);
249 self
250 }
251
252 pub fn build(self) -> Result<ServiceLevelPriority, String> {
256 let sla_id = self.sla_id.ok_or_else(|| "sla_id is required".to_string())?;
257 let priority = self.priority.ok_or_else(|| "priority is required".to_string())?;
258 let response_time_mins = self.response_time_mins.ok_or_else(|| "response_time_mins is required".to_string())?;
259 let resolution_time_mins = self.resolution_time_mins.ok_or_else(|| "resolution_time_mins is required".to_string())?;
260
261 Ok(ServiceLevelPriority {
262 id: Uuid::new_v4(),
263 sla_id,
264 priority,
265 response_time_mins,
266 resolution_time_mins,
267 metadata: AuditMetadata::default(),
268 })
269 }
270}