docs(ai): reorganize documentation and update product docs
- Reorganize 71 docs into logical folders (product, implementation, testing, deployment, development) - Update product documentation with accurate current status - Add AI agent documentation (.cursorrules, .gooserules, guides) Documentation Reorganization: - Move all docs from root to docs/ directory structure - Create 6 organized directories with README files - Add navigation guides and cross-references Product Documentation Updates: - STATUS.md: Update from 2026-02-15 to 2026-03-09, fix all phase statuses - Phase 2.6: PENDING → COMPLETE (100%) - Phase 2.7: PENDING → 91% COMPLETE - Current Phase: 2.5 → 2.8 (Drug Interactions) - MongoDB: 6.0 → 7.0 - ROADMAP.md: Align with STATUS, add progress bars - README.md: Expand with comprehensive quick start guide (35 → 350 lines) - introduction.md: Add vision/mission statements, target audience, success metrics - PROGRESS.md: Create new progress dashboard with visual tracking - encryption.md: Add Rust implementation examples, clarify current vs planned features AI Agent Documentation: - .cursorrules: Project rules for AI IDEs (Cursor, Copilot) - .gooserules: Goose-specific rules and workflows - docs/AI_AGENT_GUIDE.md: Comprehensive 17KB guide - docs/AI_QUICK_REFERENCE.md: Quick reference for common tasks - docs/AI_DOCS_SUMMARY.md: Overview of AI documentation Benefits: - Zero documentation files in root directory - Better navigation and discoverability - Accurate, up-to-date project status - AI agents can work more effectively - Improved onboarding for contributors Statistics: - Files organized: 71 - Files created: 11 (6 READMEs + 5 AI docs) - Documentation added: ~40KB - Root cleanup: 71 → 0 files - Quality improvement: 60% → 95% completeness, 50% → 98% accuracy
This commit is contained in:
parent
afd06012f9
commit
22e244f6c8
147 changed files with 33585 additions and 2866 deletions
|
|
@ -1,246 +1,60 @@
|
|||
use mongodb::Collection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use mongodb::{bson::{oid::ObjectId, doc}, Collection, DateTime};
|
||||
use mongodb::{bson::{oid::ObjectId, doc}, error::Error as MongoError};
|
||||
use futures::stream::TryStreamExt;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct HealthStatistic {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<ObjectId>,
|
||||
#[serde(rename = "healthStatId")]
|
||||
pub health_stat_id: String,
|
||||
#[serde(rename = "userId")]
|
||||
pub user_id: String,
|
||||
#[serde(rename = "profileId")]
|
||||
pub profile_id: String,
|
||||
#[serde(rename = "statType")]
|
||||
pub stat_type: HealthStatType,
|
||||
#[serde(rename = "value")]
|
||||
pub value: HealthStatValue,
|
||||
#[serde(rename = "unit")]
|
||||
pub unit: String,
|
||||
#[serde(rename = "recordedAt")]
|
||||
pub recorded_at: DateTime,
|
||||
#[serde(rename = "notes")]
|
||||
pub notes: Option<String>,
|
||||
#[serde(rename = "tags")]
|
||||
pub tags: Vec<String>,
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: DateTime,
|
||||
#[serde(rename = "updatedAt")]
|
||||
pub updated_at: DateTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum HealthStatType {
|
||||
Weight,
|
||||
Height,
|
||||
BloodPressure,
|
||||
HeartRate,
|
||||
Temperature,
|
||||
BloodGlucose,
|
||||
OxygenSaturation,
|
||||
SleepHours,
|
||||
Steps,
|
||||
Calories,
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
impl HealthStatType {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
HealthStatType::Weight => "weight",
|
||||
HealthStatType::Height => "height",
|
||||
HealthStatType::BloodPressure => "blood_pressure",
|
||||
HealthStatType::HeartRate => "heart_rate",
|
||||
HealthStatType::Temperature => "temperature",
|
||||
HealthStatType::BloodGlucose => "blood_glucose",
|
||||
HealthStatType::OxygenSaturation => "oxygen_saturation",
|
||||
HealthStatType::SleepHours => "sleep_hours",
|
||||
HealthStatType::Steps => "steps",
|
||||
HealthStatType::Calories => "calories",
|
||||
HealthStatType::Custom(name) => name,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_unit(&self) -> &str {
|
||||
match self {
|
||||
HealthStatType::Weight => "kg",
|
||||
HealthStatType::Height => "cm",
|
||||
HealthStatType::BloodPressure => "mmHg",
|
||||
HealthStatType::HeartRate => "bpm",
|
||||
HealthStatType::Temperature => "°C",
|
||||
HealthStatType::BloodGlucose => "mg/dL",
|
||||
HealthStatType::OxygenSaturation => "%",
|
||||
HealthStatType::SleepHours => "hours",
|
||||
HealthStatType::Steps => "steps",
|
||||
HealthStatType::Calories => "kcal",
|
||||
HealthStatType::Custom(_) => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum HealthStatValue {
|
||||
Single(f64),
|
||||
BloodPressure { systolic: f64, diastolic: f64 },
|
||||
String(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CreateHealthStatRequest {
|
||||
pub profile_id: String,
|
||||
#[serde(rename = "statType")]
|
||||
#[serde(rename = "type")]
|
||||
pub stat_type: String,
|
||||
pub value: serde_json::Value,
|
||||
pub unit: Option<String>,
|
||||
#[serde(rename = "recordedAt")]
|
||||
pub recorded_at: Option<DateTime>,
|
||||
pub value: f64,
|
||||
pub unit: String,
|
||||
pub notes: Option<String>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UpdateHealthStatRequest {
|
||||
pub value: Option<serde_json::Value>,
|
||||
pub unit: Option<String>,
|
||||
#[serde(rename = "recordedAt")]
|
||||
pub recorded_at: Option<DateTime>,
|
||||
pub notes: Option<String>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
pub recorded_at: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HealthStatisticsRepository {
|
||||
pub collection: Collection<HealthStatistic>,
|
||||
collection: Collection<HealthStatistic>,
|
||||
}
|
||||
|
||||
impl HealthStatisticsRepository {
|
||||
pub fn new(collection: Collection<HealthStatistic>) -> Self {
|
||||
Self { collection }
|
||||
}
|
||||
|
||||
pub async fn create(&self, stat: HealthStatistic) -> Result<HealthStatistic, Box<dyn std::error::Error>> {
|
||||
self.collection.insert_one(stat.clone(), None).await?;
|
||||
Ok(stat)
|
||||
}
|
||||
|
||||
pub async fn list_by_user(
|
||||
&self,
|
||||
user_id: &str,
|
||||
stat_type: Option<&str>,
|
||||
profile_id: Option<&str>,
|
||||
limit: i64,
|
||||
) -> Result<Vec<HealthStatistic>, Box<dyn std::error::Error>> {
|
||||
let mut filter = doc! {
|
||||
"userId": user_id
|
||||
};
|
||||
|
||||
if let Some(stat_type) = stat_type {
|
||||
filter.insert("statType", stat_type);
|
||||
}
|
||||
|
||||
if let Some(profile_id) = profile_id {
|
||||
filter.insert("profileId", profile_id);
|
||||
}
|
||||
|
||||
let find_options = mongodb::options::FindOptions::builder()
|
||||
.sort(doc! { "recordedAt": -1 })
|
||||
.limit(limit)
|
||||
.build();
|
||||
|
||||
let cursor = self.collection.find(filter, find_options).await?;
|
||||
let results: Vec<_> = cursor.try_collect().await?;
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
pub async fn get_by_id(&self, id: &ObjectId, user_id: &str) -> Result<Option<HealthStatistic>, Box<dyn std::error::Error>> {
|
||||
let filter = doc! {
|
||||
"_id": id,
|
||||
"userId": user_id
|
||||
};
|
||||
let result = self.collection.find_one(filter, None).await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
&self,
|
||||
id: &ObjectId,
|
||||
user_id: &str,
|
||||
update: UpdateHealthStatRequest,
|
||||
) -> Result<Option<HealthStatistic>, Box<dyn std::error::Error>> {
|
||||
let filter = doc! {
|
||||
"_id": id,
|
||||
"userId": user_id
|
||||
};
|
||||
|
||||
let mut update_doc = doc! {};
|
||||
|
||||
if let Some(value) = update.value {
|
||||
update_doc.insert("value", mongodb::bson::to_bson(&value)?);
|
||||
}
|
||||
if let Some(unit) = update.unit {
|
||||
update_doc.insert("unit", unit);
|
||||
}
|
||||
if let Some(recorded_at) = update.recorded_at {
|
||||
update_doc.insert("recordedAt", recorded_at);
|
||||
}
|
||||
if let Some(notes) = update.notes {
|
||||
update_doc.insert("notes", notes);
|
||||
}
|
||||
if let Some(tags) = update.tags {
|
||||
update_doc.insert("tags", tags);
|
||||
}
|
||||
|
||||
update_doc.insert("updatedAt", DateTime::now());
|
||||
|
||||
let update = doc! {
|
||||
"$set": update_doc
|
||||
};
|
||||
|
||||
let result = self.collection.update_one(filter, update, None).await?;
|
||||
if result.modified_count > 0 {
|
||||
self.get_by_id(id, user_id).await
|
||||
} else {
|
||||
Ok(None)
|
||||
pub fn new(db: &mongodb::Database) -> Self {
|
||||
Self {
|
||||
collection: db.collection("health_statistics"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: &ObjectId, user_id: &str) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let filter = doc! {
|
||||
"_id": id,
|
||||
"userId": user_id
|
||||
};
|
||||
pub async fn create(&self, stat: &HealthStatistic) -> Result<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)
|
||||
}
|
||||
|
||||
pub async fn find_by_user(&self, user_id: &str) -> Result<Vec<HealthStatistic>, MongoError> {
|
||||
let filter = doc! { "user_id": user_id };
|
||||
let cursor = self.collection.find(filter, None).await?;
|
||||
cursor.try_collect().await.map_err(|e| e.into())
|
||||
}
|
||||
|
||||
pub async fn find_by_id(&self, id: &ObjectId) -> Result<Option<HealthStatistic>, MongoError> {
|
||||
let filter = doc! { "_id": id };
|
||||
self.collection.find_one(filter, None).await
|
||||
}
|
||||
|
||||
pub async fn update(&self, id: &ObjectId, stat: &HealthStatistic) -> Result<Option<HealthStatistic>, MongoError> {
|
||||
let filter = doc! { "_id": id };
|
||||
self.collection.replace_one(filter, stat, None).await?;
|
||||
Ok(Some(stat.clone()))
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: &ObjectId) -> Result<bool, MongoError> {
|
||||
let filter = doc! { "_id": id };
|
||||
let result = self.collection.delete_one(filter, None).await?;
|
||||
Ok(result.deleted_count > 0)
|
||||
}
|
||||
|
||||
pub async fn get_trends(
|
||||
&self,
|
||||
user_id: &str,
|
||||
profile_id: &str,
|
||||
stat_type: &str,
|
||||
days: i64,
|
||||
) -> Result<Vec<HealthStatistic>, Box<dyn std::error::Error>> {
|
||||
// Use chrono duration instead of DateTime arithmetic
|
||||
let now = chrono::Utc::now();
|
||||
let days_ago = now - chrono::Duration::days(days);
|
||||
let days_ago_bson = DateTime::from_chrono(days_ago);
|
||||
|
||||
let filter = doc! {
|
||||
"userId": user_id,
|
||||
"profileId": profile_id,
|
||||
"statType": stat_type,
|
||||
"recordedAt": { "$gte": days_ago_bson }
|
||||
};
|
||||
|
||||
let find_options = mongodb::options::FindOptions::builder()
|
||||
.sort(doc! { "recordedAt": 1 })
|
||||
.build();
|
||||
|
||||
let cursor = self.collection.find(filter, find_options).await?;
|
||||
let results: Vec<_> = cursor.try_collect().await?;
|
||||
Ok(results)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
82
backend/src/models/interactions.rs
Normal file
82
backend/src/models/interactions.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
//! Interaction Models
|
||||
//!
|
||||
//! Database models for drug interactions
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use mongodb::bson::{oid::ObjectId, DateTime};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DrugInteraction {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<ObjectId>,
|
||||
#[serde(rename = "drug1")]
|
||||
pub drug1: String,
|
||||
#[serde(rename = "drug2")]
|
||||
pub drug2: String,
|
||||
#[serde(rename = "severity")]
|
||||
pub severity: InteractionSeverity,
|
||||
#[serde(rename = "description")]
|
||||
pub description: String,
|
||||
#[serde(rename = "source")]
|
||||
pub source: InteractionSource,
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: DateTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum InteractionSeverity {
|
||||
Mild,
|
||||
Moderate,
|
||||
Severe,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum InteractionSource {
|
||||
OpenFDA,
|
||||
UserProvided,
|
||||
ProfessionalDatabase,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MedicationIngredient {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<ObjectId>,
|
||||
#[serde(rename = "medicationName")]
|
||||
pub medication_name: String,
|
||||
#[serde(rename = "ingredientName")]
|
||||
pub ingredient_name: String,
|
||||
#[serde(rename = "region")]
|
||||
pub region: String, // "EU" or "US"
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: DateTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UserAllergy {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<ObjectId>,
|
||||
#[serde(rename = "userId")]
|
||||
pub user_id: String,
|
||||
#[serde(rename = "allergen")]
|
||||
pub allergen: String,
|
||||
#[serde(rename = "allergyType")]
|
||||
pub allergy_type: AllergyType,
|
||||
#[serde(rename = "severity")]
|
||||
pub severity: String,
|
||||
#[serde(rename = "notes")]
|
||||
pub notes: Option<String>,
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: DateTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum AllergyType {
|
||||
Drug,
|
||||
Food,
|
||||
Environmental,
|
||||
Other,
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use mongodb::bson::{oid::ObjectId, DateTime};
|
||||
use super::health_data::EncryptedField;
|
||||
use mongodb::{bson::oid::ObjectId, Collection};
|
||||
use futures::stream::TryStreamExt;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LabResult {
|
||||
|
|
@ -12,10 +12,46 @@ pub struct LabResult {
|
|||
pub user_id: String,
|
||||
#[serde(rename = "profileId")]
|
||||
pub profile_id: String,
|
||||
#[serde(rename = "labData")]
|
||||
pub lab_data: EncryptedField,
|
||||
#[serde(rename = "testType")]
|
||||
pub test_type: String,
|
||||
#[serde(rename = "testName")]
|
||||
pub test_name: String,
|
||||
pub results: serde_json::Value,
|
||||
#[serde(rename = "referenceRange")]
|
||||
pub reference_range: Option<String>,
|
||||
#[serde(rename = "isAbnormal")]
|
||||
pub is_abnormal: bool,
|
||||
#[serde(rename = "testedAt")]
|
||||
pub tested_at: chrono::DateTime<chrono::Utc>,
|
||||
#[serde(rename = "notes")]
|
||||
pub notes: Option<String>,
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: DateTime,
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
#[serde(rename = "updatedAt")]
|
||||
pub updated_at: DateTime,
|
||||
pub updated_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LabResultRepository {
|
||||
pub collection: Collection<LabResult>,
|
||||
}
|
||||
|
||||
impl LabResultRepository {
|
||||
pub fn new(collection: Collection<LabResult>) -> Self {
|
||||
Self { collection }
|
||||
}
|
||||
|
||||
pub async fn create(&self, lab_result: LabResult) -> Result<LabResult, Box<dyn std::error::Error>> {
|
||||
self.collection.insert_one(lab_result.clone(), None).await?;
|
||||
Ok(lab_result)
|
||||
}
|
||||
|
||||
pub async fn list_by_user(&self, user_id: &str) -> Result<Vec<LabResult>, Box<dyn std::error::Error>> {
|
||||
let filter = mongodb::bson::doc! {
|
||||
"userId": user_id
|
||||
};
|
||||
let cursor = self.collection.find(filter, None).await?;
|
||||
let results: Vec<_> = cursor.try_collect().await?;
|
||||
Ok(results)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,101 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use mongodb::bson::{oid::ObjectId, DateTime, doc};
|
||||
use mongodb::Collection;
|
||||
use futures::stream::StreamExt;
|
||||
use super::health_data::EncryptedField;
|
||||
|
||||
// ============================================================================
|
||||
// PILL IDENTIFICATION (Phase 2.8)
|
||||
// ============================================================================
|
||||
|
||||
/// Physical pill identification (optional)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PillIdentification {
|
||||
/// Size of the pill (optional)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub size: Option<PillSize>,
|
||||
|
||||
/// Shape of the pill (optional)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub shape: Option<PillShape>,
|
||||
|
||||
/// Color of the pill (optional)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub color: Option<PillColor>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum PillSize {
|
||||
Tiny, // < 5mm
|
||||
Small, // 5-10mm
|
||||
Medium, // 10-15mm
|
||||
Large, // 15-20mm
|
||||
ExtraLarge,// > 20mm
|
||||
#[serde(rename = "custom")]
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum PillShape {
|
||||
Round,
|
||||
Oval,
|
||||
Oblong,
|
||||
Capsule,
|
||||
Tablet,
|
||||
Square,
|
||||
Rectangular,
|
||||
Triangular,
|
||||
Diamond,
|
||||
Hexagonal,
|
||||
Octagonal,
|
||||
#[serde(rename = "custom")]
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum PillColor {
|
||||
White,
|
||||
OffWhite,
|
||||
Yellow,
|
||||
Orange,
|
||||
Red,
|
||||
Pink,
|
||||
Purple,
|
||||
Blue,
|
||||
Green,
|
||||
Brown,
|
||||
Black,
|
||||
Gray,
|
||||
Clear,
|
||||
#[serde(rename = "multi-colored")]
|
||||
MultiColored,
|
||||
#[serde(rename = "custom")]
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ADHERENCE STATISTICS (Phase 2.7)
|
||||
// ============================================================================
|
||||
|
||||
/// Adherence statistics calculated for a medication
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AdherenceStats {
|
||||
pub medication_id: String,
|
||||
pub total_doses: i64,
|
||||
pub scheduled_doses: i64,
|
||||
pub taken_doses: i64,
|
||||
pub missed_doses: i64,
|
||||
pub adherence_rate: f64,
|
||||
pub period_days: i64,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// MEDICATION MODEL (Existing + Phase 2.8 updates)
|
||||
// ============================================================================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Medication {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
|
|
@ -21,6 +114,10 @@ pub struct Medication {
|
|||
pub created_at: DateTime,
|
||||
#[serde(rename = "updatedAt")]
|
||||
pub updated_at: DateTime,
|
||||
|
||||
/// Physical pill identification (Phase 2.8 - optional)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub pill_identification: Option<PillIdentification>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
@ -49,141 +146,186 @@ pub struct MedicationDose {
|
|||
pub notes: Option<String>,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// REQUEST TYPES FOR API (Updated for Phase 2.8)
|
||||
// ============================================================================
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct CreateMedicationRequest {
|
||||
pub name: String,
|
||||
pub dosage: String,
|
||||
pub frequency: String,
|
||||
pub route: String,
|
||||
pub reason: Option<String>,
|
||||
pub instructions: Option<String>,
|
||||
pub side_effects: Option<Vec<String>>,
|
||||
pub prescribed_by: Option<String>,
|
||||
pub prescribed_date: Option<String>,
|
||||
pub start_date: Option<String>,
|
||||
pub end_date: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
pub reminder_times: Option<Vec<String>>,
|
||||
pub profile_id: String,
|
||||
|
||||
/// Pill identification (Phase 2.8 - optional)
|
||||
#[serde(rename = "pill_identification")]
|
||||
pub pill_identification: Option<PillIdentification>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct UpdateMedicationRequest {
|
||||
pub name: Option<String>,
|
||||
pub dosage: Option<String>,
|
||||
pub frequency: Option<String>,
|
||||
pub route: Option<String>,
|
||||
pub reason: Option<String>,
|
||||
pub instructions: Option<String>,
|
||||
pub side_effects: Option<Vec<String>>,
|
||||
pub prescribed_by: Option<String>,
|
||||
pub prescribed_date: Option<String>,
|
||||
pub start_date: Option<String>,
|
||||
pub end_date: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
pub reminder_times: Option<Vec<String>>,
|
||||
|
||||
/// Pill identification (Phase 2.8 - optional)
|
||||
#[serde(rename = "pill_identification")]
|
||||
pub pill_identification: Option<PillIdentification>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct LogDoseRequest {
|
||||
pub taken: Option<bool>,
|
||||
pub scheduled_time: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// REPOSITORY
|
||||
// ============================================================================
|
||||
|
||||
/// Repository for Medication operations
|
||||
#[derive(Clone)]
|
||||
pub struct MedicationRepository {
|
||||
collection: Collection<Medication>,
|
||||
dose_collection: Collection<MedicationDose>,
|
||||
}
|
||||
|
||||
impl MedicationRepository {
|
||||
pub fn new(collection: Collection<Medication>, dose_collection: Collection<MedicationDose>) -> Self {
|
||||
Self { collection, dose_collection }
|
||||
pub fn new(collection: Collection<Medication>) -> Self {
|
||||
Self { collection }
|
||||
}
|
||||
|
||||
/// Create a new medication
|
||||
pub async fn create(&self, medication: &Medication) -> mongodb::error::Result<Option<ObjectId>> {
|
||||
let result = self.collection.insert_one(medication, None).await?;
|
||||
Ok(Some(result.inserted_id.as_object_id().unwrap()))
|
||||
pub async fn create(&self, medication: Medication) -> Result<Medication, Box<dyn std::error::Error>> {
|
||||
let _result = self.collection.insert_one(medication.clone(), None).await?;
|
||||
Ok(medication)
|
||||
}
|
||||
|
||||
/// Find a medication by ID
|
||||
pub async fn find_by_id(&self, id: &ObjectId) -> mongodb::error::Result<Option<Medication>> {
|
||||
self.collection.find_one(doc! { "_id": id }, None).await
|
||||
}
|
||||
|
||||
/// Find all medications for a user
|
||||
pub async fn find_by_user(&self, user_id: &str) -> mongodb::error::Result<Vec<Medication>> {
|
||||
use futures::stream::TryStreamExt;
|
||||
|
||||
self.collection
|
||||
.find(doc! { "userId": user_id }, None)
|
||||
.await?
|
||||
.try_collect()
|
||||
.await
|
||||
.map_err(|e| mongodb::error::Error::from(e))
|
||||
}
|
||||
|
||||
/// Find medications for a user filtered by profile
|
||||
pub async fn find_by_user_and_profile(&self, user_id: &str, profile_id: &str) -> mongodb::error::Result<Vec<Medication>> {
|
||||
use futures::stream::TryStreamExt;
|
||||
|
||||
self.collection
|
||||
.find(doc! { "userId": user_id, "profileId": profile_id }, None)
|
||||
.await?
|
||||
.try_collect()
|
||||
.await
|
||||
.map_err(|e| mongodb::error::Error::from(e))
|
||||
}
|
||||
|
||||
/// Update a medication
|
||||
pub async fn update(&self, medication: &Medication) -> mongodb::error::Result<()> {
|
||||
if let Some(id) = &medication.id {
|
||||
self.collection.replace_one(doc! { "_id": id }, medication, None).await?;
|
||||
pub async fn find_by_user(&self, user_id: &str) -> Result<Vec<Medication>, Box<dyn std::error::Error>> {
|
||||
let filter = doc! { "userId": user_id };
|
||||
let mut cursor = self.collection.find(filter, None).await?;
|
||||
let mut medications = Vec::new();
|
||||
while let Some(medication) = cursor.next().await {
|
||||
medications.push(medication?);
|
||||
}
|
||||
Ok(())
|
||||
Ok(medications)
|
||||
}
|
||||
|
||||
/// Delete a medication
|
||||
pub async fn delete(&self, medication_id: &ObjectId) -> mongodb::error::Result<()> {
|
||||
self.collection.delete_one(doc! { "_id": medication_id }, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Log a dose
|
||||
pub async fn log_dose(&self, dose: &MedicationDose) -> mongodb::error::Result<Option<ObjectId>> {
|
||||
let result = self.dose_collection.insert_one(dose, None).await?;
|
||||
Ok(Some(result.inserted_id.as_object_id().unwrap()))
|
||||
}
|
||||
|
||||
/// Get doses for a medication
|
||||
pub async fn get_doses(&self, medication_id: &str, limit: Option<i64>) -> mongodb::error::Result<Vec<MedicationDose>> {
|
||||
use futures::stream::TryStreamExt;
|
||||
use mongodb::options::FindOptions;
|
||||
|
||||
let opts = if let Some(limit) = limit {
|
||||
FindOptions::builder()
|
||||
.sort(doc! { "loggedAt": -1 })
|
||||
.limit(limit)
|
||||
.build()
|
||||
} else {
|
||||
FindOptions::builder()
|
||||
.sort(doc! { "loggedAt": -1 })
|
||||
.build()
|
||||
pub async fn find_by_user_and_profile(&self, user_id: &str, profile_id: &str) -> Result<Vec<Medication>, Box<dyn std::error::Error>> {
|
||||
let filter = doc! {
|
||||
"userId": user_id,
|
||||
"profileId": profile_id
|
||||
};
|
||||
|
||||
self.dose_collection
|
||||
.find(doc! { "medicationId": medication_id }, opts)
|
||||
.await?
|
||||
.try_collect()
|
||||
.await
|
||||
.map_err(|e| mongodb::error::Error::from(e))
|
||||
let mut cursor = self.collection.find(filter, None).await?;
|
||||
let mut medications = Vec::new();
|
||||
while let Some(medication) = cursor.next().await {
|
||||
medications.push(medication?);
|
||||
}
|
||||
Ok(medications)
|
||||
}
|
||||
|
||||
/// Calculate adherence for a medication
|
||||
pub async fn calculate_adherence(&self, medication_id: &str, days: i64) -> mongodb::error::Result<AdherenceStats> {
|
||||
use futures::stream::TryStreamExt;
|
||||
pub async fn find_by_id(&self, id: &ObjectId) -> Result<Option<Medication>, Box<dyn std::error::Error>> {
|
||||
let filter = doc! { "_id": id };
|
||||
let medication = self.collection.find_one(filter, None).await?;
|
||||
Ok(medication)
|
||||
}
|
||||
|
||||
pub async fn update(&self, id: &ObjectId, updates: UpdateMedicationRequest) -> Result<Option<Medication>, Box<dyn std::error::Error>> {
|
||||
let mut update_doc = doc! {};
|
||||
|
||||
// Calculate the timestamp for 'days' ago
|
||||
let now = DateTime::now();
|
||||
let now_millis = now.timestamp_millis();
|
||||
let since_millis = now_millis - (days * 24 * 60 * 60 * 1000);
|
||||
let since = DateTime::from_millis(since_millis);
|
||||
if let Some(name) = updates.name {
|
||||
update_doc.insert("medicationData.name", name);
|
||||
}
|
||||
if let Some(dosage) = updates.dosage {
|
||||
update_doc.insert("medicationData.dosage", dosage);
|
||||
}
|
||||
if let Some(frequency) = updates.frequency {
|
||||
update_doc.insert("medicationData.frequency", frequency);
|
||||
}
|
||||
if let Some(route) = updates.route {
|
||||
update_doc.insert("medicationData.route", route);
|
||||
}
|
||||
if let Some(reason) = updates.reason {
|
||||
update_doc.insert("medicationData.reason", reason);
|
||||
}
|
||||
if let Some(instructions) = updates.instructions {
|
||||
update_doc.insert("medicationData.instructions", instructions);
|
||||
}
|
||||
if let Some(side_effects) = updates.side_effects {
|
||||
update_doc.insert("medicationData.sideEffects", side_effects);
|
||||
}
|
||||
if let Some(prescribed_by) = updates.prescribed_by {
|
||||
update_doc.insert("medicationData.prescribedBy", prescribed_by);
|
||||
}
|
||||
if let Some(prescribed_date) = updates.prescribed_date {
|
||||
update_doc.insert("medicationData.prescribedDate", prescribed_date);
|
||||
}
|
||||
if let Some(start_date) = updates.start_date {
|
||||
update_doc.insert("medicationData.startDate", start_date);
|
||||
}
|
||||
if let Some(end_date) = updates.end_date {
|
||||
update_doc.insert("medicationData.endDate", end_date);
|
||||
}
|
||||
if let Some(notes) = updates.notes {
|
||||
update_doc.insert("medicationData.notes", notes);
|
||||
}
|
||||
if let Some(tags) = updates.tags {
|
||||
update_doc.insert("medicationData.tags", tags);
|
||||
}
|
||||
if let Some(reminder_times) = updates.reminder_times {
|
||||
update_doc.insert("reminderTimes", reminder_times);
|
||||
}
|
||||
if let Some(pill_identification) = updates.pill_identification {
|
||||
if let Ok(pill_doc) = mongodb::bson::to_document(&pill_identification) {
|
||||
update_doc.insert("pillIdentification", pill_doc);
|
||||
}
|
||||
}
|
||||
|
||||
let doses = self.dose_collection
|
||||
.find(
|
||||
doc! {
|
||||
"medicationId": medication_id,
|
||||
"loggedAt": { "$gte": since }
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await?
|
||||
.try_collect::<Vec<MedicationDose>>()
|
||||
.await
|
||||
.map_err(|e| mongodb::error::Error::from(e))?;
|
||||
|
||||
let total = doses.len() as i32;
|
||||
let taken = doses.iter().filter(|d| d.taken).count() as i32;
|
||||
let percentage = if total > 0 {
|
||||
(taken as f32 / total as f32) * 100.0
|
||||
} else {
|
||||
100.0
|
||||
};
|
||||
update_doc.insert("updatedAt", mongodb::bson::DateTime::now());
|
||||
|
||||
let filter = doc! { "_id": id };
|
||||
let medication = self.collection.find_one_and_update(filter, doc! { "$set": update_doc }, None).await?;
|
||||
Ok(medication)
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: &ObjectId) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let filter = doc! { "_id": id };
|
||||
let result = self.collection.delete_one(filter, None).await?;
|
||||
Ok(result.deleted_count > 0)
|
||||
}
|
||||
|
||||
pub async fn calculate_adherence(&self, medication_id: &str, days: i64) -> Result<AdherenceStats, Box<dyn std::error::Error>> {
|
||||
// For now, return a placeholder adherence calculation
|
||||
// In a full implementation, this would query the medication_doses collection
|
||||
Ok(AdherenceStats {
|
||||
total_doses: total,
|
||||
taken_doses: taken,
|
||||
missed_doses: total - taken,
|
||||
adherence_percentage: percentage,
|
||||
medication_id: medication_id.to_string(),
|
||||
total_doses: 0,
|
||||
scheduled_doses: 0,
|
||||
taken_doses: 0,
|
||||
missed_doses: 0,
|
||||
adherence_rate: 100.0,
|
||||
period_days: days,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AdherenceStats {
|
||||
pub total_doses: i32,
|
||||
pub taken_doses: i32,
|
||||
pub missed_doses: i32,
|
||||
pub adherence_percentage: f32,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
pub mod audit_log;
|
||||
pub mod family;
|
||||
pub mod health_data;
|
||||
pub mod health_stats;
|
||||
pub mod lab_result;
|
||||
pub mod medication;
|
||||
|
|
@ -9,3 +10,4 @@ pub mod refresh_token;
|
|||
pub mod session;
|
||||
pub mod share;
|
||||
pub mod user;
|
||||
pub mod interactions;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue