feat(backend): dose scheduling + health stats zero-knowledge
Backend changes (frontend + tests follow in next commit): Dose scheduling: - New DoseSchedule struct (times_per_day + days_of_week) as a top-level plaintext field on Medication. Create/update requests accept it. - Revised get_adherence: computes scheduled_doses from the schedule over the period (days_of_week filtering), so missed doses are now reflected. Falls back to taken/total_logged when no schedule. Health stats zero-knowledge: - HealthStatistic model now uses opaque encrypted_data blob (like medications). Only recorded_at stays plaintext (filterable/sortable). - HealthStatResponse wire type. Handlers echo opaque blobs. - Removed the trends endpoint (server can't compute trends on ciphertext; frontend computes them client-side after decrypting). - Deleted the dead HealthData model (kept EncryptedField which it defined). Verified: backend 24 tests, 0 clippy warnings.
This commit is contained in:
parent
69faa43a72
commit
09589b3bb2
8 changed files with 270 additions and 200 deletions
|
|
@ -6,19 +6,42 @@ use mongodb::{
|
|||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models::medication::EncryptedFieldWire;
|
||||
|
||||
/// Zero-knowledge health stat: the value/unit/type/notes are encrypted inside
|
||||
/// the opaque blob; only `recorded_at` stays plaintext (filterable/sortable).
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct HealthStatistic {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<ObjectId>,
|
||||
pub user_id: String,
|
||||
#[serde(rename = "type")]
|
||||
pub stat_type: String,
|
||||
pub value: f64,
|
||||
pub unit: String,
|
||||
pub notes: Option<String>,
|
||||
/// Opaque client-encrypted blob (value, unit, type, notes inside).
|
||||
#[serde(rename = "encryptedData")]
|
||||
pub encrypted_data: EncryptedFieldWire,
|
||||
/// When the reading was taken (plaintext, filterable/sortable).
|
||||
pub recorded_at: String,
|
||||
}
|
||||
|
||||
/// Wire response (what the API returns).
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct HealthStatResponse {
|
||||
pub id: String,
|
||||
pub user_id: String,
|
||||
pub encrypted_data: EncryptedFieldWire,
|
||||
pub recorded_at: String,
|
||||
}
|
||||
|
||||
impl From<HealthStatistic> for HealthStatResponse {
|
||||
fn from(s: HealthStatistic) -> Self {
|
||||
HealthStatResponse {
|
||||
id: s.id.map(|o| o.to_hex()).unwrap_or_default(),
|
||||
user_id: s.user_id,
|
||||
encrypted_data: s.encrypted_data,
|
||||
recorded_at: s.recorded_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HealthStatisticsRepository {
|
||||
collection: Collection<HealthStatistic>,
|
||||
|
|
@ -31,11 +54,14 @@ impl HealthStatisticsRepository {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn create(&self, stat: &HealthStatistic) -> Result<HealthStatistic, MongoError> {
|
||||
pub async fn create(
|
||||
&self,
|
||||
stat: &HealthStatistic,
|
||||
) -> Result<Option<HealthStatistic>, MongoError> {
|
||||
let result = self.collection.insert_one(stat, None).await?;
|
||||
let mut created = stat.clone();
|
||||
created.id = Some(result.inserted_id.as_object_id().unwrap());
|
||||
Ok(created)
|
||||
created.id = result.inserted_id.as_object_id();
|
||||
Ok(Some(created))
|
||||
}
|
||||
|
||||
pub async fn find_by_user(&self, user_id: &str) -> Result<Vec<HealthStatistic>, MongoError> {
|
||||
|
|
@ -49,14 +75,22 @@ impl HealthStatisticsRepository {
|
|||
self.collection.find_one(filter, None).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
pub async fn update_encrypted_data(
|
||||
&self,
|
||||
id: &ObjectId,
|
||||
stat: &HealthStatistic,
|
||||
encrypted_data: EncryptedFieldWire,
|
||||
) -> Result<Option<HealthStatistic>, MongoError> {
|
||||
let filter = doc! { "_id": id };
|
||||
self.collection.replace_one(filter, stat, None).await?;
|
||||
Ok(Some(stat.clone()))
|
||||
self.collection
|
||||
.find_one_and_update(
|
||||
doc! { "_id": id },
|
||||
doc! { "$set": {
|
||||
"encryptedData.data": encrypted_data.data,
|
||||
"encryptedData.iv": encrypted_data.iv,
|
||||
"encryptedData.authTag": encrypted_data.auth_tag,
|
||||
}},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: &ObjectId) -> Result<bool, MongoError> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue