# jawakarsri/mediconnect architecture

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

- Interactive diagram: https://gitdiagram.com/jawakarsri/mediconnect
- Repository: https://github.com/jawakarsri/mediconnect

## Overview

##### Project Overview
- The project is an AI-powered healthcare platform called MediConnect, built using Next.js (v15) with React (v19) and TypeScript. It leverages Tailwind CSS for styling, integrates Google AI components for personalized healthcare recommendations, and uses Supabase for secure data and authentication.
- Its purpose is to provide personalized health insights, secure data and medical records management, real-time communications, and support 24/7 access to healthcare.

##### Main Components
a. Frontend/User Interface:
- The “app” and “components” directories show that the frontend UI follows a modern component-based architecture.
- UI areas like the landing page, authentication screens, chat interfaces, hospitals page, and admin panels.
b. API Layer:
- The “app/api” subdirectories (chat, direct-chat, staff) signal distinct API endpoints handling healthcare-related queries, communications, and staff operations.
- API endpoints that facilitate interactions like chat-based consultations and direct messaging.
c. Backend and Server Components:
- That Next.js handles both frontend and backend rendering (using server components and pages in the “app” directory) with SSR, dynamic API routes, and edge/server middleware.
d. Database/Storage Layer:
- Supabase is used extensively for secure data management. Include the “supabase” directory with its migrations and SQL files along with the “lib/supabase-admin” and “lib/supabase” modules.
- Relationships between the backend API endpoints and the Supabase database for operations like secure medical record handling.
e. AI Integration:
- Integration with Google AI, used for personalized recommendations and health insights.
- The “lib/embeddings” module and connections to AI services should be marked.
f. Additional Scripts & Data Management:
- The “scripts” directory points to background jobs for importing, uploading, and updating training data (like updating embeddings and processing medical-data).
- Support tasks as part of data preprocessing/maintenance that feeds into the health insights.

##### Relationships and Data Flow
- Client requests (e.g., web browser interactions) hitting the Next.js frontend.
- Asynchronous interactions for real-time communications (such as chat functionality) and background jobs triggered by scripts.

##### Architectural Patterns and Design Principles
- Full-stack (isomorphic) architecture used by Next.js to server-render pages and manage API routes within the same application.
- Modular separation:
- UI components (in “components” and “app” directories) are decoupled from business logic (distributed in “lib”).
- API endpoints are cleanly grouped for distinct functionalities (chat, authentication, hospitals, etc.).
- Out the use of serverless/API routes versus traditional SSR pages.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Node
    User["User"]:::frontend

    %% Frontend / UI Subgraph
    subgraph "Frontend / UI"
        direction TB
        App["Next.js App (SSR & UI)"]:::frontend
        Landing["Landing Page"]:::frontend
        Admin["Admin Page"]:::frontend
        Auth["Auth Page"]:::frontend
        Hospitals["Hospitals Page"]:::frontend
        Soon["Soon Page"]:::frontend
        Components["Components (UI Components)"]:::frontend
    end

    %% API Endpoints Subgraph
    subgraph "API Endpoints"
        direction TB
        ChatAPI["Chat API (/api/chat)"]:::backend
        DirectChatAPI["Direct Chat API (/api/direct-chat)"]:::backend
        StaffAPI["Staff API (/api/staff)"]:::backend
    end

    %% Database / Storage Subgraph
    subgraph "Database / Storage"
        direction TB
        SupabaseDB(("Supabase DB")):::external
        Migrations["Migrations"]:::external
        SupabaseClient["Supabase Client"]:::external
        SupabaseAdmin["Supabase Admin"]:::external
    end

    %% External Integrations Subgraph
    subgraph "External Integrations"
        direction TB
        GoogleAI["Google AI Integration"]:::external
    end

    %% Authentication & Security Subgraph
    subgraph "Authentication & Security"
        direction TB
        AuthContext["Auth Context"]:::backend
    end

    %% Background Data Scripts Subgraph
    subgraph "Background Data Scripts"
        direction TB
        ImportData["Import Data"]:::scripts
        LoadData["Load Data"]:::scripts
        UpdateEmbeddings["Update Embeddings"]:::scripts
        UploadTraining["Upload Training Data"]:::scripts
    end

    %% Connections
    User -->|"request"| App

    %% From Frontend App to individual pages/components
    App --> Landing
    App --> Admin
    App --> Auth
    App --> Hospitals
    App --> Soon
    App --> Components

    %% Frontend calls API Endpoints
    App -->|"calls"| ChatAPI
    App -->|"calls"| DirectChatAPI
    App -->|"calls"| StaffAPI

    %% API Endpoints interaction with Database and External services
    ChatAPI -->|"reads/writes"| SupabaseDB
    DirectChatAPI -->|"reads/writes"| SupabaseDB
    StaffAPI -->|"reads/writes"| SupabaseDB

    ChatAPI -->|"uses"| SupabaseClient
    DirectChatAPI -->|"uses"| SupabaseClient
    StaffAPI -->|"uses"| SupabaseClient

    ChatAPI -->|"integrates"| GoogleAI

    %% Authentication flow
    Auth -->|"secures"| AuthContext

    %% Admin page interaction with Supabase Admin
    Admin -->|"manages"| SupabaseAdmin

    %% Background Data Scripts updating Database & Google AI
    ImportData -->|"updates"| SupabaseDB
    LoadData -->|"updates"| SupabaseDB
    UploadTraining -->|"updates"| SupabaseDB
    UpdateEmbeddings -->|"updates"| SupabaseDB
    UpdateEmbeddings -->|"updates"| GoogleAI

    %% Migrations relationship with Database
    Migrations -->|"defines schema"| SupabaseDB

    %% Click Events
    click App "https://github.com/jawakarsri/mediconnect/tree/main/app"
    click Landing "https://github.com/jawakarsri/mediconnect/blob/main/app/page.tsx"
    click Admin "https://github.com/jawakarsri/mediconnect/blob/main/app/admin/page.tsx"
    click Auth "https://github.com/jawakarsri/mediconnect/blob/main/app/auth/page.tsx"
    click Hospitals "https://github.com/jawakarsri/mediconnect/blob/main/app/hospitals/page.tsx"
    click Soon "https://github.com/jawakarsri/mediconnect/blob/main/app/soon/page.tsx"
    click Components "https://github.com/jawakarsri/mediconnect/tree/main/components"

    click ChatAPI "https://github.com/jawakarsri/mediconnect/blob/main/app/api/chat/route.ts"
    click DirectChatAPI "https://github.com/jawakarsri/mediconnect/blob/main/app/api/direct-chat/route.ts"
    click StaffAPI "https://github.com/jawakarsri/mediconnect/blob/main/app/api/staff/route.ts"

    click Migrations "https://github.com/jawakarsri/mediconnect/blob/main/supabase/migrations/20240101000000_initial_schema.sql"
    click SupabaseClient "https://github.com/jawakarsri/mediconnect/blob/main/lib/supabase.ts"
    click SupabaseAdmin "https://github.com/jawakarsri/mediconnect/blob/main/lib/supabase-admin.ts"

    click GoogleAI "https://github.com/jawakarsri/mediconnect/blob/main/lib/embeddings.ts"

    click AuthContext "https://github.com/jawakarsri/mediconnect/blob/main/lib/auth-context.tsx"

    click ImportData "https://github.com/jawakarsri/mediconnect/blob/main/scripts/import-data.ts"
    click LoadData "https://github.com/jawakarsri/mediconnect/blob/main/scripts/load-data.ts"
    click UpdateEmbeddings "https://github.com/jawakarsri/mediconnect/blob/main/scripts/update-embeddings.ts"
    click UploadTraining "https://github.com/jawakarsri/mediconnect/blob/main/scripts/upload-training-data.ts"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef scripts fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
```
