backbone_activity/domain/entity/
activity.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::ActivityType;
7use super::ActivityDirection;
8use super::ActivityStatus;
9use super::AuditMetadata;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[serde(transparent)]
14pub struct ActivityId(pub Uuid);
15
16impl ActivityId {
17 pub fn new(id: Uuid) -> Self { Self(id) }
18 pub fn generate() -> Self { Self(Uuid::new_v4()) }
19 pub fn into_inner(self) -> Uuid { self.0 }
20}
21
22impl std::fmt::Display for ActivityId {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}", self.0)
25 }
26}
27
28impl std::str::FromStr for ActivityId {
29 type Err = uuid::Error;
30 fn from_str(s: &str) -> Result<Self, Self::Err> {
31 Ok(Self(Uuid::parse_str(s)?))
32 }
33}
34
35impl From<Uuid> for ActivityId {
36 fn from(id: Uuid) -> Self { Self(id) }
37}
38
39impl From<ActivityId> for Uuid {
40 fn from(id: ActivityId) -> Self { id.0 }
41}
42
43impl AsRef<Uuid> for ActivityId {
44 fn as_ref(&self) -> &Uuid { &self.0 }
45}
46
47impl std::ops::Deref for ActivityId {
48 type Target = Uuid;
49 fn deref(&self) -> &Self::Target { &self.0 }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
53pub struct Activity {
54 pub id: Uuid,
55 pub subject: String,
56 pub activity_type: ActivityType,
57 pub direction: Option<ActivityDirection>,
58 pub lead_id: Option<Uuid>,
59 pub opportunity_id: Option<Uuid>,
60 pub party_id: Option<Uuid>,
61 pub status: ActivityStatus,
62 pub scheduled_at: Option<DateTime<Utc>>,
63 pub occurred_at: Option<DateTime<Utc>>,
64 pub assignee_id: Option<Uuid>,
65 pub outcome: Option<String>,
66 pub notes: Option<String>,
67 #[serde(default)]
68 #[sqlx(json)]
69 pub metadata: AuditMetadata,
70}
71
72impl Activity {
73 pub fn builder() -> ActivityBuilder {
75 <ActivityBuilder as Default>::default()
76 }
77
78 pub fn new(subject: String, activity_type: ActivityType, status: ActivityStatus) -> Self {
80 Self {
81 id: Uuid::new_v4(),
82 subject,
83 activity_type,
84 direction: None,
85 lead_id: None,
86 opportunity_id: None,
87 party_id: None,
88 status,
89 scheduled_at: None,
90 occurred_at: None,
91 assignee_id: None,
92 outcome: None,
93 notes: None,
94 metadata: AuditMetadata::default(),
95 }
96 }
97
98 pub fn id(&self) -> &Uuid {
100 &self.id
101 }
102
103 pub fn typed_id(&self) -> ActivityId {
105 ActivityId(self.id)
106 }
107
108 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
110 self.metadata.created_at.as_ref()
111 }
112
113 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
115 self.metadata.updated_at.as_ref()
116 }
117
118 pub fn is_deleted(&self) -> bool {
120 self.metadata.deleted_at.is_some()
121 }
122
123 pub fn is_active(&self) -> bool {
125 self.metadata.deleted_at.is_none()
126 }
127
128 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
130 self.metadata.deleted_at.as_ref()
131 }
132
133 pub fn created_by(&self) -> Option<&Uuid> {
135 self.metadata.created_by.as_ref()
136 }
137
138 pub fn updated_by(&self) -> Option<&Uuid> {
140 self.metadata.updated_by.as_ref()
141 }
142
143 pub fn deleted_by(&self) -> Option<&Uuid> {
145 self.metadata.deleted_by.as_ref()
146 }
147
148 pub fn status(&self) -> &ActivityStatus {
150 &self.status
151 }
152
153
154 pub fn with_direction(mut self, value: ActivityDirection) -> Self {
160 self.direction = Some(value);
161 self
162 }
163
164 pub fn with_lead_id(mut self, value: Uuid) -> Self {
166 self.lead_id = Some(value);
167 self
168 }
169
170 pub fn with_opportunity_id(mut self, value: Uuid) -> Self {
172 self.opportunity_id = Some(value);
173 self
174 }
175
176 pub fn with_party_id(mut self, value: Uuid) -> Self {
178 self.party_id = Some(value);
179 self
180 }
181
182 pub fn with_scheduled_at(mut self, value: DateTime<Utc>) -> Self {
184 self.scheduled_at = Some(value);
185 self
186 }
187
188 pub fn with_occurred_at(mut self, value: DateTime<Utc>) -> Self {
190 self.occurred_at = Some(value);
191 self
192 }
193
194 pub fn with_assignee_id(mut self, value: Uuid) -> Self {
196 self.assignee_id = Some(value);
197 self
198 }
199
200 pub fn with_outcome(mut self, value: String) -> Self {
202 self.outcome = Some(value);
203 self
204 }
205
206 pub fn with_notes(mut self, value: String) -> Self {
208 self.notes = Some(value);
209 self
210 }
211
212 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
218 for (key, value) in fields {
219 match key.as_str() {
220 "subject" => {
221 if let Ok(v) = serde_json::from_value(value) { self.subject = v; }
222 }
223 "activity_type" => {
224 if let Ok(v) = serde_json::from_value(value) { self.activity_type = v; }
225 }
226 "direction" => {
227 if let Ok(v) = serde_json::from_value(value) { self.direction = v; }
228 }
229 "lead_id" => {
230 if let Ok(v) = serde_json::from_value(value) { self.lead_id = v; }
231 }
232 "opportunity_id" => {
233 if let Ok(v) = serde_json::from_value(value) { self.opportunity_id = v; }
234 }
235 "party_id" => {
236 if let Ok(v) = serde_json::from_value(value) { self.party_id = v; }
237 }
238 "status" => {
239 if let Ok(v) = serde_json::from_value(value) { self.status = v; }
240 }
241 "scheduled_at" => {
242 if let Ok(v) = serde_json::from_value(value) { self.scheduled_at = v; }
243 }
244 "occurred_at" => {
245 if let Ok(v) = serde_json::from_value(value) { self.occurred_at = v; }
246 }
247 "assignee_id" => {
248 if let Ok(v) = serde_json::from_value(value) { self.assignee_id = v; }
249 }
250 "outcome" => {
251 if let Ok(v) = serde_json::from_value(value) { self.outcome = v; }
252 }
253 "notes" => {
254 if let Ok(v) = serde_json::from_value(value) { self.notes = v; }
255 }
256 _ => {} }
258 }
259 }
260
261}
262
263impl super::Entity for Activity {
264 type Id = Uuid;
265
266 fn entity_id(&self) -> &Self::Id {
267 &self.id
268 }
269
270 fn entity_type() -> &'static str {
271 "Activity"
272 }
273}
274
275impl backbone_core::PersistentEntity for Activity {
276 fn entity_id(&self) -> String {
277 self.id.to_string()
278 }
279 fn set_entity_id(&mut self, id: String) {
280 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
281 self.id = uuid;
282 }
283 }
284 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
285 self.metadata.created_at
286 }
287 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
288 self.metadata.created_at = Some(ts);
289 }
290 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
291 self.metadata.updated_at
292 }
293 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
294 self.metadata.updated_at = Some(ts);
295 }
296 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
297 self.metadata.deleted_at
298 }
299 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
300 self.metadata.deleted_at = ts;
301 }
302}
303
304impl backbone_orm::EntityRepoMeta for Activity {
305 fn column_types() -> std::collections::HashMap<String, String> {
306 let mut m = std::collections::HashMap::new();
307 m.insert("id".to_string(), "uuid".to_string());
308 m.insert("lead_id".to_string(), "uuid".to_string());
309 m.insert("opportunity_id".to_string(), "uuid".to_string());
310 m.insert("party_id".to_string(), "uuid".to_string());
311 m.insert("assignee_id".to_string(), "uuid".to_string());
312 m.insert("activity_type".to_string(), "activity_type".to_string());
313 m.insert("direction".to_string(), "activity_direction".to_string());
314 m.insert("status".to_string(), "activity_status".to_string());
315 m.insert("scheduled_at".to_string(), "timestamptz".to_string());
316 m.insert("occurred_at".to_string(), "timestamptz".to_string());
317 m
318 }
319 fn search_fields() -> &'static [&'static str] {
320 &["subject"]
321 }
322}
323
324#[derive(Debug, Clone, Default)]
329pub struct ActivityBuilder {
330 subject: Option<String>,
331 activity_type: Option<ActivityType>,
332 direction: Option<ActivityDirection>,
333 lead_id: Option<Uuid>,
334 opportunity_id: Option<Uuid>,
335 party_id: Option<Uuid>,
336 status: Option<ActivityStatus>,
337 scheduled_at: Option<DateTime<Utc>>,
338 occurred_at: Option<DateTime<Utc>>,
339 assignee_id: Option<Uuid>,
340 outcome: Option<String>,
341 notes: Option<String>,
342}
343
344impl ActivityBuilder {
345 pub fn subject(mut self, value: String) -> Self {
347 self.subject = Some(value);
348 self
349 }
350
351 pub fn activity_type(mut self, value: ActivityType) -> Self {
353 self.activity_type = Some(value);
354 self
355 }
356
357 pub fn direction(mut self, value: ActivityDirection) -> Self {
359 self.direction = Some(value);
360 self
361 }
362
363 pub fn lead_id(mut self, value: Uuid) -> Self {
365 self.lead_id = Some(value);
366 self
367 }
368
369 pub fn opportunity_id(mut self, value: Uuid) -> Self {
371 self.opportunity_id = Some(value);
372 self
373 }
374
375 pub fn party_id(mut self, value: Uuid) -> Self {
377 self.party_id = Some(value);
378 self
379 }
380
381 pub fn status(mut self, value: ActivityStatus) -> Self {
383 self.status = Some(value);
384 self
385 }
386
387 pub fn scheduled_at(mut self, value: DateTime<Utc>) -> Self {
389 self.scheduled_at = Some(value);
390 self
391 }
392
393 pub fn occurred_at(mut self, value: DateTime<Utc>) -> Self {
395 self.occurred_at = Some(value);
396 self
397 }
398
399 pub fn assignee_id(mut self, value: Uuid) -> Self {
401 self.assignee_id = Some(value);
402 self
403 }
404
405 pub fn outcome(mut self, value: String) -> Self {
407 self.outcome = Some(value);
408 self
409 }
410
411 pub fn notes(mut self, value: String) -> Self {
413 self.notes = Some(value);
414 self
415 }
416
417 pub fn build(self) -> Result<Activity, String> {
421 let subject = self.subject.ok_or_else(|| "subject is required".to_string())?;
422
423 Ok(Activity {
424 id: Uuid::new_v4(),
425 subject,
426 activity_type: self.activity_type.unwrap_or_default(),
427 direction: self.direction,
428 lead_id: self.lead_id,
429 opportunity_id: self.opportunity_id,
430 party_id: self.party_id,
431 status: self.status.unwrap_or_default(),
432 scheduled_at: self.scheduled_at,
433 occurred_at: self.occurred_at,
434 assignee_id: self.assignee_id,
435 outcome: self.outcome,
436 notes: self.notes,
437 metadata: AuditMetadata::default(),
438 })
439 }
440}