# cokke93/proyecto-dam-appglucosa architecture

> Architecture diagram of the GitHub repository cokke93/proyecto-dam-appglucosa, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cokke93/proyecto-dam-appglucosa
- Repository: https://github.com/cokke93/proyecto-dam-appglucosa

## Overview

##### Project Type and Purpose
- This is a full-stack desktop application built with Electron (for the desktop shell and UI), Express.js (HTTP API backend), and MongoDB (data storage).
- Purpose: allow multi-user tracking of diabetic patient glucose measurements, generate interactive charts, export PDF reports, and provide nutritional guidance.

##### Core Components
a. Electron Desktop Client
- Renderer (frontend/pages + frontend/scripts + frontend/styles + Chart.js)
- Main process (frontend/main.js)
b. Express API Server (backend/app.js + backend/routes + backend/models)
c. MongoDB Database (with Mongoose ODM)
d. External Libraries & Services
- bcrypt (password hashing)
- JSON Web Tokens (authentication)
- PDF generator (export reports)

##### Relationships & Data Flows
- User interacts with Electron UI → UI makes HTTP(S) calls to Express routes (auth, measurements, food, report).
- Express controllers invoke Mongoose models (User, Measurement, FoodItem) → MongoDB.
- Responses (JSON data, PDF blobs) flow back to the renderer, which updates views or triggers downloads.
- Authentication flow: login/register endpoints generate JWTs → stored client-side → included in subsequent requests.
- Passwords hashed via bcrypt before persisting to MongoDB.

##### Architectural Patterns & Design Principles
- Layered architecture: Presentation (Electron renderer) → Application/API (Express) → Data (MongoDB).
- Separation of concerns:
- Routes handle HTTP;
- Models handle data schema and persistence;
- Frontend scripts handle DOM/UI logic.
- Stateless API calls secured via JWT tokens.
- MVC-like structure on backend: Routes as controllers, Mongoose schemas as models, views managed by Electron.

##### Execution Steps
2. Below it: Electron Main → spawns Renderer & manages window
6. Data flows: authentication flow, CRUD operations, PDF export

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Title
    subgraph "AppGlucosa Architecture"
    end

    %% Top Layer: Electron Desktop Client
    subgraph "Electron Desktop Client" 
        direction TB
        EM["Main Process<br/>(frontend/main.js)"]:::frontend
        ER["Renderer Process"]:::frontend
        subgraph "UI Components" 
            direction TB
            PAGES["Pages<br/>(HTML)"]:::frontend
            SCRIPTS["Scripts<br/>(JS + Chart.js)"]:::frontend
            STYLES["Styles<br/>(CSS)"]:::frontend
        end
    end

    EM -->|spawns| ER
    ER -->|renders| PAGES
    ER -->|executes| SCRIPTS
    ER -->|applies| STYLES

    %% Middle Layer: Express API Server
    subgraph "Express API (backend)" 
        direction TB
        APP["app.js"]:::backend
        subgraph "Routes (Controllers)" 
            direction TB
            AUTH["auth.js"]:::backend
            MEAS["measurements.js"]:::backend
            FOOD["food.js"]:::backend
            REP["report.js"]:::backend
        end
        subgraph "Models (Mongoose)" 
            direction TB
            USER_M["User.js"]:::backend
            MEAS_M["Measurement.js"]:::backend
            FOOD_M["FoodItem.js"]:::backend
        end
        APP -->|uses| AUTH
        APP -->|uses| MEAS
        APP -->|uses| FOOD
        APP -->|uses| REP
        AUTH -->|CRUD| USER_M
        MEAS -->|CRUD| MEAS_M
        FOOD -->|CRUD| FOOD_M
        REP -->|PDF gen| FOOD_M
    end

    %% Bottom Layer: Database
    DB[(MongoDB)]:::database
    USER_M -->|read/write| DB
    MEAS_M -->|read/write| DB
    FOOD_M -->|read/write| DB

    %% Data Flows: UI to API
    ER -->|POST /auth/login<br/>returns JWT| AUTH
    ER -->|POST /auth/register| AUTH
    ER -->|GET /measurements?userId=…| MEAS
    ER -->|POST /measurements| MEAS
    ER -->|GET /food?query=…| FOOD
    ER -->|GET /report?userId=…| REP

    %% Authentication Flow
    AUTH -->|generate JWT| ER
    ER -->|include JWT in headers| AUTH
    AUTH -->|validate JWT via middleware| APP

    %% External Libraries & Services
    BL["bcrypt"]:::external
    JWT["jsonwebtoken"]:::external
    PDF["PDF Generator"]:::external
    AUTH -->|hash passwords| BL
    AUTH -->|sign/verify tokens| JWT
    REP -->|create PDF| PDF

    %% Legend
    subgraph "Legend" 
        direction TB
        F[Blue: Frontend]:::frontend
        B[Green: Backend]:::backend
        D[Orange: Database]:::database
        E[Purple: External]:::external
    end

    %% Click Events - Electron Client
    click EM "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/frontend/main.js"
    click PAGES "https://github.com/cokke93/proyecto-dam-appglucosa/tree/main/frontend/pages"
    click SCRIPTS "https://github.com/cokke93/proyecto-dam-appglucosa/tree/main/frontend/scripts"
    click STYLES "https://github.com/cokke93/proyecto-dam-appglucosa/tree/main/frontend/styles"

    %% Click Events - API
    click APP "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/app.js"
    click AUTH "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/routes/auth.js"
    click MEAS "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/routes/measurements.js"
    click FOOD "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/routes/food.js"
    click REP "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/routes/report.js"

    %% Click Events - Models
    click USER_M "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/models/User.js"
    click MEAS_M "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/models/Measurement.js"
    click FOOD_M "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/models/FoodItem.js"

    %% Click Events - Config
    click BL "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/package.json"
    click JWT "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/package.json"
    click PDF "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/routes/report.js"
    click F "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/frontend/package.json"
    click B "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/backend/package.json"
    click E "https://github.com/cokke93/proyecto-dam-appglucosa/blob/main/README.md"

    classDef frontend fill:#b3d9ff,stroke:#3399ff,stroke-width:1px
    classDef backend fill:#d0f0c0,stroke:#339933,stroke-width:1px
    classDef database fill:#ffe0b3,stroke:#ff9933,stroke-width:1px
    classDef external fill:#e6ccff,stroke:#9933ff,stroke-width:1px
```
