MEDAI (Medication Engagement & Dose Adherence Intelligence) is the core healthcare infrastructure and intelligent API engine powering the MEDAI Medical Adherence Ecosystem. Engineered with Django 6.1, Django REST Framework (DRF), and Simple JWT, it bridges the critical gap between prescribed medical routines and everyday patient adherence.
The platform provides a unified backend for patient profiles, caregiver-patient links, customized medication routines, and granular daily dose tracking. Supported by an analytical risk scoring engine, MEDAI continuously evaluates patient consistency, calculates risk categories (low, moderate, high), and triggers automated escalations to linked caregivers whenever critical doses are missed.
Stop worrying about missed medications. Start empowering patients and caregivers with intelligent, automated medical adherence tracking.
| Guide | Description |
|---|---|
| βοΈ Setup Guide | Local installation, development environment, and configuration |
| ποΈ Architecture Overview | Project structure, design decisions, and technical architecture |
| π Authentication API | Token lifecycle (Register, Login, Me, Token Refresh) and security spec |
| π Medication & Dose Tracking | Routine schedules, daily dose records, and intake state machine |
| π Analytics & Risk Engine | Statistical adherence percentage calculation and risk thresholds |
| π¨ Alerts & Notifications | Automated caregiver escalation for missed doses and notifications |
| π Connecting Frontend | Integrating with the React 19 + Vite client application |
| π€ Contributing Guide | How to contribute templates, components, and improvements |
# Clone the repository
git clone https://github.com/04harithecoder/Medical-Adherence-App.git
cd Medical-Adherence-App/MEDAI_backend
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows (PowerShell / Command Prompt)
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate# Install required packages
pip install -r requirements.txt
# Copy sample environment configuration
# Windows
copy .env.example .env
# macOS / Linux
cp .env.example .envOpen .env in your code editor and choose either zero-config SQLite or MySQL:
DB_ENGINE=sqliteNo database server installation required. Uses local db.sqlite3 file out of the box.
-- Run in MySQL CLI / Workbench / phpMyAdmin:
CREATE DATABASE medai CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;Configure your .env:
DB_ENGINE=mysql
DB_NAME=medai
DB_USER=root
DB_PASSWORD=your_secure_password
DB_HOST=127.0.0.1
DB_PORT=3306# Apply Django migrations
python manage.py makemigrations
python manage.py migrate
# Create Admin Superuser (role automatically assigned to 'admin')
python manage.py createsuperuser
# Launch the development server
python manage.py runserver- REST API root:
http://localhost:8000/api/ - Django Admin panel:
http://localhost:8000/admin/
The backend strictly mirrors the Phase 1 ER design with a modular, decoupled domain-driven Django structure:
MEDAI_backend/
βββ accounts/ # User (custom, email-based), Patient, Caregiver & Link models
βββ medications/ # Medicine entities, dosages, and daily recurrence schedules
βββ doses/ # Individual dose tracking instances with action timestamps
βββ analytics/ # Statistical adherence computations & risk scoring
βββ alerts/ # Escalation alerts for repeated missed dosages
βββ notifications/ # Unified notifications for reminders, system updates & alerts
βββ medai_backend/ # Project settings, URL routers, and standardized responses
βββ manage.py # Django CLI orchestrator
erDiagram
USERS ||--o| PATIENTS : "1-to-1 Profile"
USERS ||--o| CAREGIVERS : "1-to-1 Profile"
CAREGIVERS }|--|{ PATIENTS : "CaregiverPatientLink"
PATIENTS ||--o{ MEDICATIONS : "manages"
MEDICATIONS ||--o{ MEDICATION_SCHEDULES : "has"
MEDICATION_SCHEDULES ||--o{ DOSE_RECORDS : "generates"
PATIENTS ||--o{ DOSE_RECORDS : "records"
PATIENTS ||--o{ ADHERENCE_ANALYSIS : "evaluates"
PATIENTS ||--o{ ALERTS : "triggers"
USERS ||--o{ NOTIFICATIONS : "receives"
MEDAI uses email-first authentication with JWT access + refresh token pairs.
| Method | Endpoint | Access | Purpose |
|---|---|---|---|
POST |
/api/auth/register |
Public | Register user (role must be patient or caregiver) |
POST |
/api/auth/login |
Public | Authenticate user & receive access + refresh tokens |
GET |
/api/auth/me |
Authenticated | Retrieve current user profile & details |
POST |
/api/auth/refresh |
Public | Refresh expired access token using valid refresh_token |
All API endpoints uniformly return a standard response envelope:
{
"success": true,
"data": {
"user": {
"id": 1,
"email": "patient@medai.health",
"full_name": "John Doe",
"role": "patient",
"phone": "+1234567890"
},
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
},
"message": "Account created successfully."
}{
"success": false,
"error": {
"code": "INVALID_CREDENTIALS",
"message": "Invalid email or password."
}
}The medication system guarantees patient consistency by breaking down prescriptions into distinct entities:
Medication: Stores medicine name, dosage strength, prescription dates, and doctor instructions.MedicationSchedule: Defines the precise routine (e.g.,08:00:00,14:00:00,20:00:00) and active recurrence days (ALL,MON,WED,FRI).DoseRecord: Concrete tracking logs for scheduled times. Supports four core lifecycle states:scheduled: Waiting for scheduled intake time.taken: Patient logged completion withaction_time.missed: Grace period expired without patient confirmation.skipped: Patient purposefully skipped (with optional note/reason).
The analytics module analyzes patient dosing patterns over daily, weekly, and monthly intervals:
| Risk Level | Adherence Range | System Behavior |
|---|---|---|
| π’ Low | β₯ 85% |
Positive reinforcement & streak badges awarded |
| π‘ Moderate | 70% β 84% |
Automated prompt notifications sent to patient |
| π΄ High | < 70% |
Immediate escalation alert dispatched to linked Caregiver |
- Alert Engine: Dispatches critical
repeated_missed_doserecords when 2 or more sequential doses are missed within a monitored window. - Notification Center: Unifies in-app push messages across 4 categories:
reminderβ Pre-scheduled dose promptsmissed_doseβ Immediate missed alert warningsalertβ Caregiver notification of patient risk level escalationsystemβ Account and platform announcements
The client frontend is built with React 19, Vite 6, and Tailwind CSS. To connect the client with this backend:
- Navigate to the
MEDAI_frontenddirectory. - Edit or create
.env:VITE_API_BASE_URL=http://localhost:8000/api
- Start the frontend client:
cd ../MEDAI_frontend npm install npm run dev
Contributions make the open-source healthcare community an incredible place to learn, inspire, and create! Any contributions you make are greatly appreciated.
- Fork the Project (
https://github.com/04harithecoder/Medical-Adherence-App) - Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
