# garehmalika/medassit architecture

> Architecture diagram of the GitHub repository garehmalika/medassit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/garehmalika/medassit
- Repository: https://github.com/garehmalika/medassit

## Overview

##### Project Type and Purpose
- This is a full-stack healthcare/telemedicine application.
- The Frontend is built in Next.js (React) and provides user interfaces for authentication, dashboards, patient management, consultations, prescriptions, certificates, billing, analytics, and settings.
- The Backend is a Flask monolith exposing REST/AI endpoints, orchestrating business logic, interacting with a relational database, generating PDFs, sending emails, handling payments, and offloading long-running tasks to Celery.

##### File Structure Analysis
- Top-level: two folders “frontend” and “Backend.”
- Frontend:
- src/app – Next.js pages/routes, relying on Next’s App Router (server+client components).
- src/components, src/services, src/hooks, src/context – UI components, API wrappers, custom hooks, global state.
- Tailwind & global CSS, Next config, ESLint, environment variables.
- Backend:
- app/run.py initializes Flask, extensions, middleware (auth, CORS, rate limit).
- app/routes – REST endpoints for AI, auth, patients, consultations, prescriptions, certificates, analytics, documents, users.
- app/services & app/ai – Business logic and GPT/Whisper integrations.
- app/database, app/migrations – PostgreSQL schema, Alembic migrations, seed scripts.
- app/email_service, payment_service, pdf_service – External integrations.
- Celery setup (celery_worker.py) for async tasks (email, PDF generation, billing).
- Deployment: Dockerfiles (backend/frontend), docker-compose; setup and coding standards guides.
- Tests covering auth, AI services, PDF generation, CRUD operations.

##### Key Architectural Patterns
- Layered architecture in the backend:
- Routes → Middleware → Services → Models → Database.
- Separate AI “service” layer interacting with OpenAI APIs.
- Asynchronous task queue (Celery + Redis/RabbitMQ).
- Frontend separation of concerns:
- Pages vs. shared UI components vs. API service layers.
- Context for global state (authentication, notifications).

a. Frontend (Next.js)
- UI Layer (React components, pages)
- Client-side state (AuthContext, NotificationContext)
- API Service Layer (authService, consultationService, etc.)
b. Backend (Flask)
- HTTP API Endpoints
- Middleware (Auth, CORS, Rate Limit)
- Business Logic Services (auth_service, consultation_service, etc.)
- AI Services (GPT, Whisper wrappers)
- PDF Generation Service
- Email Service
- Payment Service
- Celery Worker for background jobs
c. Database
- PostgreSQL (with Alembic migrations)
d. Message Broker
- Redis or RabbitMQ (for Celery)
e. External Services
- OpenAI (GPT, Whisper)
- SMTP provider (for email_service)
- Payment gateway (Stripe/PayPal via payment_service)
- Object storage (if used by storage_service)

##### Relationships and Data Flow
- Frontend ↔ Backend: HTTP(S) API calls (fetch/Axios).
- Backend → Database: SQLAlchemy ORM.
- Backend → Celery Broker: push tasks for async processing.
- Celery Worker → Database / External Services: update records, send emails, generate/store PDFs.
- Backend → External AI APIs: on-demand prompts and transcriptions.
- Backend → Payment Gateway: subscription billing and webhooks.

##### Final Recommendations
- Asynchronous flows separately from synchronous HTTP calls.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (Next.js/React)" 
        direction TB
        UI["UI Layer<br/>(pages/components)"]:::frontend
        Contexts["Client-Side State<br/>(Auth, Notifications, Theme)"]:::frontend
        API["API Service Layer<br/>(authService, etc.)"]:::frontend
    end

    %% Backend API Layer
    subgraph "Backend API (Flask)" 
        direction TB
        Routes["HTTP API Endpoints"]:::backend
        Middleware["Middleware<br/>(Auth, CORS, Rate Limit, Error)"]:::backend
        Services["Business Logic Services"]:::backend
        AI["AI Services<br/>(GPT, Whisper, Prompt)"]:::backend
        PDF["PDF Generation Services"]:::backend
        Email["Email Service"]:::backend
        Payment["Payment Service"]:::backend
        Storage["Storage Service"]:::backend
    end

    %% Asynchronous Worker
    subgraph "Celery Worker (Async Tasks)" 
        direction TB
        Broker["Message Broker<br/>(Redis/RabbitMQ)"]:::background
        Worker["Celery Worker"]:::background
    end

    %% Data Layer
    DB["PostgreSQL Database<br/>(SQLAlchemy, Alembic)"]:::database

    %% External Services
    subgraph "External Services" 
        direction TB
        OpenAI["OpenAI APIs (GPT, Whisper)"]:::external
        SMTP["SMTP Provider"]:::external
        PaymentGW["Payment Gateway"]:::external
        ObjStorage["Object Storage"]:::external
    end

    %% Connections
    UI -->|"calls"| API
    API -->|"HTTP requests"| Routes
    Routes -->|"passes through"| Middleware
    Middleware -->|"invokes"| Services
    Services -->|"reads/writes"| DB
    Services -->|"calls"| AI
    Services -->|"calls"| PDF
    Services -->|"uses"| Email
    Services -->|"uses"| Payment
    Services -->|"uses"| Storage

    Services -->|"push task"| Broker
    Broker -->|"dispatch"| Worker
    Worker -->|"updates"| DB
    Worker -->|"generates"| PDF
    Worker -->|"sends"| SMTP
    Worker -->|"processes billing"| PaymentGW
    Worker -->|"stores"| ObjStorage

    Services -->|"bulk external AI"| OpenAI
    Payment -->|"webhooks"| PaymentGW

    %% Click Events Frontend
    click UI "https://github.com/garehmalika/medassit/tree/main/frontend/src/app"
    click UI "https://github.com/garehmalika/medassit/tree/main/frontend/src/components"
    click Contexts "https://github.com/garehmalika/medassit/blob/main/frontend/src/context/AuthContext.tsx"
    click Contexts "https://github.com/garehmalika/medassit/blob/main/frontend/src/context/NotificationContext.tsx"
    click Contexts "https://github.com/garehmalika/medassit/blob/main/frontend/src/context/ThemeContext.tsx"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/authService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/consultationService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/patientService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/prescriptionService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/certificateService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/documentService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/aiService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/whisperService.ts"
    click API "https://github.com/garehmalika/medassit/blob/main/frontend/src/services/api.ts"

    %% Click Events Backend
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/auth_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/patient_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/consultation_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/prescription_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/certificate_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/document_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/analytics_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/ai_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/routes/user_routes.py"
    click Routes "https://github.com/garehmalika/medassit/blob/main/Backend/app/run.py"
    click Middleware "https://github.com/garehmalika/medassit/blob/main/Backend/app/middleware/auth_middleware.py"
    click Middleware "https://github.com/garehmalika/medassit/blob/main/Backend/app/middleware/cors_middleware.py"
    click Middleware "https://github.com/garehmalika/medassit/blob/main/Backend/app/middleware/rate_limit_middleware.py"
    click Middleware "https://github.com/garehmalika/medassit/blob/main/Backend/app/middleware/error_middleware.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/auth_service.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/patient_service.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/consultation_service.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/prescription_service.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/certificate_service.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/document_service.py"
    click Services "https://github.com/garehmalika/medassit/blob/main/Backend/app/services/user_service.py"
    click AI "https://github.com/garehmalika/medassit/blob/main/Backend/app/ai/gpt_service.py"
    click AI "https://github.com/garehmalika/medassit/blob/main/Backend/app/ai/whisper_service.py"
    click AI "https://github.com/garehmalika/medassit/blob/main/Backend/app/ai/consultation_ai.py"
    click AI "https://github.com/garehmalika/medassit/blob/main/Backend/app/ai/prescription_ai.py"
    click AI "https://github.com/garehmalika/medassit/blob/main/Backend/app/ai/certificate_ai.py"
    click AI "https://github.com/garehmalika/medassit/blob/main/Backend/app/ai/prompt_templates.py"
    click PDF "https://github.com/garehmalika/medassit/blob/main/Backend/app/pdf/pdf_service.py"
    click PDF "https://github.com/garehmalika/medassit/blob/main/Backend/app/pdf/consultation_pdf.py"
    click PDF "https://github.com/garehmalika/medassit/blob/main/Backend/app/pdf/prescription_pdf.py"
    click PDF "https://github.com/garehmalika/medassit/blob/main/Backend/app/pdf/certificate_pdf.py"
    click PDF "https://github.com/garehmalika/medassit/tree/main/Backend/app/pdf/templates"
    click Email "https://github.com/garehmalika/medassit/blob/main/Backend/app/email_service.py"
    click Payment "https://github.com/garehmalika/medassit/blob/main/Backend/app/payment_service.py"
    click Storage "https://github.com/garehmalika/medassit/blob/main/Backend/app/storage_service.py"
    click Worker "https://github.com/garehmalika/medassit/blob/main/Backend/app/celery_worker.py"
    click DB "https://github.com/garehmalika/medassit/blob/main/Backend/app/database.py"
    click DB "https://github.com/garehmalika/medassit/tree/main/Backend/app/models/"
    click DB "https://github.com/garehmalika/medassit/tree/main/Backend/app/migrations/"
    click DB "https://github.com/garehmalika/medassit/blob/main/Backend/app/database/init.sql"
    click DB "https://github.com/garehmalika/medassit/blob/main/Backend/app/database/backup_script.sh"
    click DB "https://github.com/garehmalika/medassit/blob/main/Backend/app/database/seed_medications.sql"
    click Worker "https://github.com/garehmalika/medassit/blob/main/Backend/app/config.py"
    click Worker "https://github.com/garehmalika/medassit/blob/main/Backend/app/deployment/docker/Dockerfile.backend"
    click Worker "https://github.com/garehmalika/medassit/blob/main/Backend/app/deployment/docker/docker-compose.yml"
    click UI "https://github.com/garehmalika/medassit/blob/main/frontend/next.config.js"
    click UI "https://github.com/garehmalika/medassit/blob/main/frontend/package.json"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#00796B,color:#004D40
    classDef backend fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20
    classDef background fill:#FFF3E0,stroke:#F57C00,color:#E65100
    classDef database fill:#EFEBE9,stroke:#5D4037,color:#3E2723,shape:cylinder
    classDef external fill:#ECEFF1,stroke:#607D8B,color:#37474F,shape:hexagon
```
