# bernaba123/e-commerce architecture

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

- Interactive diagram: https://gitdiagram.com/bernaba123/e-commerce
- Repository: https://github.com/bernaba123/e-commerce

## Overview

##### Project type and purpose
- This is a full-stack e-commerce web application with both user‐facing and admin interfaces.
- Frontend is a React single-page app built with Vite and styled via Tailwind.
- Backend is a Node.js/Express RESTful API following an MVC pattern, using JWT for authentication and WebSockets for real-time order tracking.
- Data is persisted in a document database (e.g. MongoDB via Mongoose models).

##### File structure
- Top-level separates “backend” and “frontend” directories.
- Backend directories:
- controllers/ (business logic per resource)
- models/ (Mongoose schemas: User, Product, Order, Request)
- routes/ (Express routers for auth, users, products, orders, requests)
- middleware/ (auth guard, input validation, error handler)
- utils/ (JWT token generation, admin‐creation script, tracking simulator)
- script/seedData.js (initial data load)
- ssl/ (cert/key for HTTPS)
- server.js (Express entrypoint)
- Frontend directories:
- components/ (UI, admin modals, auth‐guards, layout, common dialogs)
- pages/ (React Router pages: Home, Products, Cart, Checkout, Tracking, Admin/User dashboards, Auth)
- context/ (AuthContext, CartContext)
- hooks/useApi.js (API wrapper)
- services/api.js (HTTP client), socketService.js (WebSocket client)
- Root has Vite, Tailwind, ESLint configs and a single index.html.

##### README for additional insights

a. Main components to represent:
- Client (React SPA)
- Vite dev/server or CDN in production (serves static assets)
- Backend API server (Node.js/Express)
- Database (MongoDB)
- Authentication subsystem (JWT issuance & validation)
- Real-time subsystem (WebSocket or Socket.io)
- Utility scripts (seedData, tracking simulator)
- SSL termination (HTTPS layer in front of Express)
b. Relationships & data flow:
- Browser → HTTP(S) → Static asset server (Vite/NGINX/CDN) → React app
- React app → HTTP(S) → Express API → Controllers → Models → Database
- Express API emits real-time events → WebSocket server → React socketService → UI
- React uses Context API to manage auth state and cart data client-side
- Admin scripts connect directly to the database for seeding or maintenance
c. Architectural patterns & design principles:
- Backend: MVC (Models, Controllers, Routes) + middleware pipeline
- Security: JWT for stateless auth, input validation, error handling middleware
- Separation of concerns: clear split between frontend assets and backend services
- Real-time event‐driven updates for order tracking
- Frontend: Component-based UI, context for global state, hooks for data fetching
d. Key technologies & frameworks:
- Frontend: React, Vite, Tailwind CSS, React Router, Context API, WebSockets
- Backend: Node.js, Express, Mongoose (MongoDB), JSON Web Tokens, Socket.io (or ws)
- Dev & deployment: Vite, postcss, ESLint, SSL certificates

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client/UI Layer
    subgraph "Client/UI Layer"
        direction TB
        Browser["Browser"]:::external
        ViteConfig["vite.config.js"]:::frontend
        IndexHtml["index.html"]:::frontend
        Browser -->|"HTTPS 443"| ViteConfig
        Browser -->|"HTTPS 443"| IndexHtml

        ReactEntry["main.jsx"]:::frontend
        AppComp["App.jsx"]:::frontend
        ViteConfig -->|"serves"| ReactEntry
        IndexHtml -->|"loads"| ReactEntry
        ReactEntry --> AppComp

        subgraph "Pages" 
            direction TB
            HomePage["HomePage.jsx"]:::frontend
            ProductsPage["ProductsPage.jsx"]:::frontend
            ProductDetailsPage["ProductDetailsPage.jsx"]:::frontend
            CartPage["CartPage.jsx"]:::frontend
            CheckoutPage["CheckoutPage.jsx"]:::frontend
            OrderTrackingPage["OrderTrackingPage.jsx"]:::frontend
            RequestPage["RequestPage.jsx"]:::frontend
            LoginPage["LoginPage.jsx"]:::frontend
            SignupPage["SignupPage.jsx"]:::frontend
            AdminDashboard["AdminDashboard.jsx"]:::frontend
            UserDashboard["UserDashboard.jsx"]:::frontend
        end
        AppComp --> Pages

        subgraph "Components"
            direction TB
            AdminComponents["components/admin"]:::frontend
            AuthGuardComponents["components/auth"]:::frontend
            CommonDialogs["components/common"]:::frontend
            LayoutComponents["components/layout"]:::frontend
        end
        AppComp --> Components

        subgraph "Context Providers"
            direction TB
            AuthContext["AuthContext.jsx"]:::frontend
            CartContext["CartContext.jsx"]:::frontend
        end
        AppComp --> ContextProviders

        subgraph "Hooks & Services"
            direction TB
            UseApi["useApi.js"]:::frontend
            ApiService["api.js"]:::frontend
            SocketService["socketService.js"]:::frontend
        end
        AppComp --> HooksServices

    end

    %% Server/Infrastructure Layer
    subgraph "Server/Infrastructure Layer"
        direction TB
        SSL_Cert["server.crt"]:::backend
        SSL_Key["server.key"]:::backend
        SSL_Cert -->|SSL Termination| ServerEntry
        SSL_Key -->|SSL Termination| ServerEntry

        ServerEntry["server.js"]:::backend

        subgraph "Routes"
            direction TB
            AuthRoutes["auth.js"]:::backend
            UsersRoutes["users.js"]:::backend
            ProductsRoutes["products.js"]:::backend
            OrdersRoutes["orders.js"]:::backend
            RequestsRoutes["requests.js"]:::backend
        end
        ServerEntry --> Routes

        subgraph "Controllers"
            direction TB
            AuthController["authController.js"]:::backend
            UserController["userController.js"]:::backend
            ProductController["productController.js"]:::backend
            OrderController["orderController.js"]:::backend
            RequestController["requestController.js"]:::backend
        end
        Routes --> Controllers

        subgraph "Models"
            direction TB
            UserModel["User.js"]:::backend
            ProductModel["Product.js"]:::backend
            OrderModel["Order.js"]:::backend
            RequestModel["Request.js"]:::backend
        end
        Controllers --> Models

        subgraph "Middleware"
            direction TB
            AuthMiddleware["auth.js"]:::backend
            ValidationMiddleware["validation.js"]:::backend
            ErrorHandler["errorHandler.js"]:::backend
        end
        ServerEntry --> Middleware

        subgraph "Utils"
            direction TB
            GenerateToken["generateToken.js"]:::backend
            CreateAdmin["createAdmin.js"]:::backend
            TrackingSim["trackingSimulator.js"]:::backend
        end
        ServerEntry --> Utils

        subgraph "Scripts"
            direction TB
            SeedData["seedData.js"]:::backend
        end
        Scripts --> Models

        Database["MongoDB"]:::database
        Models --> Database

    end

    %% Interactions
    AppComp -->|"REST API JSON"| AuthRoutes
    UseApi --> AuthRoutes
    ApiService --> AuthRoutes
    SocketService -->|"WSS 3000"| TrackingSim
    OrderTrackingPage -->|"subscribe"| SocketService
    TrackingSim -->|"emit events"| SocketService
    AuthContext -->|"stores JWT"| AppComp
    CartContext -->|"stores cart"| AppComp

    %% Click Events
    click ViteConfig "https://github.com/bernaba123/e-commerce/blob/main/vite.config.js"
    click IndexHtml "https://github.com/bernaba123/e-commerce/blob/main/index.html"
    click ReactEntry "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/main.jsx"
    click AppComp "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/App.jsx"
    click HomePage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/HomePage.jsx"
    click ProductsPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/ProductsPage.jsx"
    click ProductDetailsPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/ProductDetailsPage.jsx"
    click CartPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/CartPage.jsx"
    click CheckoutPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/CheckoutPage.jsx"
    click OrderTrackingPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/OrderTrackingPage.jsx"
    click RequestPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/RequestPage.jsx"
    click LoginPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/auth/LoginPage.jsx"
    click SignupPage "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/auth/SignupPage.jsx"
    click AdminDashboard "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/dashboard/AdminDashboard.jsx"
    click UserDashboard "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/pages/dashboard/UserDashboard.jsx"
    click AdminComponents "https://github.com/bernaba123/e-commerce/tree/main/frontend/src/components/admin"
    click AuthGuardComponents "https://github.com/bernaba123/e-commerce/tree/main/frontend/src/components/auth"
    click CommonDialogs "https://github.com/bernaba123/e-commerce/tree/main/frontend/src/components/common"
    click LayoutComponents "https://github.com/bernaba123/e-commerce/tree/main/frontend/src/components/layout"
    click AuthContext "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/context/AuthContext.jsx"
    click CartContext "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/context/CartContext.jsx"
    click UseApi "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/hooks/useApi.js"
    click ApiService "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/services/api.js"
    click SocketService "https://github.com/bernaba123/e-commerce/blob/main/frontend/src/services/socketService.js"
    click ServerEntry "https://github.com/bernaba123/e-commerce/blob/main/backend/server.js"
    click SSL_Cert "https://github.com/bernaba123/e-commerce/blob/main/backend/ssl/server.crt"
    click SSL_Key "https://github.com/bernaba123/e-commerce/blob/main/backend/ssl/server.key"
    click AuthRoutes "https://github.com/bernaba123/e-commerce/blob/main/backend/routes/auth.js"
    click UsersRoutes "https://github.com/bernaba123/e-commerce/blob/main/backend/routes/users.js"
    click ProductsRoutes "https://github.com/bernaba123/e-commerce/blob/main/backend/routes/products.js"
    click OrdersRoutes "https://github.com/bernaba123/e-commerce/blob/main/backend/routes/orders.js"
    click RequestsRoutes "https://github.com/bernaba123/e-commerce/blob/main/backend/routes/requests.js"
    click AuthController "https://github.com/bernaba123/e-commerce/blob/main/backend/controllers/authController.js"
    click UserController "https://github.com/bernaba123/e-commerce/blob/main/backend/controllers/userController.js"
    click ProductController "https://github.com/bernaba123/e-commerce/blob/main/backend/controllers/productController.js"
    click OrderController "https://github.com/bernaba123/e-commerce/blob/main/backend/controllers/orderController.js"
    click RequestController "https://github.com/bernaba123/e-commerce/blob/main/backend/controllers/requestController.js"
    click UserModel "https://github.com/bernaba123/e-commerce/blob/main/backend/models/User.js"
    click ProductModel "https://github.com/bernaba123/e-commerce/blob/main/backend/models/Product.js"
    click OrderModel "https://github.com/bernaba123/e-commerce/blob/main/backend/models/Order.js"
    click RequestModel "https://github.com/bernaba123/e-commerce/blob/main/backend/models/Request.js"
    click AuthMiddleware "https://github.com/bernaba123/e-commerce/blob/main/backend/middleware/auth.js"
    click ValidationMiddleware "https://github.com/bernaba123/e-commerce/blob/main/backend/middleware/validation.js"
    click ErrorHandler "https://github.com/bernaba123/e-commerce/blob/main/backend/middleware/errorHandler.js"
    click GenerateToken "https://github.com/bernaba123/e-commerce/blob/main/backend/utils/generateToken.js"
    click CreateAdmin "https://github.com/bernaba123/e-commerce/blob/main/backend/utils/createAdmin.js"
    click TrackingSim "https://github.com/bernaba123/e-commerce/blob/main/backend/utils/trackingSimulator.js"
    click SeedData "https://github.com/bernaba123/e-commerce/blob/main/backend/script/seedData.js"

    %% Styles
    classDef frontend fill:#ccffcc,stroke:#006600,color:#000;
    classDef backend fill:#cce5ff,stroke:#003366,color:#000;
    classDef database fill:#ffdd99,stroke:#994d00,color:#000;
    classDef external fill:#efefef,stroke:#999,color:#333;
    classDef realtime fill:#e0ccff,stroke:#660099,color:#000;
```
