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 }
159
160impl super::Entity for ServiceLevelPriority {
161 type Id = Uuid;
162
163 fn entity_id(&self) -> &Self::Id {
164 &self.id
165 }
166
167 fn entity_type() -> &'static str {
168 "ServiceLevelPriority"
169 }
170}
171
172impl backbone_core::PersistentEntity for ServiceLevelPriority {
173 fn entity_id(&self) -> String {
174 self.id.to_string()
175 }
176 fn set_entity_id(&mut self, id: String) {
177 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
178 self.id = uuid;
179 }
180 }
181 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
182 self.metadata.created_at
183 }
184 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
185 self.metadata.created_at = Some(ts);
186 }
187 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
188 self.metadata.updated_at
189 }
190 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
191 self.metadata.updated_at = Some(ts);
192 }
193 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
194 self.metadata.deleted_at
195 }
196 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
197 self.metadata.deleted_at = ts;
198 }
199}
200
201impl backbone_orm::EntityRepoMeta for ServiceLevelPriority {
202 fn column_types() -> std::collections::HashMap<String, String> {
203 let mut m = std::collections::HashMap::new();
204 m.insert("id".to_string(), "uuid".to_string());
205 m.insert("sla_id".to_string(), "uuid".to_string());
206 m.insert("priority".to_string(), "issue_priority".to_string());
207 m
208 }
209 fn search_fields() -> &'static [&'static str] {
210 &[]
211 }
212}
213
214#[derive(Debug, Clone, Default)]
219pub struct ServiceLevelPriorityBuilder {
220 sla_id: Option<Uuid>,
221 priority: Option<IssuePriority>,
222 response_time_mins: Option<i32>,
223 resolution_time_mins: Option<i32>,
224}
225
226impl ServiceLevelPriorityBuilder {
227 pub fn sla_id(mut self, value: Uuid) -> Self {
229 self.sla_id = Some(value);
230 self
231 }
232
233 pub fn priority(mut self, value: IssuePriority) -> Self {
235 self.priority = Some(value);
236 self
237 }
238
239 pub fn response_time_mins(mut self, value: i32) -> Self {
241 self.response_time_mins = Some(value);
242 self
243 }
244
245 pub fn resolution_time_mins(mut self, value: i32) -> Self {
247 self.resolution_time_mins = Some(value);
248 self
249 }
250
251 pub fn build(self) -> Result<ServiceLevelPriority, String> {
255 let sla_id = self.sla_id.ok_or_else(|| "sla_id is required".to_string())?;
256 let priority = self.priority.ok_or_else(|| "priority is required".to_string())?;
257 let response_time_mins = self.response_time_mins.ok_or_else(|| "response_time_mins is required".to_string())?;
258 let resolution_time_mins = self.resolution_time_mins.ok_or_else(|| "resolution_time_mins is required".to_string())?;
259
260 Ok(ServiceLevelPriority {
261 id: Uuid::new_v4(),
262 sla_id,
263 priority,
264 response_time_mins,
265 resolution_time_mins,
266 metadata: AuditMetadata::default(),
267 })
268 }
269}