# addynoven/fitness-tracking-app architecture

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

- Interactive diagram: https://gitdiagram.com/addynoven/fitness-tracking-app
- Repository: https://github.com/addynoven/fitness-tracking-app

## Overview

##### Main Components
- API Gateway/Server: Represented by the entry point (backend/src/server.ts) that bootstraps the Express server.
- Controllers & Routes: These organize the REST endpoints (for authentication, activities, insights, goals, and profiles). Group components by their responsibility (e.g., Home.controller, activity.controller, insight controllers, etc.) and illustrate how they receive HTTP requests.
- Services: Business logic is split into a range of services (like activity.service, insight.service, weeklyBestDay.service, etc.) that process data and execute calculations (including the AI-powered calorie calculator and streak detection).
- Data Models & Database: MongoDB is used with models organized under backend/src/Models/Mongodb. Show a direct link from service calls to model interactions.
- Middleware: Authentication, error handling, asynchronous handlers, etc. Their role is to validate tokens, handle errors, and manage asynchronous flows before hitting controllers.
- Configuration & Utilities: These include environment settings (via .env files), mailing configuration (Mailgen, transporter), and various utility methods for calculating calories and processing insights.

##### Relationships and Data Flow
- API Request Flow: Illustrate how a client request enters the system at the server, passes through middleware (e.g., authMiddleware, errorHandler), and then is routed to the appropriate controller.
- Service/Model Communication: Services interact with MongoDB models to read/write and process data. Connect services to the data store.
- External/Utility Integration: Highlight where utilities (like calorieCalculator or insight processors) are used by services and sometimes by middleware.
- Authentication Flow: Detail the flow for authentication endpoints (sign-up, sign-in, sign-out) going through designated auth controllers with JWT validation.

##### Architectural Patterns and Design Principles
- Modularization: The project is neatly divided into discrete modules (controllers, services, middleware, models, etc.) following separation of concerns.
- RESTful API: Clearly mapping endpoints to resource-based controllers and using standard HTTP methods for CRUD operations.
- Middleware Usage: A layered approach to request processing through third-party and custom middleware.
- Service-Oriented Logic: Business logic is encapsulated in services that may internally use utility functions and aggregators (especially for AI-powered insights).
- Configuration Management: Centralized configuration (in backend/src/config) ensures environment-specific settings are managed separately.

##### Technologies/Frameworks
- Node.js with Express – for creating the API server.
- MongoDB with Mongoose – for data persistence.
- JWT – for authentication.
- TypeScript – strongly typed implementation.
- AI Modules – algorithms for calorie calculation, streak detection, and recommendations.

- From “Middleware” to “Controllers” and then to “Services”.
- From “Services” to “Models/Database”.
- Asymmetric dependencies. For example, “Controllers” depend on “Services” but not vice versa.
- Optionally, add a summary annotation indicating this is for a smart fitness platform with AI components, which covers both traditional software (API + DB) and AI algorithms.

##### Detail Every Essential Element
- That the architecture distinguishes between request handling (controllers), business logic (services), and persistence (models) – a hallmark of clean, maintainable architecture.
- That AI-driven functionalities (calorie calculation, streak insights) are handled within services augmented by specific utility modules.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "API Layer"
        APIServer["API Gateway/Server"]:::api
    end

    subgraph "Request Processing"
        MiddlewareLayer["Middleware"]:::middleware
        RTE["Routes"]:::controller
        CTR["Controllers"]:::controller
    end

    subgraph "Business Logic"
        Services["Services"]:::service
        Utilities["Utilities"]:::utility
        Configuration["Configuration"]:::config
    end

    subgraph "Data Layer"
        Models["MongoDB & Models"]:::database
    end

    subgraph "External"
        ExternalTools["External Tools (Postman,Newman,Swagger)"]:::external
    end

    APIServer -->|"HTTP_Request"| MiddlewareLayer
    MiddlewareLayer -->|"Routing"| RTE
    RTE -->|"Delegates"| CTR
    CTR -->|"Calls_Service"| Services
    CTR -->|"Uses_Utility"| Utilities
    Services -->|"DB_Query"| Models
    Services -->|"Uses_Utility"| Utilities
    Configuration -->|"Configures"| Services
    APIServer ---|"Tested_with"| ExternalTools

    click APIServer "https://github.com/addynoven/fitness-tracking-app/blob/main/backend/src/server.ts"
    click MiddlewareLayer "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/middleware"
    click RTE "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/routes"
    click CTR "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/controllers"
    click Services "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/services"
    click Models "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/Models/Mongodb"
    click Configuration "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/config"
    click Utilities "https://github.com/addynoven/fitness-tracking-app/tree/main/backend/src/utils"

    classDef api fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef middleware fill:#FFC0CB,stroke:#000,stroke-width:1px;
    classDef controller fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef service fill:#FFFF99,stroke:#000,stroke-width:1px;
    classDef database fill:#98FB98,stroke:#000,stroke-width:1px;
    classDef config fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef utility fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef external fill:#FFA07A,stroke:#000,stroke-width:1px;
```
