# aditi9975/ecochain architecture

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

- Interactive diagram: https://gitdiagram.com/aditi9975/ecochain
- Repository: https://github.com/aditi9975/ecochain

## Overview

1. **Overall shape**
   - EcoChain is a **two-tier application**: a **Node.js/Express backend** and a **React 18 + TypeScript frontend**.
   - The backend owns business logic, persistence, authentication, real-time updates, and file uploads.
   - The frontend is a single-page app that talks to the backend over HTTP and subscribes to live status updates.

2. **Backend boundary**
   - The root `server.js` boots the API server and wires together routes, middleware, logging, error handling, and real-time channels.
   - The backend is organized as a classic layered Express app:
     - `routes/` defines HTTP endpoints
     - `controllers/` contains request handlers and use-case logic
     - `database/models/` holds MongoDB/Mongoose entities
     - `middleware/` handles auth, validation, caching, compression, rate limiting, uploads, logging, monitoring, and errors
     - `config/` and `database/` handle environment, security, and DB connection/setup

3. **Frontend boundary**
   - The frontend in `client/src` is a routed SPA with page-level screens and reusable UI components.
   - `App.tsx`, `pages/`, and `components/` define the user experience; `contexts/` and `services/` manage state and API integration.
   - The built frontend in `client/build` suggests a static production artifact deployed separately from the API.

4. **Core business subsystems**
   - The domain is split into five major areas:
     - **Auth / user management**: registration, login, profiles, role-based access
     - **Waste collection workflow**: submit, schedule, track, verify collections
     - **Marketplace / e-commerce**: browse products, cart, checkout, orders, reviews
     - **EcoToken economy**: wallet, transactions, token awarding, earning opportunities
     - **Factory/admin operations**: factory onboarding, product management, material requests, approvals, dashboards

5. **Data model and persistence**
   - MongoDB is the primary datastore, accessed through Mongoose models in `database/models/`.
   - The schema includes collections for `User`, `Factory`, `Product`, `Order`, `Transaction`, `EcoTokenTransaction`, `GarbageCollection`, `CollectorApplication`, `FactoryApplication`, `MaterialRequest`, `ProductReview`, `SystemConfiguration`, `Analytics`, and related admin/payment records.
   - This means most business state is persisted in MongoDB rather than in-memory services.

6. **Request flow**
   - A typical request enters via `routes/`, passes through middleware for authentication/validation/rate limiting, then reaches a controller.
   - Controllers read/write Mongoose models and may trigger side effects such as token accounting, order status changes, or sync events.
   - The frontend consumes these endpoints through the service layer in `client/src/services/`, which centralizes API calls by domain.

7. **Real-time and synchronization paths**
   - The repo supports **WebSocket** and **SSE**-style updates.
   - `utils/socketManager.js`, `services/websocketService.ts`, `services/sseService.ts`, `controllers/sseController.js`, and `services/syncService.ts` indicate live updates for connection status, synchronization, and operational events.
   - This is important for architecture diagrams because some state changes are not purely pull-based API calls.

8. **Identity, authorization, and roles**
   - Authentication is JWT-based with refresh-token support, backed by bcrypt password hashing.
   - Authorization is role-driven across at least four roles: **user, collector, factory, admin**.
   - `middleware/auth.js`, `client/src/context/AuthContext.tsx`, and `client/src/components/ProtectedRoute.tsx` show that both backend and frontend enforce access boundaries.

9. **Operational features**
   - The platform includes infrastructure-oriented concerns in the app itself:
     - request logging and monitoring
     - caching
     - compression
     - input validation and sanitization
     - rate limiting
     - upload handling for images/profile/product assets
   - User-uploaded media is stored under `public/uploads/`, while the frontend also has image assets in `client/public/images/`.

10. **Frontend state and UX structure**
   - The frontend uses context-based state for auth, cart, eco-chain data, and sync status.
   - Domain-specific hooks and helpers support loading/error handling, API error parsing, websocket status, image stability, and connection tests.
   - Page coverage is broad: dashboard, marketplace, cart/checkout, orders, profile, wallet, collections/waste submission, factory/admin dashboards, and approval flows.

11. **Deployment topology**
   - The repo is prepared for separate deployments:
     - backend on Render-like Node hosting
     - frontend on Vercel/Netlify as static React build
   - `render.yaml`, `vercel.json`, `netlify.toml`, and `nginx.conf` indicate the app can be reverse-proxied and deployed in multiple hosting models.

12. **Notable integration surfaces**
   - External dependencies that materially affect the architecture are:
     - **MongoDB Atlas** for persistence
     - **JWT** for session control
     - **WebSocket/SSE** for live updates
   - The repository does not appear to rely on a separate microservice fleet; instead, it is a **modular monolith backend** with a rich frontend client.

13. **Diagramming takeaway**
   - A clear architecture diagram should show:
     - Users/collectors/factories/admins interacting with the React SPA
     - SPA calling Express REST endpoints
     - Express controllers using Mongoose models on MongoDB
     - Separate live-update channel via WebSocket/SSE
     - File uploads flowing into the server’s upload/public asset storage
     - Role-based authorization and token economy logic spanning multiple backend domains

Main entry points: [`database/models/index.js`](https://github.com/aditi9975/ecochain/blob/main/database/models/index.js) (Models), [`routes/index.js`](https://github.com/aditi9975/ecochain/blob/main/routes/index.js) (API routes), [`client/src/App.tsx`](https://github.com/aditi9975/ecochain/blob/main/client/src/App.tsx) (SPA shell), [`api/server.js`](https://github.com/aditi9975/ecochain/blob/main/api/server.js) (Server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_api_server["Server<br/>Express bootstrap<br/>[server.js]"]
  node_routes_index["API routes<br/>Route hub<br/>[index.js]"]
  node_middleware_auth["Auth guard<br/>JWT auth middleware<br/>[auth.js]"]
  node_middleware_stack["Middleware<br/>HTTP pipeline"]
  node_controllers_core["Controllers<br/>Use-case layer"]
  node_routes_auth["Auth routes<br/>REST routes<br/>[authRoutes.js]"]
  node_routes_market["Marketplace routes<br/>REST routes"]
  node_routes_waste["Collection routes<br/>REST routes"]
  node_routes_admin["Admin routes<br/>REST routes<br/>[adminRoutes.js]"]
  node_controllers_user["User API<br/>User controller<br/>[userController.js]"]
  node_controllers_market["Marketplace API<br/>Commerce controller"]
  node_controllers_waste["Waste API<br/>Collection controller"]
  node_controllers_tx["Token API<br/>Economy controller"]
  node_controllers_admin["Admin API<br/>Admin controller<br/>[adminController.js]"]
end

subgraph group_frontend["Frontend"]
  node_client_app["SPA shell<br/>React app<br/>[App.tsx]"]
  node_client_auth["Auth state<br/>React context<br/>[AuthContext.tsx]"]
  node_client_api["API layer<br/>Service layer<br/>[api.ts]"]
  node_client_market["Screens<br/>Pages and components<br/>[Marketplace.tsx]"]
  node_client_build["Build output<br/>Static bundle"]
end

subgraph group_data["Data & storage"]
  node_db_models[("Models<br/>[index.js]")]
  node_config_db["DB config<br/>Mongo setup<br/>[database.js]"]
  node_uploads["Uploads<br/>Asset storage"]
end

subgraph group_infra["Ops & realtime"]
  node_controllers_sync["Live sync<br/>Sync controller<br/>[syncController.js]"]
  node_client_realtime["Realtime client<br/>WebSocket/SSE services"]
end

node_api_server -->|"mounts"| node_routes_index
node_api_server -->|"uses"| node_middleware_stack
node_api_server -->|"connects"| node_config_db
node_api_server -->|"wires realtime"| node_controllers_sync
node_routes_index -->|"includes"| node_routes_auth
node_routes_index -->|"includes"| node_routes_market
node_routes_index -->|"includes"| node_routes_waste
node_routes_index -->|"includes"| node_routes_admin
node_routes_auth -->|"guards"| node_middleware_auth
node_routes_market -->|"handles"| node_controllers_market
node_routes_waste -->|"handles"| node_controllers_waste
node_routes_admin -->|"handles"| node_controllers_admin
node_controllers_user -->|"reads/writes"| node_db_models
node_controllers_market -->|"reads/writes"| node_db_models
node_controllers_waste -->|"reads/writes"| node_db_models
node_controllers_tx -->|"reads/writes"| node_db_models
node_controllers_admin -->|"reads/writes"| node_db_models
node_controllers_sync -->|"observes"| node_db_models
node_controllers_market -->|"stores media"| node_uploads
node_client_app -->|"uses"| node_client_auth
node_client_app -->|"calls"| node_client_api
node_client_app -->|"subscribes"| node_client_realtime
node_client_app -->|"renders"| node_client_market
node_client_api -->|"requests"| node_routes_index
node_client_realtime -->|"listens to"| node_controllers_sync
node_db_models -->|"backed by"| node_config_db
node_client_build -->|"built from"| node_client_app

click node_api_server "https://github.com/aditi9975/ecochain/blob/main/api/server.js"
click node_routes_index "https://github.com/aditi9975/ecochain/blob/main/routes/index.js"
click node_middleware_auth "https://github.com/aditi9975/ecochain/blob/main/middleware/auth.js"
click node_routes_auth "https://github.com/aditi9975/ecochain/blob/main/routes/authRoutes.js"
click node_routes_market "https://github.com/aditi9975/ecochain/blob/main/routes/marketplaceRoutes.js"
click node_routes_waste "https://github.com/aditi9975/ecochain/blob/main/routes/garbageCollectionRoutes.js"
click node_routes_admin "https://github.com/aditi9975/ecochain/blob/main/routes/adminRoutes.js"
click node_controllers_user "https://github.com/aditi9975/ecochain/blob/main/controllers/userController.js"
click node_controllers_market "https://github.com/aditi9975/ecochain/blob/main/controllers/marketplaceController.js"
click node_controllers_waste "https://github.com/aditi9975/ecochain/blob/main/controllers/garbageCollectionController.js"
click node_controllers_tx "https://github.com/aditi9975/ecochain/blob/main/controllers/transactionController.js"
click node_controllers_admin "https://github.com/aditi9975/ecochain/blob/main/controllers/adminController.js"
click node_controllers_sync "https://github.com/aditi9975/ecochain/blob/main/controllers/syncController.js"
click node_db_models "https://github.com/aditi9975/ecochain/blob/main/database/models/index.js"
click node_config_db "https://github.com/aditi9975/ecochain/blob/main/config/database.js"
click node_uploads "https://github.com/aditi9975/ecochain/tree/main/public/uploads"
click node_client_app "https://github.com/aditi9975/ecochain/blob/main/client/src/App.tsx"
click node_client_auth "https://github.com/aditi9975/ecochain/blob/main/client/src/context/AuthContext.tsx"
click node_client_api "https://github.com/aditi9975/ecochain/blob/main/client/src/services/api.ts"
click node_client_realtime "https://github.com/aditi9975/ecochain/blob/main/client/src/services/websocketService.ts"
click node_client_market "https://github.com/aditi9975/ecochain/blob/main/client/src/pages/Marketplace.tsx"
click node_client_build "https://github.com/aditi9975/ecochain/tree/main/client/build"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_server,node_routes_index,node_middleware_auth,node_middleware_stack,node_controllers_core,node_routes_auth,node_routes_market,node_routes_waste,node_routes_admin,node_controllers_user,node_controllers_market,node_controllers_waste,node_controllers_tx,node_controllers_admin toneBlue
class node_client_app,node_client_auth,node_client_api,node_client_market,node_client_build toneAmber
class node_db_models,node_config_db,node_uploads toneMint
class node_controllers_sync,node_client_realtime toneRose
```

## Components

### Backend

Node.js/Express API, business logic, middleware, and persistence

- [Server](https://github.com/aditi9975/ecochain/blob/main/api/server.js): Express bootstrap · Bootstraps the API, middleware, routes, errors, and realtime wiring · `api/server.js`
- [API routes](https://github.com/aditi9975/ecochain/blob/main/routes/index.js): Route hub · Central route composition for the backend · `routes/index.js`
- [Auth guard](https://github.com/aditi9975/ecochain/blob/main/middleware/auth.js): JWT auth middleware · Authentication and role-based access checks · `middleware/auth.js`
- **Middleware**: HTTP pipeline · Validation, caching, compression, rate limiting, uploads, logging, and errors
- **Controllers**: Use-case layer · Request handlers for users, marketplace, collections, transactions, admin, and sync
- [Auth routes](https://github.com/aditi9975/ecochain/blob/main/routes/authRoutes.js): REST routes · Login, registration, and session endpoints · `routes/authRoutes.js`
- [Marketplace routes](https://github.com/aditi9975/ecochain/blob/main/routes/marketplaceRoutes.js): REST routes · Products, cart, checkout, and order flows · `routes/marketplaceRoutes.js`
- [Collection routes](https://github.com/aditi9975/ecochain/blob/main/routes/garbageCollectionRoutes.js): REST routes · Waste submission and pickup workflow endpoints · `routes/garbageCollectionRoutes.js`
- [Admin routes](https://github.com/aditi9975/ecochain/blob/main/routes/adminRoutes.js): REST routes · Admin and approval workflow endpoints · `routes/adminRoutes.js`
- [User API](https://github.com/aditi9975/ecochain/blob/main/controllers/userController.js): User controller · Profiles, auth-adjacent account actions, and user management · `controllers/userController.js`
- [Marketplace API](https://github.com/aditi9975/ecochain/blob/main/controllers/marketplaceController.js): Commerce controller · Marketplace, products, orders, and product lifecycle logic · `controllers/marketplaceController.js`
- [Waste API](https://github.com/aditi9975/ecochain/blob/main/controllers/garbageCollectionController.js): Collection controller · Collection requests, scheduling, tracking, and verification · `controllers/garbageCollectionController.js`
- [Token API](https://github.com/aditi9975/ecochain/blob/main/controllers/transactionController.js): Economy controller · Wallet, transactions, and EcoToken accounting · `controllers/transactionController.js`
- [Admin API](https://github.com/aditi9975/ecochain/blob/main/controllers/adminController.js): Admin controller · Factory onboarding, approvals, and administrative operations · `controllers/adminController.js`

### Frontend

React SPA, UI state, and API integration

- [SPA shell](https://github.com/aditi9975/ecochain/blob/main/client/src/App.tsx): React app · Top-level routed client application · `client/src/App.tsx`
- [Auth state](https://github.com/aditi9975/ecochain/blob/main/client/src/context/AuthContext.tsx): React context · Client-side session and role state · `client/src/context/AuthContext.tsx`
- [API layer](https://github.com/aditi9975/ecochain/blob/main/client/src/services/api.ts): Service layer · Centralized HTTP integration for domain services · `client/src/services/api.ts`
- [Screens](https://github.com/aditi9975/ecochain/blob/main/client/src/pages/Marketplace.tsx): Pages and components · Route-level UI for dashboard, marketplace, wallet, orders, collections, and admin flows · `client/src/pages/Marketplace.tsx`
- [Build output](https://github.com/aditi9975/ecochain/tree/main/client/build): Static bundle · Prebuilt production frontend artifact · `client/build`

### Data & storage

MongoDB models and file asset storage

- [Models](https://github.com/aditi9975/ecochain/blob/main/database/models/index.js): Mongoose models · MongoDB entity layer for users, factories, orders, transactions, collections, and reviews · `database/models/index.js`
- [DB config](https://github.com/aditi9975/ecochain/blob/main/config/database.js): Mongo setup · Database connection and initialization configuration · `config/database.js`
- [Uploads](https://github.com/aditi9975/ecochain/tree/main/public/uploads): Asset storage · Server-side image and media storage for profiles and products · `public/uploads`

### Ops & realtime

Deploy/runtime integration and live-update channels

- [Live sync](https://github.com/aditi9975/ecochain/blob/main/controllers/syncController.js): Sync controller · Synchronization and status updates for connected clients · `controllers/syncController.js`
- [Realtime client](https://github.com/aditi9975/ecochain/blob/main/client/src/services/websocketService.ts): WebSocket/SSE services · Client connection and sync services for live updates · `client/src/services/websocketService.ts`

## Connections

- Server → API routes: mounts · Registers the API route tree
- Server → Middleware: uses · Applies request pipeline concerns
- Server → DB config: connects · Initializes database connectivity
- Server → Live sync: wires realtime · Exposes synchronization and live events
- API routes → Auth routes: includes · Composes auth endpoints
- API routes → Marketplace routes: includes · Composes marketplace endpoints
- API routes → Collection routes: includes · Composes collection endpoints
- API routes → Admin routes: includes · Composes admin endpoints
- Auth routes → Auth guard: guards · Protects identity-sensitive operations
- Marketplace routes → Marketplace API: handles · Delegates commerce requests to controllers
- Collection routes → Waste API: handles · Delegates collection workflow requests
- Admin routes → Admin API: handles · Delegates admin and approval requests
- User API → Models: reads/writes · Persists account state
- Marketplace API → Models: reads/writes · Persists commerce state
- Waste API → Models: reads/writes · Persists collection state
- Token API → Models: reads/writes · Persists token and transaction state
- Admin API → Models: reads/writes · Persists admin, factory, and approval state
- Live sync → Models: observes · Uses persisted state to broadcast sync events
- Marketplace API → Uploads: stores media · Handles product and profile asset uploads
- SPA shell → Auth state: uses · Shares client session state
- SPA shell → API layer: calls · Fetches backend data through services
- SPA shell → Realtime client: subscribes · Receives live updates
- SPA shell → Screens: renders · Navigates between page-level screens
- API layer → API routes: requests · Sends HTTP traffic to the API
- Realtime client → Live sync: listens to · Consumes sync and live-status events
- Models → DB config: backed by · MongoDB connection and setup
- Build output → SPA shell: built from · Production artifact generated from the React app
