# joaquindecima/coderhouse-js-ecommerce architecture

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

- Interactive diagram: https://gitdiagram.com/joaquindecima/coderhouse-js-ecommerce
- Repository: https://github.com/joaquindecima/coderhouse-js-ecommerce

## Overview

##### Main Components
- Frontend Client:
- Represented by static assets in the “public” folder (CSS, JavaScript, images) and the Handlebars views under “views”. This is the browser-facing layer that renders pages and interacts with client-side scripts (including chat, ecommerce operations, dynamic themes, etc.).
- Backend Server:
- The “src/server.js” is the entry point to your Node.js application.
- Express is used as the server framework to handle routing, middleware and API endpoints.
- Routers & API Layer:
- Located in “src/routers/”, these handle different parts of your application (authentication, product, cart, chat, and error routes). There are both standard routes (e.g., authRouter.js for session-based flows and global and product routers) and distinct API endpoints (e.g., authRouterAPI, cartRouterAPI).
- Business Logic / Models:
- Found in “src/model/”, organized by domain (cart, chat, order, product, user) the models encapsulate business rules and interact with the data access layer directly.
- Data Access Layer:
- “src/dao/” and model-specific DAO implementations (e.g., cartMongoManager.js, productFirebaseManager.js) indicate multiple persistence mechanisms. This demonstrates an abstraction that can swap between MongoDB, Firebase, or even file-based storage.
- Middleware:
- Middleware files such as “isAuthenticated”, “isNotAuthenticated”, and “isSeller” enforce access control and other logic before requests hit the routers.
- Utility Tools:
- “src/tools/” contain helper services for logging (Winston), sending emails (Nodemailer), SMS (Twilio), token management (JWT), and other domain-specific tasks.
- Configuration & Environment:
- The “config” folder (with Firebase admin JSON) and the .env file (via .env.example) define the runtime configuration. They include details for Mongo connections, Twilio credentials, session secrets, and JWT secrets.

##### Relationships & Data Flows
- The Browser interacts with the server via HTTP requests, triggering appropriate routes (e.g., login, product browsing, cart actions) and also using Socket.IO for real-time functionalities like chat.
- The Express server (entry point from src/server.js) dispatches these incoming requests to proper routers which, in turn, call into the business logic (models) and then to the DAO for data interactions.
- Middleware sits between the incoming request and the routers to enforce security and access controls.
- The DAO layer abstracts the underlying databases (MongoDB, Firebase) so that the business logic remains decoupled from the data storage implementations.
- Utility tools are invoked where required, for example, when sending email confirmations, SMS notifications, or logging important event information.
- The Handlebars view engine uses templates from the “views” folder to generate dynamic HTML responses for the user interface.

##### Architectural Patterns & Principles
- MVC-like Separation: Although not strictly the classic MVC, the division between “views” (presentation), “routers and middleware” (controller-like functions), and “models/dao” (data/business logic) is evident.
- Abstraction for Data Persistence: Different manager implementations (Mongo, Firebase, File) allow plugging in multiple back-end storage systems without changing higher-level logic.
- Decoupling of Concerns: Middleware for security and validation, separate modular routers, and isolated utility modules reflect a system that is segmented to simplify maintenance and testing.
- Microservices-ish tendencies (or plugin architecture) in the sense that each domain (cart, chat, product, etc.) has its own set of manager files and routers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Client
    subgraph "Frontend Client"
        FS["Static Assets (public)"]:::client
        HV["Handlebars Views (views)"]:::client
        FS -->|"loads"| HV
    end

    %% Backend Server
    subgraph "Backend Server"
        server["Entry Point (src/server.js)"]:::backend

        %% Routers & API Layer
        subgraph "Routers & API Layer"
            authR["Auth Router"]:::router
            prodR["Product Router"]:::router
            cartR["Cart Router"]:::router
            chatR["Chat Router"]:::router
            errorR["Error Router"]:::router
            globalR["Global Router"]:::router
        end

        %% Middleware
        subgraph "Middleware"
            authM["isAuthenticated.js"]:::middleware
            notAuthM["isNotAuthenticated.js"]:::middleware
            sellerM["isSeller.js"]:::middleware
        end

        %% Business Logic / Models
        subgraph "Business Logic / Models"
            models["Models (Cart, Chat, Order, Product, User)"]:::models
        end

        %% Data Access & Utility Tools
        subgraph "Data Access & Utility Tools"
            dao["Data Access Objects (DAOs)"]:::dao
            cartDAO["cartMongoManager.js"]:::dao
            productDAO["productFirebaseManager.js"]:::dao
            tools["Utility Tools"]:::tools
        end

        %% Connections inside Backend
        server -->|"routes"| authR
        server --> prodR
        server --> cartR
        server --> chatR
        server --> errorR
        server --> globalR

        authR -->|"pre-process"| authM
        prodR -->|"pre-process"| notAuthM
        cartR -->|"pre-process"| sellerM

        authM --> models
        notAuthM --> models
        sellerM --> models

        models --> dao
        models --> tools
        dao --- cartDAO
        dao --- productDAO
    end

    %% Configuration & Environment
    subgraph "Configuration & Environment"
        env[".env.example"]:::config
        conf["Configuration Files (config)"]:::config
    end
    server ---|"uses"| env
    server --- conf

    %% External Services
    dbM["MongoDB"]:::external
    fb["Firebase"]:::external
    fsys["File System"]:::external
    twilio["Twilio SMS"]:::external
    nodemailer["Nodemailer Email"]:::external
    socket["Socket.IO"]:::external

    %% Data flows from DAO to External Databases
    dao -->|"Mongo ops"| dbM
    dao -->|"Firebase ops"| fb
    dao -->|"File ops"| fsys

    %% Utility tools invoking external services
    tools -->|"sendEmail"| nodemailer
    tools -->|"sendSMS"| twilio
    tools -->|"realtime"| socket

    %% Client to Server communication (HTTP/WS)
    FS ---|"HTTP/WS"| server
    HV ---|"RenderedHTML"| server

    %% Styles
    classDef client fill:#AEECEF,stroke:#333,stroke-width:2px;
    classDef backend fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef router fill:#D6EAF8,stroke:#333,stroke-width:2px;
    classDef middleware fill:#F5CBA7,stroke:#333,stroke-width:2px;
    classDef models fill:#D5F5E3,stroke:#333,stroke-width:2px;
    classDef dao fill:#FCF3CF,stroke:#333,stroke-width:2px;
    classDef tools fill:#E8DAEF,stroke:#333,stroke-width:2px;
    classDef config fill:#EBDEF0,stroke:#333,stroke-width:2px;
    classDef external fill:#FADBD8,stroke:#E74C3C,stroke-width:2px;

    %% Click Events
    click FS "https://github.com/joaquindecima/coderhouse-js-ecommerce/tree/master/public/"
    click HV "https://github.com/joaquindecima/coderhouse-js-ecommerce/tree/master/views/"
    click server "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/server.js"
    click authR "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/routers/authRouter.js"
    click prodR "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/routers/productRouter.js"
    click cartR "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/routers/cartRouter.js"
    click chatR "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/routers/chatRouter.js"
    click errorR "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/routers/errorRouter.js"
    click globalR "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/routers/globalRouter.js"
    click authM "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/middleware/isAuthenticated.js"
    click notAuthM "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/middleware/isNotAuthenticated.js"
    click sellerM "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/middleware/isSeller.js"
    click models "https://github.com/joaquindecima/coderhouse-js-ecommerce/tree/master/src/model/"
    click dao "https://github.com/joaquindecima/coderhouse-js-ecommerce/tree/master/src/dao/"
    click cartDAO "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/model/cart/cartMongoManager.js"
    click productDAO "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/src/model/product/productFirebaseManager.js"
    click tools "https://github.com/joaquindecima/coderhouse-js-ecommerce/tree/master/src/tools/"
    click env "https://github.com/joaquindecima/coderhouse-js-ecommerce/blob/master/.env.example"
    click conf "https://github.com/joaquindecima/coderhouse-js-ecommerce/tree/master/config/"
```
