1use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10use serde_json::Value;
11use std::collections::HashMap;
12use validator::Validate;
13
14#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
19pub struct Document {
20 #[validate(length(min = 1, max = 64))]
22 pub id: String,
23
24 #[validate(length(min = 1, max = 64))]
26 pub form_id: String,
27
28 #[validate(length(min = 1, max = 64))]
30 pub workflow_id: String,
31
32 pub current_phase: String,
34
35 pub data: HashMap<String, Value>,
42
43 pub created_at: DateTime<Utc>,
45
46 pub updated_at: DateTime<Utc>,
48}
49
50impl Document {
51 pub fn new(id: &str, form_id: &str, workflow_id: &str) -> Self {
53 let now = Utc::now();
54 Self {
55 id: id.to_string(),
56 form_id: form_id.to_string(),
57 workflow_id: workflow_id.to_string(),
58 current_phase: "".to_string(),
59 data: HashMap::new(),
60 created_at: now,
61 updated_at: now,
62 }
63 }
64
65 pub fn get_value(&self, field_id: &str) -> Option<&Value> {
67 self.data.get(field_id)
68 }
69
70 pub fn set_value(&mut self, field_id: &str, value: Value) {
72 self.data.insert(field_id.to_string(), value);
73 self.updated_at = Utc::now();
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use serde_json::json;
81
82 #[test]
83 fn test_document_creation() {
84 let mut doc = Document::new("doc_123", "incident_report", "flow_123");
85
86 doc.set_value("title", json!("Server Crash"));
88 doc.set_value("severity", json!(5));
89
90 assert_eq!(doc.id, "doc_123");
91 assert_eq!(doc.data.get("title"), Some(&json!("Server Crash")));
92 }
93
94 #[test]
95 fn test_serialization() {
96 let mut doc = Document::new("doc_1", "form_1", "flow_1");
97 doc.set_value("active", json!(true));
98
99 let json_output = serde_json::to_string(&doc).unwrap();
100 assert!(json_output.contains("doc_1"));
101 assert!(json_output.contains("active"));
102 assert!(json_output.contains("true"));
103 }
104}