# rishibakshii/mern-ecommerce architecture

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

- Interactive diagram: https://gitdiagram.com/rishibakshii/mern-ecommerce
- Repository: https://github.com/rishibakshii/mern-ecommerce

## Overview

##### Project type and purpose
- This is a full-stack MERN ecommerce application.
- Frontend: React + Redux Toolkit + Material UI SPA
- Backend: Node.js + Express REST API
- Database: MongoDB (via Mongoose)
- External services: SMTP/email provider for OTP & password resets

##### File structure
- Top-level “backend” folder
– routes/ → Express route definitions (one file per resource: Auth, Product, Order, etc.)
– controllers/ → business logic handlers invoked by routes (CRUD, validation)
– models/ → Mongoose schemas for User, Product, Order, Review, Cart, Wishlist, Address, OTP, PasswordResetToken
– middleware/ → VerifyToken (JWT auth)
– database/db.js → MongoDB connection setup
– utils/ → token generation, OTP/email utilities, user sanitization
– seed/ → sample data scripts to populate collections
– index.js → Express app bootstrapping, middleware registration, route mounting
- Top-level “frontend” folder
– src/app/store.js → Redux store configuration
– src/features/… → Redux “slices” and RTK Query APIs for each domain (auth, products, cart, orders, reviews, wishlist, addresses, brands, categories, user)
– src/features/**/components & src/pages → React components & pages for each feature
– src/config/axios.js → configured Axios instance pointing at backend
– src/hooks/useAuth → custom hooks for session checks
– src/layout/RootLayout.js → global layout (Navbar, Footer)
– src/assets & src/theme → static assets and Material UI theme

##### README insights
- Confirms MERN stack, Redux Toolkit, Material UI
- Highlights features for both User and Admin
- Mentions OTP/email flows, JWT auth, seeding, environment variables

##### Main components
A. Client (Browser / React SPA)
- UI Layer (Material UI components)
- Presentation Layer (pages, layouts)
- State Layer (Redux store, slices, RTK Query)
B. API Server (Express)
- HTTP Layer (routes)
- Controller Layer (controllers folder)
- Business Logic / Utilities (utils for tokens, emails, sanitization)
- Security Layer (VerifyToken middleware)
C. Data Layer
- MongoDB collections (Users, Products, Orders, Reviews, Cart, Wishlist, Addresses, OTPs, PasswordResetTokens)
D. External Services
- Email/SMTP provider for OTPs & password resets
E. DevOps / Deployment (optional)
- Vercel config (backend/vercel.json)
- Environment variables (.env files)

##### Relationships and data flow
1. User interacts with React SPA → dispatches Redux actions / RTK Query calls → Axios issues HTTP requests (GET/POST/PUT/DELETE)
2. Express routes receive requests → pass through VerifyToken (for protected routes) → route handlers invoke controllers
3. Controllers call Mongoose models → perform CRUD on MongoDB
4. Controllers use utils to generate JWT, OTP, send emails via SMTP provider
5. MongoDB sends back documents → controllers sanitize output → responses flow back to client → Redux updates UI

##### Architectural patterns & design principles
- MVC-inspired layering on backend (routes → controllers → models)
- Slice-based architecture on frontend (feature folder per domain, co-locating API calls, slice logic, and UI)
- Single-Page Application with client-side routing (React Router implied in pages setup)
- Stateless REST API with JWT session management
- Data seeding utility for rapid dev/testing

##### Detail level—capture all essential modules
- Frontend: store.js, feature slices & APIs, UI components, hooks, config
- Backend: routes, controllers, models, middleware, utils, seed script
- Data: collection names
- External: email/SMTP provider

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Side
    subgraph "Client (Browser) - React SPA"
        direction TB
        Index["index.js"]:::frontend
        App["App.js"]:::frontend
        PublicHTML["index.html"]:::frontend
        Index --> App
        App --> PublicHTML
    end

    %% UI & State Layer
    subgraph "Frontend: UI & State" 
        direction TB
        Store["store.js"]:::frontend
        subgraph "Feature Slices & APIs"
            direction TB
            AuthSlice["AuthSlice.jsx"]:::frontend
            AuthApi["AuthApi.jsx"]:::frontend
            ProductSlice["ProductSlice.jsx"]:::frontend
            ProductApi["ProductApi.jsx"]:::frontend
            CartSlice["CartSlice.jsx"]:::frontend
            CartApi["CartApi.jsx"]:::frontend
            OrderSlice["OrderSlice.jsx"]:::frontend
            OrderApi["OrderApi.jsx"]:::frontend
            ReviewSlice["ReviewSlice.jsx"]:::frontend
            ReviewApi["ReviewApi.jsx"]:::frontend
            WishlistSlice["WishlistSlice.jsx"]:::frontend
            WishlistApi["WishlistApi.jsx"]:::frontend
            AddressSlice["AddressSlice.jsx"]:::frontend
            AddressApi["AddressApi.jsx"]:::frontend
            BrandSlice["BrandSlice.jsx"]:::frontend
            BrandApi["BrandApi.jsx"]:::frontend
            CategorySlice["CategoriesSlice.jsx"]:::frontend
            CategoryApi["CategoriesApi.jsx"]:::frontend
            UserSlice["UserSlice.jsx"]:::frontend
            UserApi["UserApi.jsx"]:::frontend
        end
        AxiosConfig["axios.js"]:::frontend
        Hooks["useAuth hooks"]:::frontend
        Layout["RootLayout.js"]:::frontend
        Pages["Page Components"]:::frontend
        UIComp["Material UI Components"]:::frontend

        App --> Store
        Store --> AuthSlice
        AuthSlice --> AuthApi
        Store --> ProductSlice
        ProductSlice --> ProductApi
        Store --> CartSlice
        CartSlice --> CartApi
        Store --> OrderSlice
        OrderSlice --> OrderApi
        Store --> ReviewSlice
        ReviewSlice --> ReviewApi
        Store --> WishlistSlice
        WishlistSlice --> WishlistApi
        Store --> AddressSlice
        AddressSlice --> AddressApi
        Store --> BrandSlice
        BrandSlice --> BrandApi
        Store --> CategorySlice
        CategorySlice --> CategoryApi
        Store --> UserSlice
        UserSlice --> UserApi
        App --> AxiosConfig
        App --> Hooks
        App --> Layout
        Layout --> Pages
        Pages --> UIComp
    end

    %% Backend Side
    subgraph "Backend: Express API Server"
        direction TB
        Server["index.js"]:::backend
        Routes["routes/*.js"]:::backend
        Controllers["controllers/*.js"]:::backend
        Middleware["VerifyToken.js"]:::backend
        Utils["utils/*"]:::backend
        DBConnect["db.js"]:::backend
        Seed["seed scripts"]:::backend
        VercelConfig["vercel.json"]:::backend

        Server --> Routes
        Routes --> Middleware
        Routes --> Controllers
        Controllers --> Utils
        Server --> DBConnect
        Server --> Seed
        Server --> VercelConfig
    end

    %% Data Layer
    subgraph "Database: MongoDB"
        direction TB
        MongoDB["MongoDB Collections"]:::db
    end

    %% External Services
    subgraph "External Services"
        direction TB
        EmailService["SMTP/Email Provider"]:::external
    end

    %% Data Flows
    UIComp -->|"REST API calls via Axios"| Server
    Server -->|"Mongoose CRUD Operations"| MongoDB
    Server -->|"Send OTP/Emails"| EmailService
    MongoDB -->|"Documents"| Controllers
    Controllers -->|"Sanitized Responses"| UIComp

    %% Click Events
    click Index "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/index.js"
    click App "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/App.js"
    click PublicHTML "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/public/index.html"
    click Store "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/app/store.js"
    click AuthSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/auth/AuthSlice.jsx"
    click AuthApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/auth/AuthApi.jsx"
    click ProductSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/products/ProductSlice.jsx"
    click ProductApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/products/ProductApi.jsx"
    click CartSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/cart/CartSlice.jsx"
    click CartApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/cart/CartApi.jsx"
    click OrderSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/order/OrderSlice.jsx"
    click OrderApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/order/OrderApi.jsx"
    click ReviewSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/review/ReviewSlice.jsx"
    click ReviewApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/review/ReviewApi.jsx"
    click WishlistSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/wishlist/WishlistSlice.jsx"
    click WishlistApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/wishlist/WishlistApi.jsx"
    click AddressSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/address/AddressSlice.jsx"
    click AddressApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/address/AddressApi.jsx"
    click BrandSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/brands/BrandSlice.jsx"
    click BrandApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/brands/BrandApi.jsx"
    click CategorySlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/categories/CategoriesSlice.jsx"
    click CategoryApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/categories/CategoriesApi.jsx"
    click UserSlice "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/user/UserSlice.jsx"
    click UserApi "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/features/user/UserApi.jsx"
    click AxiosConfig "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/config/axios.js"
    click Hooks "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/hooks/useAuth/useAuthCheck.js"
    click Layout "https://github.com/rishibakshii/mern-ecommerce/blob/main/frontend/src/layout/RootLayout.js"
    click Server "https://github.com/rishibakshii/mern-ecommerce/blob/main/backend/index.js"
    click Routes "https://github.com/rishibakshii/mern-ecommerce/tree/main/backend/routes"
    click Controllers "https://github.com/rishibakshii/mern-ecommerce/tree/main/backend/controllers"
    click Middleware "https://github.com/rishibakshii/mern-ecommerce/blob/main/backend/middleware/VerifyToken.js"
    click Utils "https://github.com/rishibakshii/mern-ecommerce/blob/main/backend/utils/GenerateToken.js"
    click DBConnect "https://github.com/rishibakshii/mern-ecommerce/blob/main/backend/database/db.js"
    click Seed "https://github.com/rishibakshii/mern-ecommerce/blob/main/backend/seed/seed.js"
    click VercelConfig "https://github.com/rishibakshii/mern-ecommerce/blob/main/backend/vercel.json"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366D6;
    classDef backend fill:#D1F7C4,stroke:#228B22;
    classDef db fill:#FFD8B1,stroke:#FF8C00;
    classDef external fill:#EAD1DC,stroke:#800080;
```
