Skip to main content

qualia_client_core/wellfair/api/
pwa.rs

1//! PWA package & publish
2
3use super::super::journal::JournalEntry;
4use wellfare_core::finance::{
5    build_ledger_entry_envelope, derived_balance, ledger_entry_summary,
6    parse_ledger_entry_summary, BalanceReport, LedgerEntry,
7};
8use wellfare_core::life_records::{
9    build_case_task_envelope, build_life_event_envelope, build_welfare_case_envelope,
10    case_task_summary, life_event_summary, welfare_case_summary, CaseTaskReport, LifeEventReport,
11    WelfareCaseReport,
12};
13use wellfare_core::mental_wellbeing::{
14    build_therapy_note_envelope, build_wellbeing_observation_envelope, therapy_note_summary,
15    wellbeing_observation_summary, TherapyNote, WellbeingObservation,
16};
17
18use super::*;
19
20impl WebizenHostApi {
21    // --- WP2: Package & Publish a qapp as an installable PWA bundle (companion-PWA P0/WP2) ---
22
23    /// Author a qapp from discrete fields and write its installable PWA bundle to `target_dir`.
24    /// Returns the written (bundle-relative) file paths. Serving the bundle over a secure origin so
25    /// a phone can install it is a later stage (P1); this produces the artifact.
26    #[cfg(not(target_arch = "wasm32"))]
27    #[allow(clippy::too_many_arguments)]
28    pub fn publish_qapp_pwa(
29        &self,
30        target_dir: &str,
31        id: &str,
32        name: &str,
33        kind: &str,
34        description: &str,
35        capabilities_csv: &str,
36        wasm_filename: &str,
37    ) -> Result<Vec<String>, String> {
38        let manifest = super::super::qapp_publish::build_manifest(
39            id,
40            name,
41            kind,
42            description,
43            capabilities_csv,
44            wasm_filename,
45        );
46        super::super::qapp_publish::write_pwa_bundle(std::path::Path::new(target_dir), &manifest)
47    }
48
49    /// Adds life-event.
50    pub fn add_life_event(&mut self, report: &LifeEventReport) -> Result<JournalEntry, String> {
51        let payload = serde_json::to_string(report).map_err(|e| e.to_string())?;
52        let hash = Self::payload_hash_hex(&payload);
53        let asserted = Self::now_unix() as u32;
54        let envelope = build_life_event_envelope(
55            report,
56            &self.owner_did,
57            &self.author_did,
58            asserted,
59            Some(hash),
60        );
61        let summary = life_event_summary(report);
62        self.submit_record_with_summary(QAPP_LIFE, envelope, SOURCE_LIFE, Some(summary))?;
63        self.latest_journal_entry()
64    }
65
66    /// Adds welfare-case.
67    pub fn add_welfare_case(&mut self, report: &WelfareCaseReport) -> Result<JournalEntry, String> {
68        let payload = serde_json::to_string(report).map_err(|e| e.to_string())?;
69        let hash = Self::payload_hash_hex(&payload);
70        let asserted = Self::now_unix() as u32;
71        let envelope = build_welfare_case_envelope(
72            report,
73            &self.owner_did,
74            &self.author_did,
75            asserted,
76            Some(hash),
77        );
78        let summary = welfare_case_summary(report);
79        self.submit_record_with_summary(QAPP_LIFE, envelope, SOURCE_LIFE, Some(summary))?;
80        self.latest_journal_entry()
81    }
82
83    /// Adds case-task.
84    pub fn add_case_task(&mut self, report: &CaseTaskReport) -> Result<JournalEntry, String> {
85        let payload = serde_json::to_string(report).map_err(|e| e.to_string())?;
86        let hash = Self::payload_hash_hex(&payload);
87        let asserted = Self::now_unix() as u32;
88        let envelope = build_case_task_envelope(
89            report,
90            &self.owner_did,
91            &self.author_did,
92            asserted,
93            Some(hash),
94        );
95        let summary = case_task_summary(report);
96        self.submit_record_with_summary(QAPP_LIFE, envelope, SOURCE_LIFE, Some(summary))?;
97        self.latest_journal_entry()
98    }
99
100    /// Adds wellbeing-observation.
101    pub fn add_wellbeing_observation(
102        &mut self,
103        report: &WellbeingObservation,
104    ) -> Result<JournalEntry, String> {
105        let payload = serde_json::to_string(report).map_err(|e| e.to_string())?;
106        let hash = Self::payload_hash_hex(&payload);
107        let asserted = Self::now_unix() as u32;
108        let envelope = build_wellbeing_observation_envelope(
109            report,
110            &self.owner_did,
111            &self.author_did,
112            asserted,
113            Some(hash),
114        );
115        let summary = wellbeing_observation_summary(report);
116        self.submit_record_with_summary(QAPP_WELLBEING, envelope, SOURCE_WELLBEING, Some(summary))?;
117        self.latest_journal_entry()
118    }
119
120    /// Adds therapy-note.
121    pub fn add_therapy_note(&mut self, report: &TherapyNote) -> Result<JournalEntry, String> {
122        let payload = serde_json::to_string(report).map_err(|e| e.to_string())?;
123        let hash = Self::payload_hash_hex(&payload);
124        let asserted = Self::now_unix() as u32;
125        let envelope = build_therapy_note_envelope(
126            report,
127            &self.owner_did,
128            &self.author_did,
129            asserted,
130            Some(hash),
131        );
132        let summary = therapy_note_summary(report);
133        self.submit_record_with_summary(QAPP_WELLBEING, envelope, SOURCE_WELLBEING, Some(summary))?;
134        self.latest_journal_entry()
135    }
136
137    /// Latest journal entry.
138    pub(crate) fn latest_journal_entry(&self) -> Result<JournalEntry, String> {
139        self.vault
140            .list_health_records(1)
141            .map_err(|e| e.to_string())?
142            .into_iter()
143            .next()
144            .ok_or_else(|| "record committed but not found in journal".to_string())
145    }
146
147    /// Record a signed personal-finance ledger entry (Phase 5 / FIN-01..).
148    pub fn add_ledger_entry(&mut self, entry: &LedgerEntry) -> Result<JournalEntry, String> {
149        let payload = serde_json::to_string(entry).map_err(|e| e.to_string())?;
150        let hash = Self::payload_hash_hex(&payload);
151        let asserted = Self::now_unix() as u32;
152        let envelope = build_ledger_entry_envelope(
153            entry,
154            &self.owner_did,
155            &self.author_did,
156            asserted,
157            Some(hash),
158        );
159        let summary = ledger_entry_summary(entry);
160        self.submit_record_with_summary(QAPP_FINANCE, envelope, SOURCE_FINANCE, Some(summary))?;
161        self.finalize_batch().ok();
162        self.latest_journal_entry()
163    }
164
165    /// List ledger journal rows (most recent first).
166    pub fn list_ledger_entries(&self, limit: usize) -> Result<Vec<JournalEntry>, String> {
167        self.list_journal_by_kind("ledger_entry", limit)
168    }
169
170    /// Derived balance across the ledger. Balances are a pure derivation over the
171    /// unique-entry-id set, so a duplicate or replayed commit can never move money (ยง17).
172    pub fn ledger_balance(&self, limit: usize) -> Result<BalanceReport, String> {
173        let rows = self.list_ledger_entries(limit)?;
174        let mut entries = Vec::with_capacity(rows.len());
175        for row in rows {
176            if let Some(ref summary) = row.summary {
177                if let Some(entry) = parse_ledger_entry_summary(
178                    row.id.clone(),
179                    summary,
180                    row.asserted_instant.to_unix_secs() as u32,
181                ) {
182                    entries.push(entry);
183                }
184            }
185        }
186        Ok(derived_balance(&entries))
187    }
188}