How aditi9975/ecochain works
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: models/index.js Models · routes/index.js API routes · App.tsx SPA shell · server.js Server
Components 24
Backend
Node.js/Express API, business logic, middleware, and persistence
- Server · Bootstraps the API, middleware, routes, errors, and realtime wiring
api/server.js - API routes · Central route composition for the backend
routes/index.js - Auth guard · Authentication and role-based access checks
middleware/auth.js - Middleware · Validation, caching, compression, rate limiting, uploads, logging, and errors
- Controllers · Request handlers for users, marketplace, collections, transactions, admin, and sync
- Auth routes · Login, registration, and session endpoints
routes/authRoutes.js - Marketplace routes · Products, cart, checkout, and order flows
routes/marketplaceRoutes.js - Collection routes · Waste submission and pickup workflow endpoints
routes/garbageCollectionRoutes.js - Admin routes · Admin and approval workflow endpoints
routes/adminRoutes.js - User API · Profiles, auth-adjacent account actions, and user management
controllers/userController.js - Marketplace API · Marketplace, products, orders, and product lifecycle logic
controllers/marketplaceController.js - Waste API · Collection requests, scheduling, tracking, and verification
controllers/garbageCollectionController.js - Token API · Wallet, transactions, and EcoToken accounting
controllers/transactionController.js - Admin API · Factory onboarding, approvals, and administrative operations
controllers/adminController.js
Frontend
React SPA, UI state, and API integration
- SPA shell · Top-level routed client application
client/src/App.tsx - Auth state · Client-side session and role state
client/src/context/AuthContext.tsx - API layer · Centralized HTTP integration for domain services
client/src/services/api.ts - Screens · Route-level UI for dashboard, marketplace, wallet, orders, collections, and admin flows
client/src/pages/Marketplace.tsx - Build output · Prebuilt production frontend artifact
client/build
Data & storage
MongoDB models and file asset storage
Ops & realtime
Deploy/runtime integration and live-update channels
- Live sync · Synchronization and status updates for connected clients
controllers/syncController.js - Realtime client · Client connection and sync services for live updates
client/src/services/websocketService.ts
Connections 27
- 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