# supanusan/e-commerce_backend architecture

> Architecture diagram of the GitHub repository supanusan/e-commerce_backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/supanusan/e-commerce_backend
- Repository: https://github.com/supanusan/e-commerce_backend

## Overview

##### What this project is (type + purpose)
This is a **Node.js/Express REST API backend** for an **e-commerce**-like system. The README lists API areas: **auth/users**, **products (including admin CRUD)**, **cart**, **orders**, **admin order management**, and **payments** (PayHere init + webhook).
The file tree confirms a typical Express backend structure with:
- `server.js` (app bootstrap)
- `routes/*` (HTTP route modules)
- `middleware/auth.js` (authentication/authorization)
- `config/mongoose.js` (MongoDB connection)
- `config/shema/*` (MongoDB/Mongoose models; “schema” misspelled in path)
- `config/multer.js` and `config/cloudinary.js` (image/file upload pipeline)

Folder structure to architectural layers:

##### Entry / Runtime
- **`server.js`**: The main process that initializes Express, loads middleware, mounts routes, connects to MongoDB.

##### HTTP Interface (Controllers/Routes)
- **`routes/auth.js`** → `/api/auth` (register, login, logout, me)
- **`routes/product.js`** → `/api/products` (list/details/search; admin add/update/delete)
- **`routes/cart.js`** → `/api/cart` (add/update/remove/clear)
- **`routes/order.js`** → `/api/orders` (create, my orders, details)
- **`routes/admin.js`** → `/api/admin` (all orders, update status)
- **`routes/payment.js`** → `/api/payments` (PayHere init, webhook)

##### Middleware / Cross-cutting concerns
- **`middleware/auth.js`**: likely JWT/session validation + role checks (user/admin). This is important to show as a gate in front of protected endpoints.

##### Data layer
- **`config/mongoose.js`**: MongoDB connection + configuration
- **`config/shema/*.js`**:
- `user.js`
- `product.js`
- `cart.js`
- `order.js`

##### External integrations
- **`config/multer.js`**: upload handling (multipart/form-data)
- **`config/cloudinary.js`**: Cloudinary media storage (product images, etc.)
- **PayHere**: called by `/api/payments` routes; also calls you back via webhook.

##### Client(s) (even though not in repo)
- Web app / Mobile app / Admin dashboard / Postman
##### Express API Server (Node.js)
- broken down internally into:
- Route modules: Auth, Users (README mentions `/api/users` though route file isn’t shown; call out as “planned/implicit” or “missing route module” depending on what you find in `server.js`)
- Middleware: Auth middleware
- Domain/Model layer: Mongoose schemas (User/Product/Cart/Order)
##### PayHere Payment Gateway
- “init payment” outbound call
- “webhook callback” inbound call

##### Architectural patterns to highlight (based on repo)
- **Layered architecture**: Routes/Controllers → Middleware → Models/Data access
- **REST resource design**: auth, products, cart, orders, payments
- **External service integration**: Cloudinary + PayHere
- **Event/callback interaction**: PayHere webhook is asynchronous inbound traffic (important to show)

Elements explicitly:

##### Clear labels
- Use names that match the repo: “routes/auth.js”, “middleware/auth.js”, “schema/order.js”, etc.
- Also label endpoint groups: `/api/auth`, `/api/products`, etc.

- Protocol: HTTPS, MongoDB driver
- Payload concept: “JWT”, “Order status update”, “Image URL”

##### Boundaries
- “API Server Process” boundary around routes/middleware/models
- “External Services” boundary around Cloudinary/PayHere
- “Database” boundary around MongoDB

##### Open `server.js` and confirm
- Base path prefixes (`/api/auth`, etc.)
- Which route modules are actually mounted (README mentions `/api/users`; confirm if it exists or is embedded elsewhere)
##### In each `routes/*.js`, confirm
- Which routes are protected by `middleware/auth.js`
- Admin checks (role-based)
##### In payment route
- Confirm webhook endpoint path and how it updates orders.
##### In multer/cloudinary configs
- Confirm whether uploads are used in product admin routes; show that connection.

## Diagram (Mermaid)

```mermaid
flowchart TB

%% =========================
%% Context (C4 L1)
%% =========================
subgraph C4L1["Context (C4 Level 1)"]
  direction TB

  Clients["Client(s)\nWeb/Mobile/Admin/Postman"]:::client
  APIServer["E-commerce REST API\nNode.js + Express"]:::api

  subgraph DB_BOUNDARY["Database"]
    direction TB
    MongoDB[("MongoDB")]:::db
  end

  subgraph EXT_BOUNDARY["External Services"]
    direction TB
    Cloudinary["Cloudinary\nMedia Storage"]:::external
    PayHere["PayHere\nPayment Gateway"]:::external
  end

  Clients -->|"HTTPS REST /api/*"| APIServer
  APIServer -->|"CRUD via Mongoose"| MongoDB

  Clients -->|"multipart/form-data (images)"| APIServer
  APIServer -->|"upload media"| Cloudinary

  Clients -->|"start payment"| APIServer
  APIServer -->|"init payment session"| PayHere
  PayHere -.->|"webhook callback\npayment status"| APIServer
end

%% =========================
%% API Internals (C4 L2/3 hybrid)
%% =========================
subgraph API_PROCESS["API Server Process (Node/Express)"]
  direction TB

  ServerEntry["server.js\nApp Bootstrap\n- init Express\n- mount routes\n- connect DB"]:::api

  subgraph ROUTES["HTTP Routes (Controller Layer) - routes/*"]
    direction TB
    RAuth["/api/auth\nroutes/auth.js"]:::route
    RProduct["/api/products\nroutes/product.js\n(list/search/details + admin CRUD)"]:::route
    RCart["/api/cart\nroutes/cart.js"]:::route
    ROrder["/api/orders\nroutes/order.js"]:::route
    RAdmin["/api/admin\nroutes/admin.js\n(admin order mgmt)"]:::route
    RPayment["/api/payments\nroutes/payment.js\n(init + webhook)"]:::route
  end

  subgraph MIDDLEWARE["Middleware (Cross-cutting)"]
    direction TB
    AuthMW["middleware/auth.js\nJWT Auth + Role Checks\n(user/admin)"]:::middleware
  end

  subgraph DAL["Data Access Layer (Mongoose)"]
    direction TB
    MongooseCfg["config/mongoose.js\nMongoDB Connection"]:::config

    subgraph MODELS["Domain Models - config/shema/*"]
      direction TB
      UserModel["User Model\nconfig/shema/user.js"]:::model
      ProductModel["Product Model\nconfig/shema/product.js"]:::model
      CartModel["Cart Model\nconfig/shema/cart.js"]:::model
      OrderModel["Order Model\nconfig/shema/order.js"]:::model
    end
  end

  subgraph UPLOADS["Uploads & Media"]
    direction TB
    MulterCfg["config/multer.js\nParse uploads"]:::config
    CloudinaryCfg["config/cloudinary.js\nUpload adapter"]:::config
  end

  subgraph SECRETS["Configuration / Secrets"]
    direction TB
    EnvCfg["Environment Variables\n(.env copy)\nDB_URI, JWT_SECRET,\nCloudinary keys, PayHere keys"]:::config
  end

  %% Entry wiring (conceptual)
  ServerEntry -->|"mounts"| RAuth
  ServerEntry -->|"mounts"| RProduct
  ServerEntry -->|"mounts"| RCart
  ServerEntry -->|"mounts"| ROrder
  ServerEntry -->|"mounts"| RAdmin
  ServerEntry -->|"mounts"| RPayment

  ServerEntry -->|"loads"| AuthMW
  ServerEntry -->|"connects via"| MongooseCfg
  ServerEntry -->|"reads"| EnvCfg

  %% Auth middleware gate (protected routes)
  RCart -->|"requires auth"| AuthMW
  ROrder -->|"requires auth"| AuthMW
  RAdmin -->|"requires admin role"| AuthMW
  RProduct -->|"admin CRUD requires admin role"| AuthMW
  RAuth -->|"me/logout requires auth"| AuthMW

  %% Routes -> Models (CRUD)
  RAuth -->|"create/lookup"| UserModel
  RProduct -->|"read/write"| ProductModel
  RCart -->|"read/write"| CartModel
  ROrder -->|"create/query"| OrderModel
  RAdmin -->|"query/update status"| OrderModel
  RPayment -->|"update order status"| OrderModel

  %% Models -> Mongoose -> MongoDB
  UserModel -->|"Mongoose"| MongooseCfg
  ProductModel -->|"Mongoose"| MongooseCfg
  CartModel -->|"Mongoose"| MongooseCfg
  OrderModel -->|"Mongoose"| MongooseCfg
  MongooseCfg -->|"driver connection"| MongoDB

  %% Upload pipeline (typically used by product admin)
  RProduct -->|"handles image upload"| MulterCfg
  MulterCfg -->|"streams file"| CloudinaryCfg
  CloudinaryCfg -->|"HTTPS API"| Cloudinary
  CloudinaryCfg -->|"returns URL/public_id"| RProduct
  RProduct -->|"persist image metadata"| ProductModel

  %% Payments integration
  RPayment -->|"init payment"| PayHere
  PayHere -.->|"webhook -> verify & update"| RPayment
end

%% =========================
%% Key Data Entities (mini model)
%% =========================
subgraph ENTITIES["Key Data Entities (Mini View)"]
  direction TB
  U["User"]:::entity
  P["Product"]:::entity
  C["Cart"]:::entity
  O["Order"]:::entity

  U -->|"owns"| C
  U -->|"places"| O
  C -->|"contains"| P
  O -->|"contains items of"| P
end

%% Tie mini-entities to concrete models
UserModel -->|"represents"| U
ProductModel -->|"represents"| P
CartModel -->|"represents"| C
OrderModel -->|"represents"| O

%% =========================
%% Click Events (from component_mapping)
%% =========================
click ServerEntry "https://github.com/supanusan/e-commerce_backend/blob/main/server.js"
click RAuth "https://github.com/supanusan/e-commerce_backend/blob/main/routes/auth.js"
click RProduct "https://github.com/supanusan/e-commerce_backend/blob/main/routes/product.js"
click RCart "https://github.com/supanusan/e-commerce_backend/blob/main/routes/cart.js"
click ROrder "https://github.com/supanusan/e-commerce_backend/blob/main/routes/order.js"
click RAdmin "https://github.com/supanusan/e-commerce_backend/blob/main/routes/admin.js"
click RPayment "https://github.com/supanusan/e-commerce_backend/blob/main/routes/payment.js"
click AuthMW "https://github.com/supanusan/e-commerce_backend/blob/main/middleware/auth.js"
click MongooseCfg "https://github.com/supanusan/e-commerce_backend/blob/main/config/mongoose.js"
click UserModel "https://github.com/supanusan/e-commerce_backend/blob/main/config/shema/user.js"
click ProductModel "https://github.com/supanusan/e-commerce_backend/blob/main/config/shema/product.js"
click CartModel "https://github.com/supanusan/e-commerce_backend/blob/main/config/shema/cart.js"
click OrderModel "https://github.com/supanusan/e-commerce_backend/blob/main/config/shema/order.js"
click MulterCfg "https://github.com/supanusan/e-commerce_backend/blob/main/config/multer.js"
click CloudinaryCfg "https://github.com/supanusan/e-commerce_backend/blob/main/config/cloudinary.js"
click EnvCfg "https://github.com/supanusan/e-commerce_backend/blob/main/.env copy"

%% =========================
%% Styles
%% =========================
classDef client fill:#E5E7EB,stroke:#6B7280,color:#111827,stroke-width:1px
classDef api fill:#DBEAFE,stroke:#1D4ED8,color:#0B1B3A,stroke-width:1px
classDef route fill:#BFDBFE,stroke:#1D4ED8,color:#0B1B3A,stroke-width:1px
classDef middleware fill:#93C5FD,stroke:#1E40AF,color:#0B1B3A,stroke-width:1px
classDef model fill:#60A5FA,stroke:#1E3A8A,color:#081225,stroke-width:1px
classDef db fill:#BBF7D0,stroke:#15803D,color:#052E16,stroke-width:1px
classDef external fill:#FED7AA,stroke:#C2410C,color:#451A03,stroke-width:1px
classDef config fill:#E0E7FF,stroke:#4338CA,color:#1E1B4B,stroke-width:1px
classDef entity fill:#F1F5F9,stroke:#334155,color:#0F172A,stroke-width:1px
```
