1use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::record::{EpistemicStatus, EvidenceType, RecordEnvelope, SensitivityClass};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct DisputedDiagnosisReport {
11 pub id: String,
12 pub label: String,
13 #[serde(default, skip_serializing_if = "Option::is_none")]
14 pub attributed_by: Option<String>,
15 #[serde(default, skip_serializing_if = "Option::is_none")]
16 pub dispute_reason: Option<String>,
17 #[serde(default, skip_serializing_if = "Option::is_none")]
18 pub supporting_notes: Option<String>,
19}
20
21impl DisputedDiagnosisReport {
22 pub fn new(label: impl Into<String>) -> Self {
24 Self {
25 id: Uuid::new_v4().to_string(),
26 label: label.into(),
27 attributed_by: None,
28 dispute_reason: None,
29 supporting_notes: None,
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
35#[serde(rename_all = "snake_case")]
36pub enum DwellingType {
37 Fixed,
38 Temporary,
39 MobileShelter,
40 Homeless,
41 Unknown,
42}
43
44impl Default for DwellingType {
45 fn default() -> Self {
46 Self::Unknown
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
52pub struct HousingSafetyReport {
53 pub id: String,
54 #[serde(default)]
55 pub dwelling_type: DwellingType,
56 #[serde(default)]
57 pub homelessness: bool,
58 #[serde(default)]
59 pub violence_concern: bool,
60 #[serde(default, skip_serializing_if = "Option::is_none")]
61 pub hazards: Option<String>,
62 #[serde(default, skip_serializing_if = "Option::is_none")]
63 pub location_notes: Option<String>,
64 #[serde(default, skip_serializing_if = "Option::is_none")]
65 pub notes: Option<String>,
66}
67
68impl HousingSafetyReport {
69 pub fn new() -> Self {
71 Self {
72 id: Uuid::new_v4().to_string(),
73 dwelling_type: DwellingType::Unknown,
74 homelessness: false,
75 violence_concern: false,
76 hazards: None,
77 location_notes: None,
78 notes: None,
79 }
80 }
81}
82
83pub fn disputed_diagnosis_record_id(uuid: &str) -> String {
85 format!("urn:wellfair:disputed_diagnosis:{uuid}")
86}
87
88pub fn housing_safety_record_id(uuid: &str) -> String {
90 format!("urn:wellfair:housing_safety:{uuid}")
91}
92
93pub fn journal_kind_for_personal_record_id(record_id: &str) -> Option<&'static str> {
95 if record_id.contains(":disputed_diagnosis:") {
96 Some("disputed_diagnosis")
97 } else if record_id.contains(":housing_safety:") {
98 Some("housing_safety")
99 } else {
100 None
101 }
102}
103
104fn envelope_with_epistemic(
105 id: &str,
106 owner_did: &str,
107 author_did: &str,
108 asserted_unix: u32,
109 blob_hash: Option<String>,
110 epistemic: EpistemicStatus,
111 sensitivity: SensitivityClass,
112) -> RecordEnvelope {
113 RecordEnvelope {
114 id: id.to_string(),
115 owner_did: owner_did.to_string(),
116 author_did: author_did.to_string(),
117 proxy_did: None,
118 epistemic_status: epistemic,
119 evidence_type: EvidenceType::SelfReported,
120 sensitivity,
121 asserted_time_unix: asserted_unix,
122 asserted_instant: None,
123 valid_time_start_unix: Some(asserted_unix),
124 valid_time_start_instant: None,
125 valid_time_end_unix: None,
126 valid_time_end_instant: None,
127 predecessor_id: None,
128 blob_hash,
129 tombstone: false,
130 }
131}
132
133pub fn build_disputed_diagnosis_envelope(
135 report: &DisputedDiagnosisReport,
136 owner_did: &str,
137 author_did: &str,
138 asserted_unix: u32,
139 blob_hash: Option<String>,
140) -> RecordEnvelope {
141 let id = disputed_diagnosis_record_id(&report.id);
142 envelope_with_epistemic(
143 &id,
144 owner_did,
145 author_did,
146 asserted_unix,
147 blob_hash,
148 EpistemicStatus::Disputed,
149 SensitivityClass::Restricted,
150 )
151}
152
153pub fn build_housing_safety_envelope(
155 report: &HousingSafetyReport,
156 owner_did: &str,
157 author_did: &str,
158 asserted_unix: u32,
159 blob_hash: Option<String>,
160) -> RecordEnvelope {
161 let id = housing_safety_record_id(&report.id);
162 envelope_with_epistemic(
163 &id,
164 owner_did,
165 author_did,
166 asserted_unix,
167 blob_hash,
168 EpistemicStatus::Asserted,
169 SensitivityClass::Restricted,
170 )
171}
172
173pub fn disputed_diagnosis_summary(report: &DisputedDiagnosisReport) -> String {
175 serde_json::json!({
176 "label": report.label,
177 "epistemic": "disputed",
178 "attributed_by": report.attributed_by,
179 "dispute_reason": report.dispute_reason,
180 })
181 .to_string()
182}
183
184pub fn housing_safety_summary(report: &HousingSafetyReport) -> String {
186 serde_json::json!({
187 "dwelling_type": report.dwelling_type,
188 "homelessness": report.homelessness,
189 "violence_concern": report.violence_concern,
190 "hazards": report.hazards,
191 })
192 .to_string()
193}
194
195#[cfg(test)]
196mod tests {
197 use super::*;
198
199 #[test]
200 fn disputed_diagnosis_uses_disputed_epistemic() {
201 let report = DisputedDiagnosisReport::new("Bipolar disorder");
202 let env = build_disputed_diagnosis_envelope(
203 &report,
204 "did:wf:owner",
205 "did:wf:owner",
206 1_700_000_000,
207 Some("abc".into()),
208 );
209 assert!(env.id.contains(":disputed_diagnosis:"));
210 assert_eq!(env.epistemic_status, EpistemicStatus::Disputed);
211 assert_eq!(
212 journal_kind_for_personal_record_id(&env.id),
213 Some("disputed_diagnosis")
214 );
215 }
216
217 #[test]
218 fn housing_safety_envelope_kind() {
219 let report = HousingSafetyReport {
220 id: "h1".into(),
221 dwelling_type: DwellingType::MobileShelter,
222 homelessness: true,
223 violence_concern: false,
224 hazards: Some("mould".into()),
225 location_notes: None,
226 notes: None,
227 };
228 let env = build_housing_safety_envelope(
229 &report,
230 "did:wf:owner",
231 "did:wf:owner",
232 1_700_000_000,
233 None,
234 );
235 assert!(env.id.contains(":housing_safety:"));
236 assert_eq!(env.sensitivity, SensitivityClass::Restricted);
237 let summary = housing_safety_summary(&report);
238 assert!(summary.contains("mobile_shelter") || summary.contains("MobileShelter"));
239 }
240}