# raunakgarg05/onlineshoppingsite architecture

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

- Interactive diagram: https://gitdiagram.com/raunakgarg05/onlineshoppingsite
- Repository: https://github.com/raunakgarg05/onlineshoppingsite

## Overview

##### Project Type and Purpose
- This is a full-stack e-commerce web application built with Node.js and Express.js on the backend, EJS templates for server-side rendered views, and MongoDB (via Mongoose) as the primary database. It supports user authentication, shopping cart, orders, invoice generation, email notifications (SendGrid), and payment processing (Stripe).

##### File Structure Analysis
- Top-Level Files
- app.js – main Express application entrypoint
- package.json / package-lock.json – dependencies and scripts
- .gitignore, README.md
- Directories
- controllers/ – request handlers for admin, auth, shop, error
- routes/ – route definitions mapping URLs to controllers
- models/ – Mongoose schemas for Product, User, Order
- middleware/ – authentication guard
- util/ – file and path helpers
- data/ – local JSON data (cart, products) and generated PDF invoices
- public/ – static assets: CSS, JS, images
- views/ – EJS templates organized by feature (auth, admin, shop, includes)

##### Key Components
a. Client Layer
- Browser / User interface
- Static assets (public/css, public/js, images) delivered via Express’s static middleware
- EJS-rendered HTML views
b. Server Layer (Express Application)
- app.js (Express instance)
- Middleware stack: session handling, CSRF protection, body parsing, custom is-auth
- Route handlers: routes/auth, routes/shop, routes/admin
- Controllers: business logic for authentication, product management, cart, orders, error handling
- Utility services: file helper (saving invoices), path helper
c. Data Layer
- MongoDB Atlas (users, products, orders collections via Mongoose)
- Local JSON files for cart and products (fallback or seed data)
- File system storage for PDF invoices
d. External Services
- SendGrid Email API – signup confirmation, password reset
- Stripe API – payment processing

##### Relationships and Data Flow
- User → Browser → HTTP Request → Express routes
- Express middleware applies security, sessions → passes to controllers
- Controllers → Mongoose models → MongoDB (read/write)
- Controllers → util/file → PDF invoices written to disk → served back to user or emailed
- Controllers call external APIs:
- SendGrid (email delivery)
- Stripe (payment authorization)
- Express → renders EJS views injecting dynamic data → HTTP response → Browser

##### Architectural Patterns and Design Principles
- MVC-inspired structure: Routes → Controllers (C) → Models (M) → Views (V)
- Separation of concerns: middleware concerns vs. business logic vs. data access
- Externalized configuration (API keys, database URI) via environment variables
- Use of RESTful routes for shop/admin/auth
- File-based PDF invoice generation as a service layer

## Diagram (Mermaid)

```mermaid
flowchart TB
  subgraph "Client Layer"
    Browser("Browser / User Interface"):::frontend
    StaticAssets("Static Assets"):::frontend
    Views("EJS Views"):::frontend
  end

  subgraph "Server Layer"
    App("Express App (app.js)"):::backend
    Middleware("Session, CSRF, BodyParser,\nCustom is-auth"):::backend
    subgraph "Route Definitions"
      AuthRoutes("Auth Routes"):::backend
      ShopRoutes("Shop Routes"):::backend
      AdminRoutes("Admin Routes"):::backend
    end
    subgraph "Controllers"
      AuthController("Auth Controller"):::backend
      ShopController("Shop Controller"):::backend
      AdminController("Admin Controller"):::backend
      ErrorController("Error Controller"):::backend
    end
    subgraph "Utilities"
      FileHelper("File Helper"):::backend
      PathHelper("Path Helper"):::backend
    end
    Config("Config & Dependencies"):::backend
  end

  subgraph "Data Layer"
    MongoDB("MongoDB Atlas"):::data
    ProductModel("Product Model"):::data
    UserModel("User Model"):::data
    OrderModel("Order Model"):::data
    ProductsJSON("Products JSON"):::data
    CartJSON("Cart JSON"):::data
    InvoiceFiles("Invoice Files (.pdf)"):::data
  end

  subgraph "External Services"
    SendGrid("SendGrid Email API"):::external
    Stripe("Stripe Payment API"):::external
  end

  %% Connections
  Browser -->|"HTTP Request"| App
  App -->|"serve static"| StaticAssets
  App -->|"render views"| Views
  App -->|"use middleware"| Middleware
  App -->|"use routes"| AuthRoutes
  App -->|"use routes"| ShopRoutes
  App -->|"use routes"| AdminRoutes

  AuthRoutes -->|"calls"| AuthController
  ShopRoutes -->|"calls"| ShopController
  AdminRoutes -->|"calls"| AdminController

  AuthController -->|"validateCredentials()"| UserModel
  AuthController -->|"sendEmail()"| SendGrid
  ShopController -->|"findAll()"| ProductModel
  ShopController -->|"addToCart()"| CartJSON
  ShopController -->|"createOrder()"| OrderModel
  ShopController -->|"generateInvoice()"| FileHelper
  ShopController -->|"processPayment()"| Stripe
  ShopController -->|"sendEmail()"| SendGrid
  AdminController -->|"CRUD operations"| ProductModel
  AuthController -->|"error flow"| ErrorController
  ShopController -->|"error flow"| ErrorController
  AdminController -->|"error flow"| ErrorController

  FileHelper -->|"write PDF"| InvoiceFiles
  FileHelper -->|"use path"| PathHelper

  ProductModel <-->|"read/write"| MongoDB
  UserModel <-->|"read/write"| MongoDB
  OrderModel <-->|"read/write"| MongoDB

  %% Click Events
  click StaticAssets "https://github.com/raunakgarg05/onlineshoppingsite/tree/main/public/css/"
  click Views "https://github.com/raunakgarg05/onlineshoppingsite/tree/main/views/"
  click App "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/app.js"
  click Middleware "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/middleware/is-auth.js"
  click AuthRoutes "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/routes/auth.js"
  click ShopRoutes "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/routes/shop.js"
  click AdminRoutes "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/routes/admin.js"
  click AuthController "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/controllers/auth.js"
  click ShopController "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/controllers/shop.js"
  click AdminController "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/controllers/admin.js"
  click ErrorController "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/controllers/error.js"
  click FileHelper "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/util/file.js"
  click PathHelper "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/util/path.js"
  click ProductModel "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/models/product.js"
  click UserModel "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/models/user.js"
  click OrderModel "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/models/order.js"
  click ProductsJSON "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/data/products.json"
  click CartJSON "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/data/cart.json"
  click InvoiceFiles "https://github.com/raunakgarg05/onlineshoppingsite/tree/main/data/invoices/"
  click Config "https://github.com/raunakgarg05/onlineshoppingsite/blob/main/package.json"

  %% Styles
  classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:1px
  classDef backend fill:#d4edda,stroke:#155724,stroke-width:1px
  classDef data fill:#fff3cd,stroke:#856404,stroke-width:1px
  classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:1px
```
