# a169n/fitme architecture

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

- Interactive diagram: https://gitdiagram.com/a169n/fitme
- Repository: https://github.com/a169n/fitme

## Overview

##### Project type and purpose
- FitMe is a monorepo full-stack web application.
- Frontend: React + Vite + Redux Toolkit for a single-page app.
- Backend: Node.js + Express + Mongoose for RESTful APIs.
- Purpose: Manage users, restaurants, foods, categories, orders, reviews, messages; integrate with external exercise/recipe APIs; handle auth, file uploads, and email notifications.

##### Key directories and architectural patterns
- client/ → React app (components, pages, hooks, redux).
- server/ → Express app (routes, controllers, models, middleware).
- Root uses concurrently to start both dev servers.
- MVC-style on the backend (models/controllers/routes).
- Presentation/container pattern on the frontend; global state via Redux slices and RTK Query for API calls.

##### Main components
a. Client Tier (Browser)
- React SPA (pages & components)
- Redux store & RTK Query services
- Custom hooks for auth and role checks
b. API Gateway (Express Server)
- Routers (apiRouter, authRouter, cartRouter, etc.)
- Controllers (business logic)
- Middleware (authMiddleware for JWT, multer for file uploads, cors)
- Config (db connection via Mongoose, env variables via dotenv)
c. Data Tier
- MongoDB cluster (collections: users, restaurants, foods, categories, orders, messages, reviews, globalCategories)
d. External Services
- Third-party exercise API
- Third-party recipes API
- SMTP/Mail service via Nodemailer
e. File storage
- Local server/uploads directory via multer (images)

##### Component interactions and data flows
- Browser ↔ React SPA: user actions trigger API calls via RTK Query.
- React SPA → Express API endpoints (HTTP REST).
- Express → Controllers → Mongoose Models → MongoDB.
- Express ↔ External APIs: fetch exercises & recipes on demand.
- Express → Nodemailer: send email notifications on login/registration.
- Express → multer → server/uploads: handle image uploads/deletions.
- JWT tokens issued by authController, validated by authMiddleware on protected routes.

##### Important architectural patterns/design principles
- Separation of concerns:
- Frontend UI / state management vs. backend REST & business logic.
- Controllers for logic, models for data schema, routes for API definitions.
- Statelessness: RESTful endpoints use JWT for authentication.
- Single source of truth: Redux store holds client state; MongoDB is persistent store.
- Scalability: MVC layering allows independent scaling of API, DB, and front end.
- Extensibility: RTK Query services add new endpoints easily; Express router/controller pattern permits adding new resources.

a. Sketch the three tiers horizontally: Client | API | Data & Services.
b. Inside Client tier, subdivide into UI layer, state management, network layer.
c. Inside API tier, stack routers at top, controllers in middle, models at bottom, middleware on the side along incoming routes.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Tier
    subgraph "Client Tier"
        direction TB
        App["React SPA (App.jsx)"]:::frontend
        Vite["Vite Config"]:::frontend
        Components["UI Components"]:::frontend
        Pages["Pages"]:::frontend
        Hooks["Custom Hooks"]:::frontend
        Store["Redux Store"]:::frontend
        Slices["Redux Slices"]:::frontend
        Services["RTK Query Services"]:::frontend
    end

    %% API Gateway
    subgraph "API Gateway"
        direction TB
        Server["Express Server (server.js)"]:::backend
        Env["Env Config (.env)"]:::backend
        DBConfig["DB Config (db.js)"]:::backend
        Routers["Routers"]:::backend
        Controllers["Controllers"]:::backend
        Middleware["Middleware"]:::backend
        Models["Mongoose Models"]:::backend
    end

    %% Data & Services Tier
    subgraph "Data & Services"
        direction TB
        Mongo["MongoDB Cluster"]:::database
        ExtAPIs["External Exercise & Recipe APIs"]:::external
        MailServ["SMTP / Mail Service"]:::external
        Storage["File Storage (uploads)"]:::storage
    end

    %% Connections
    App -->|User Actions| Store
    Store -->|State Updates| Services
    Services -->|REST Calls| Server
    App -->|HTTP REST| Server
    Server -->|Connect| Mongo
    Server -->|Fetch Data| ExtAPIs
    Server -->|Send Emails| MailServ
    Server -->|Handle Files| Storage
    Server --> Routers
    Routers -->|route to| Controllers
    Controllers -->|use| Models
    Server --> Middleware
    Server --> Env
    Server --> DBConfig

    %% Click Events
    click App "https://github.com/a169n/fitme/blob/main/client/src/App.jsx"
    click Vite "https://github.com/a169n/fitme/blob/main/client/vite.config.js"
    click Components "https://github.com/a169n/fitme/tree/main/client/src/components"
    click Pages "https://github.com/a169n/fitme/tree/main/client/src/pages"
    click Hooks "https://github.com/a169n/fitme/blob/main/client/src/hooks/useAccess.js"
    click Hooks "https://github.com/a169n/fitme/blob/main/client/src/hooks/useAdmin.js"
    click Hooks "https://github.com/a169n/fitme/blob/main/client/src/hooks/useUser.js"
    click Store "https://github.com/a169n/fitme/blob/main/client/src/redux/store.js"
    click Slices "https://github.com/a169n/fitme/blob/main/client/src/redux/slices/authSlice.js"
    click Services "https://github.com/a169n/fitme/tree/main/client/src/redux/services"
    click Server "https://github.com/a169n/fitme/blob/main/server/server.js"
    click Env "https://github.com/a169n/fitme/blob/main/server/.env"
    click DBConfig "https://github.com/a169n/fitme/blob/main/server/config/db.js"
    click Routers "https://github.com/a169n/fitme/tree/main/server/routes"
    click Controllers "https://github.com/a169n/fitme/tree/main/server/controllers"
    click Middleware "https://github.com/a169n/fitme/tree/main/server/middleware"
    click Models "https://github.com/a169n/fitme/tree/main/server/models"
    click Storage "https://github.com/a169n/fitme/tree/main/server/uploads"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#333,stroke-width:1px
    classDef backend fill:#D0FFD6,stroke:#333,stroke-width:1px
    classDef database fill:#FFE0A1,stroke:#333,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#333,stroke-width:1px
    classDef storage fill:#F0E0FF,stroke:#333,stroke-width:1px
```
