Skip to main content

wellfare_core/
life_records.rs

1//! Life events, welfare cases, and case tasks (LIF-01..17 records-first).
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::record::{
7    EpistemicStatus, EvidenceType, InstantBridge, RecordEnvelope, SensitivityClass,
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum CaseStatus {
13    Open,
14    Paused,
15    Closed,
16}
17
18impl Default for CaseStatus {
19    fn default() -> Self {
20        Self::Open
21    }
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25pub struct LifeEventReport {
26    pub id: String,
27    pub title: String,
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub event_type: Option<String>,
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub wellbeing_impact: Option<String>,
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub occurred_at_unix: Option<u32>,
34    /// High-resolution instant (T71 bridge). Preferred over `occurred_at_unix`
35    /// when present; the u32 field is kept for backward-compatible deserialization.
36    #[serde(skip_serializing_if = "Option::is_none", default)]
37    pub occurred_at_instant: Option<InstantBridge>,
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub notes: Option<String>,
40}
41
42impl LifeEventReport {
43    /// Creates a new instance.
44    pub fn new(title: impl Into<String>) -> Self {
45        Self {
46            id: Uuid::new_v4().to_string(),
47            title: title.into(),
48            event_type: None,
49            wellbeing_impact: None,
50            occurred_at_unix: None,
51            occurred_at_instant: None,
52            notes: None,
53        }
54    }
55
56    /// Resolve the occurred-at instant, preferring the high-resolution
57    /// `InstantBridge` field when present (T71 bridge).
58    pub fn occurred_at(&self) -> Option<InstantBridge> {
59        self.occurred_at_instant
60            .or_else(|| self.occurred_at_unix.map(|t| InstantBridge::from_coarse(t)))
61    }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
65pub struct WelfareCaseReport {
66    pub id: String,
67    pub title: String,
68    #[serde(default)]
69    pub status: CaseStatus,
70    #[serde(default, skip_serializing_if = "Option::is_none")]
71    pub summary: Option<String>,
72    #[serde(default, skip_serializing_if = "Option::is_none")]
73    pub assistance_needs: Option<String>,
74}
75
76impl WelfareCaseReport {
77    /// Creates a new instance.
78    pub fn new(title: impl Into<String>) -> Self {
79        Self {
80            id: Uuid::new_v4().to_string(),
81            title: title.into(),
82            status: CaseStatus::Open,
83            summary: None,
84            assistance_needs: None,
85        }
86    }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
90pub struct CaseTaskReport {
91    pub id: String,
92    pub case_id: String,
93    pub title: String,
94    #[serde(default)]
95    pub completed: bool,
96    #[serde(default, skip_serializing_if = "Option::is_none")]
97    pub due_at_unix: Option<u32>,
98    /// High-resolution instant (T71 bridge). Preferred over `due_at_unix`
99    /// when present; the u32 field is kept for backward-compatible deserialization.
100    #[serde(skip_serializing_if = "Option::is_none", default)]
101    pub due_at_instant: Option<InstantBridge>,
102}
103
104impl CaseTaskReport {
105    /// Creates a new instance.
106    pub fn new(case_id: impl Into<String>, title: impl Into<String>) -> Self {
107        Self {
108            id: Uuid::new_v4().to_string(),
109            case_id: case_id.into(),
110            title: title.into(),
111            completed: false,
112            due_at_unix: None,
113            due_at_instant: None,
114        }
115    }
116
117    /// Resolve the due-at instant, preferring the high-resolution
118    /// `InstantBridge` field when present (T71 bridge).
119    pub fn due_at(&self) -> Option<InstantBridge> {
120        self.due_at_instant
121            .or_else(|| self.due_at_unix.map(|t| InstantBridge::from_coarse(t)))
122    }
123}
124
125///  record ID.
126pub fn life_event_record_id(uuid: &str) -> String {
127    format!("urn:wellfair:life_event:{uuid}")
128}
129
130///  record ID.
131pub fn welfare_case_record_id(uuid: &str) -> String {
132    format!("urn:wellfair:welfare_case:{uuid}")
133}
134
135///  record ID.
136pub fn case_task_record_id(uuid: &str) -> String {
137    format!("urn:wellfair:case_task:{uuid}")
138}
139
140fn life_envelope(
141    id: &str,
142    owner_did: &str,
143    author_did: &str,
144    asserted_unix: u32,
145    blob_hash: Option<String>,
146    sensitivity: SensitivityClass,
147) -> RecordEnvelope {
148    RecordEnvelope {
149        id: id.to_string(),
150        owner_did: owner_did.to_string(),
151        author_did: author_did.to_string(),
152        proxy_did: None,
153        epistemic_status: EpistemicStatus::Asserted,
154        evidence_type: EvidenceType::SelfReported,
155        sensitivity,
156        asserted_time_unix: asserted_unix,
157        asserted_instant: None,
158        valid_time_start_unix: Some(asserted_unix),
159        valid_time_start_instant: None,
160        valid_time_end_unix: None,
161        valid_time_end_instant: None,
162        predecessor_id: None,
163        blob_hash,
164        tombstone: false,
165    }
166}
167
168/// Builds the  envelope.
169pub fn build_life_event_envelope(
170    report: &LifeEventReport,
171    owner_did: &str,
172    author_did: &str,
173    asserted_unix: u32,
174    blob_hash: Option<String>,
175) -> RecordEnvelope {
176    life_envelope(
177        &life_event_record_id(&report.id),
178        owner_did,
179        author_did,
180        asserted_unix,
181        blob_hash,
182        SensitivityClass::Restricted,
183    )
184}
185
186/// Builds the  envelope.
187pub fn build_welfare_case_envelope(
188    report: &WelfareCaseReport,
189    owner_did: &str,
190    author_did: &str,
191    asserted_unix: u32,
192    blob_hash: Option<String>,
193) -> RecordEnvelope {
194    life_envelope(
195        &welfare_case_record_id(&report.id),
196        owner_did,
197        author_did,
198        asserted_unix,
199        blob_hash,
200        SensitivityClass::Restricted,
201    )
202}
203
204/// Builds the  envelope.
205pub fn build_case_task_envelope(
206    report: &CaseTaskReport,
207    owner_did: &str,
208    author_did: &str,
209    asserted_unix: u32,
210    blob_hash: Option<String>,
211) -> RecordEnvelope {
212    life_envelope(
213        &case_task_record_id(&report.id),
214        owner_did,
215        author_did,
216        asserted_unix,
217        blob_hash,
218        SensitivityClass::Restricted,
219    )
220}
221
222///  summary.
223pub fn life_event_summary(report: &LifeEventReport) -> String {
224    serde_json::json!({
225        "title": report.title,
226        "event_type": report.event_type,
227        "occurred_at_unix": report.occurred_at_unix,
228    })
229    .to_string()
230}
231
232///  summary.
233pub fn welfare_case_summary(report: &WelfareCaseReport) -> String {
234    serde_json::json!({
235        "title": report.title,
236        "status": report.status,
237    })
238    .to_string()
239}
240
241///  summary.
242pub fn case_task_summary(report: &CaseTaskReport) -> String {
243    serde_json::json!({
244        "case_id": report.case_id,
245        "title": report.title,
246        "completed": report.completed,
247        "due_at_unix": report.due_at_unix,
248    })
249    .to_string()
250}
251
252#[cfg(test)]
253mod tests {
254    use super::*;
255
256    #[test]
257    fn life_event_envelope_kind() {
258        let report = LifeEventReport::new("Moved residence");
259        let env = build_life_event_envelope(&report, "did:o", "did:a", 100, None);
260        assert!(env.id.contains(":life_event:"));
261        assert_eq!(env.sensitivity, SensitivityClass::Restricted);
262    }
263
264    #[test]
265    fn welfare_case_is_restricted_sanctuary_kind() {
266        let report = WelfareCaseReport::new("Housing assistance");
267        let env = build_welfare_case_envelope(&report, "did:o", "did:a", 100, None);
268        assert_eq!(env.sensitivity, SensitivityClass::Restricted);
269    }
270}