1use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::record::{EpistemicStatus, EvidenceType, RecordEnvelope, SensitivityClass};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum ConditionStatus {
12 Active,
13 Resolved,
14 InRemission,
15}
16
17impl Default for ConditionStatus {
18 fn default() -> Self {
19 Self::Active
20 }
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25pub struct ConditionReport {
26 pub id: String,
27 pub label: String,
28 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub icd10_code: Option<String>,
30 #[serde(default)]
31 pub status: ConditionStatus,
32 #[serde(default, skip_serializing_if = "Option::is_none")]
33 pub notes: Option<String>,
34}
35
36impl ConditionReport {
37 pub fn new(label: impl Into<String>) -> Self {
39 Self {
40 id: Uuid::new_v4().to_string(),
41 label: label.into(),
42 icd10_code: None,
43 status: ConditionStatus::Active,
44 notes: None,
45 }
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51pub struct AllergyReport {
52 pub id: String,
53 pub substance: String,
54 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub reaction: Option<String>,
56 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub severity: Option<String>,
58 #[serde(default, skip_serializing_if = "Option::is_none")]
59 pub notes: Option<String>,
60}
61
62impl AllergyReport {
63 pub fn new(substance: impl Into<String>) -> Self {
65 Self {
66 id: Uuid::new_v4().to_string(),
67 substance: substance.into(),
68 reaction: None,
69 severity: None,
70 notes: None,
71 }
72 }
73}
74
75pub fn condition_record_id(uuid: &str) -> String {
77 format!("urn:wellfair:condition:{uuid}")
78}
79
80pub fn allergy_record_id(uuid: &str) -> String {
82 format!("urn:wellfair:allergy:{uuid}")
83}
84
85pub fn journal_kind_for_record_id(record_id: &str) -> &'static str {
87 if record_id.contains(":condition:") {
88 "condition"
89 } else if record_id.contains(":allergy:") {
90 "allergy"
91 } else if record_id.contains(":disputed_diagnosis:") {
92 "disputed_diagnosis"
93 } else if record_id.contains(":housing_safety:") {
94 "housing_safety"
95 } else if record_id.contains(":life_event:") {
96 "life_event"
97 } else if record_id.contains(":welfare_case:") {
98 "welfare_case"
99 } else if record_id.contains(":case_task:") {
100 "case_task"
101 } else if record_id.contains(":wellbeing_assessment:") {
102 "wellbeing_assessment"
103 } else if record_id.contains(":wellbeing_observation:") {
104 "wellbeing_observation"
105 } else if record_id.contains(":therapy_note:") {
106 "therapy_note"
107 } else if record_id.contains(":sanctuary_note:") {
108 "sanctuary_note"
109 } else if record_id.contains(":medication:") {
110 "medication"
111 } else if record_id.contains(":med_admin:") {
112 "med_administration"
113 } else if record_id.contains(":diet:") {
114 "diet"
115 } else if record_id.contains(":ledger_entry:") {
116 "ledger_entry"
117 } else if record_id.contains(":credential:") {
118 "credential"
119 } else if record_id.contains(":project_membership:") {
120 "project_membership"
121 } else if record_id.contains(":project:") {
122 "project"
123 } else if record_id.contains(":contribution:") {
124 "contribution"
125 } else if record_id.contains(":clinical_report:") {
126 "clinical_report"
127 } else if record_id.contains(":clinical_attachment:") {
128 "clinical_attachment"
129 } else if record_id.contains(":assistance_need:") {
130 "assistance_need"
131 } else if record_id.contains(":welfare_stream:") {
132 "welfare_stream"
133 } else if record_id.contains(":authority_attestation:") {
134 "authority_attestation"
135 } else if record_id.contains(":government_letter:") {
136 "government_letter"
137 } else if record_id.contains(":guardianship_proposal:") {
138 "guardianship_proposal"
139 } else if record_id.contains(":guardianship_vote:") {
140 "guardianship_vote"
141 } else if record_id.contains(":agency-delegation:") {
142 "agency_delegation"
143 } else if record_id.contains(":work_item_status:") {
144 "work_item_status"
145 } else if record_id.contains(":work_item:") {
146 "work_item"
147 } else if record_id.contains(":weight:") {
148 "weight"
149 } else if record_id.contains(":sleep:") {
150 "sleep"
151 } else if record_id.contains(":steps:") {
152 "steps"
153 } else if record_id.contains(":heart_rate:") {
154 "heart_rate"
155 } else {
156 "record"
157 }
158}
159
160fn self_reported_envelope(
161 id: &str,
162 owner_did: &str,
163 author_did: &str,
164 asserted_unix: u32,
165 blob_hash: Option<String>,
166) -> RecordEnvelope {
167 RecordEnvelope {
168 id: id.to_string(),
169 owner_did: owner_did.to_string(),
170 author_did: author_did.to_string(),
171 proxy_did: None,
172 epistemic_status: EpistemicStatus::Asserted,
173 evidence_type: EvidenceType::SelfReported,
174 sensitivity: SensitivityClass::Restricted,
175 asserted_time_unix: asserted_unix,
176 asserted_instant: None,
177 valid_time_start_unix: Some(asserted_unix),
178 valid_time_start_instant: None,
179 valid_time_end_unix: None,
180 valid_time_end_instant: None,
181 predecessor_id: None,
182 blob_hash,
183 tombstone: false,
184 }
185}
186
187pub fn build_condition_envelope(
189 report: &ConditionReport,
190 owner_did: &str,
191 author_did: &str,
192 asserted_unix: u32,
193 blob_hash: Option<String>,
194) -> RecordEnvelope {
195 let id = condition_record_id(&report.id);
196 self_reported_envelope(&id, owner_did, author_did, asserted_unix, blob_hash)
197}
198
199pub fn build_allergy_envelope(
201 report: &AllergyReport,
202 owner_did: &str,
203 author_did: &str,
204 asserted_unix: u32,
205 blob_hash: Option<String>,
206) -> RecordEnvelope {
207 let id = allergy_record_id(&report.id);
208 self_reported_envelope(&id, owner_did, author_did, asserted_unix, blob_hash)
209}
210
211pub fn condition_summary(report: &ConditionReport) -> String {
213 serde_json::json!({
214 "label": report.label,
215 "status": report.status,
216 "icd10": report.icd10_code,
217 })
218 .to_string()
219}
220
221pub fn allergy_summary(report: &AllergyReport) -> String {
223 serde_json::json!({
224 "substance": report.substance,
225 "reaction": report.reaction,
226 "severity": report.severity,
227 })
228 .to_string()
229}
230
231#[cfg(test)]
232mod tests {
233 use super::*;
234
235 #[test]
236 fn condition_envelope_is_self_reported_restricted() {
237 let report = ConditionReport::new("Type 2 diabetes");
238 let env = build_condition_envelope(
239 &report,
240 "did:wf:owner",
241 "did:wf:owner",
242 1_700_000_000,
243 Some("abc123".into()),
244 );
245 assert!(env.id.contains(":condition:"));
246 assert_eq!(env.evidence_type, EvidenceType::SelfReported);
247 assert_eq!(env.sensitivity, SensitivityClass::Restricted);
248 assert_eq!(journal_kind_for_record_id(&env.id), "condition");
249 }
250
251 #[test]
252 fn allergy_envelope_kind() {
253 let report = AllergyReport::new("Peanuts");
254 let env =
255 build_allergy_envelope(&report, "did:wf:owner", "did:wf:owner", 1_700_000_000, None);
256 assert!(env.id.contains(":allergy:"));
257 assert_eq!(journal_kind_for_record_id(&env.id), "allergy");
258 }
259
260 #[test]
261 fn summaries_include_primary_fields() {
262 let cond = ConditionReport {
263 id: "c1".into(),
264 label: "Asthma".into(),
265 icd10_code: Some("J45".into()),
266 status: ConditionStatus::Active,
267 notes: None,
268 };
269 let summary = condition_summary(&cond);
270 assert!(summary.contains("Asthma"));
271 assert!(summary.contains("J45"));
272
273 let allergy = AllergyReport {
274 id: "a1".into(),
275 substance: "Latex".into(),
276 reaction: Some("Rash".into()),
277 severity: Some("moderate".into()),
278 notes: None,
279 };
280 let allergy_summary = allergy_summary(&allergy);
281 assert!(allergy_summary.contains("Latex"));
282 assert!(allergy_summary.contains("Rash"));
283 }
284}