Skip to main content

wellfare_core/
mental_wellbeing.rs

1//! Mental wellbeing observations and therapy notes (MHT — no licensed instruments).
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::record::{
7    EpistemicStatus, EvidenceType, InstantBridge, RecordEnvelope, SensitivityClass,
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct WellbeingObservation {
12    pub id: String,
13    pub mood_label: String,
14    #[serde(default, skip_serializing_if = "Option::is_none")]
15    pub intensity_1_10: Option<u8>,
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub notes: Option<String>,
18}
19
20impl WellbeingObservation {
21    /// Creates a new instance.
22    pub fn new(mood_label: impl Into<String>) -> Self {
23        Self {
24            id: Uuid::new_v4().to_string(),
25            mood_label: mood_label.into(),
26            intensity_1_10: None,
27            notes: None,
28        }
29    }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct TherapyNote {
34    pub id: String,
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    pub provider_label: Option<String>,
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub session_date_unix: Option<u32>,
39    /// High-resolution instant (T71 bridge). Preferred over `session_date_unix`
40    /// when present; the u32 field is kept for backward-compatible deserialization.
41    #[serde(skip_serializing_if = "Option::is_none", default)]
42    pub session_date_instant: Option<InstantBridge>,
43    pub notes: String,
44}
45
46impl TherapyNote {
47    /// Creates a new instance.
48    pub fn new(notes: impl Into<String>) -> Self {
49        Self {
50            id: Uuid::new_v4().to_string(),
51            provider_label: None,
52            session_date_unix: None,
53            session_date_instant: None,
54            notes: notes.into(),
55        }
56    }
57
58    /// Resolve the session-date instant, preferring the high-resolution
59    /// `InstantBridge` field when present (T71 bridge).
60    pub fn session_date(&self) -> Option<InstantBridge> {
61        self.session_date_instant.or_else(|| {
62            self.session_date_unix
63                .map(|t| InstantBridge::from_coarse(t))
64        })
65    }
66}
67
68///  record ID.
69pub fn wellbeing_observation_record_id(uuid: &str) -> String {
70    format!("urn:wellfair:wellbeing_observation:{uuid}")
71}
72
73///  record ID.
74pub fn therapy_note_record_id(uuid: &str) -> String {
75    format!("urn:wellfair:therapy_note:{uuid}")
76}
77
78fn wellbeing_envelope(
79    id: &str,
80    owner_did: &str,
81    author_did: &str,
82    asserted_unix: u32,
83    blob_hash: Option<String>,
84    sensitivity: SensitivityClass,
85) -> RecordEnvelope {
86    RecordEnvelope {
87        id: id.to_string(),
88        owner_did: owner_did.to_string(),
89        author_did: author_did.to_string(),
90        proxy_did: None,
91        epistemic_status: EpistemicStatus::Asserted,
92        evidence_type: EvidenceType::SelfReported,
93        sensitivity,
94        asserted_time_unix: asserted_unix,
95        asserted_instant: None,
96        valid_time_start_unix: Some(asserted_unix),
97        valid_time_start_instant: None,
98        valid_time_end_unix: None,
99        valid_time_end_instant: None,
100        predecessor_id: None,
101        blob_hash,
102        tombstone: false,
103    }
104}
105
106/// Builds the  envelope.
107pub fn build_wellbeing_observation_envelope(
108    report: &WellbeingObservation,
109    owner_did: &str,
110    author_did: &str,
111    asserted_unix: u32,
112    blob_hash: Option<String>,
113) -> RecordEnvelope {
114    wellbeing_envelope(
115        &wellbeing_observation_record_id(&report.id),
116        owner_did,
117        author_did,
118        asserted_unix,
119        blob_hash,
120        SensitivityClass::Restricted,
121    )
122}
123
124/// Builds the  envelope.
125pub fn build_therapy_note_envelope(
126    report: &TherapyNote,
127    owner_did: &str,
128    author_did: &str,
129    asserted_unix: u32,
130    blob_hash: Option<String>,
131) -> RecordEnvelope {
132    wellbeing_envelope(
133        &therapy_note_record_id(&report.id),
134        owner_did,
135        author_did,
136        asserted_unix,
137        blob_hash,
138        SensitivityClass::Classified,
139    )
140}
141
142///  summary.
143pub fn wellbeing_observation_summary(report: &WellbeingObservation) -> String {
144    serde_json::json!({
145        "mood": report.mood_label,
146        "intensity": report.intensity_1_10,
147    })
148    .to_string()
149}
150
151///  summary.
152pub fn therapy_note_summary(report: &TherapyNote) -> String {
153    serde_json::json!({
154        "provider": report.provider_label,
155        "session_date_unix": report.session_date_unix,
156        "note_len": report.notes.len(),
157    })
158    .to_string()
159}
160
161#[cfg(test)]
162mod tests {
163    use super::*;
164
165    #[test]
166    fn therapy_note_classified_sanctuary_kind() {
167        let note = TherapyNote::new("session reflection");
168        let env = build_therapy_note_envelope(&note, "did:o", "did:a", 1, None);
169        assert_eq!(env.sensitivity, SensitivityClass::Classified);
170        assert!(env.id.contains(":therapy_note:"));
171    }
172}