-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathevent.rs
More file actions
98 lines (87 loc) · 2.64 KB
/
Copy pathevent.rs
File metadata and controls
98 lines (87 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
/// Core event type for audit trail
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Event {
/// Unique event ID
pub id: String,
/// Document or aggregate ID being modified
pub aggregate_id: String,
/// Type of event (Created, Updated, Deleted, etc.)
pub event_type: String,
/// Event payload as JSON
pub data: serde_json::Value,
/// Timestamp when event occurred
pub timestamp: DateTime<Utc>,
/// Sequential event number
pub sequence: u64,
/// User or system that triggered the event
pub actor: String,
/// Additional metadata
pub metadata: Option<serde_json::Value>,
}
impl Event {
/// Create a new event
pub fn new(
aggregate_id: String,
event_type: String,
data: serde_json::Value,
actor: String,
) -> Self {
Self {
id: Uuid::new_v4().to_string(),
aggregate_id,
event_type,
data,
timestamp: Utc::now(),
sequence: 0,
actor,
metadata: None,
}
}
/// Add metadata to event
pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
self.metadata = Some(metadata);
self
}
/// Serialize event to JSON string
pub fn to_json(&self) -> crate::error::Result<String> {
serde_json::to_string(self)
.map_err(|e| crate::error::AuditError::SerializationError(e.to_string()))
}
/// Deserialize event from JSON string
pub fn from_json(json: &str) -> crate::error::Result<Self> {
serde_json::from_str(json)
.map_err(|e| crate::error::AuditError::SerializationError(e.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_creation() {
let event = Event::new(
"doc-1".to_string(),
"Created".to_string(),
serde_json::json!({"title": "Test"}),
"user-1".to_string(),
);
assert_eq!(event.aggregate_id, "doc-1");
assert_eq!(event.event_type, "Created");
assert!(!event.id.is_empty());
}
#[test]
fn test_event_serialization() {
let event = Event::new(
"doc-1".to_string(),
"Created".to_string(),
serde_json::json!({"title": "Test"}),
"user-1".to_string(),
);
let json = event.to_json().unwrap();
let deserialized = Event::from_json(&json).unwrap();
assert_eq!(event.id, deserialized.id);
assert_eq!(event.aggregate_id, deserialized.aggregate_id);
}
}