# amit2003dh/kisan-setu architecture

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

- Interactive diagram: https://gitdiagram.com/amit2003dh/kisan-setu
- Repository: https://github.com/amit2003dh/kisan-setu

## Overview

#### Purpose and deployable surfaces
KisanSetu is a multi-role agricultural marketplace: farmers list crops, sellers manage products, buyers purchase, delivery partners fulfill orders, and admins oversee the platform. It has two React single-page clients and one Node/Express backend:
- Customer/role application: `frontend/src/index.js`, `frontend/src/App.js`
- Separate admin application: `admin/src/index.js`, `admin/src/App.js`
- API and realtime server: `backend/server.js`, `backend/crop-analysis-server.js`

#### Client application and role routing
The main React app owns role-specific dashboards and transactional workflows, using API access and route protection as its client boundary. Its pages cover crop/product listing, carts, orders, payment, delivery, crop analysis, and messaging.
- Application composition: `frontend/src/App.js`
- Shared authenticated access: `frontend/src/components/ProtectedRoute.js`
- API client boundary: `frontend/src/api/api.js`
- Client state for shopping: `frontend/src/context/CartContext.js`

#### Marketplace catalog and order lifecycle
The backend exposes resource routes for users, crops, products, carts/orders, tracking, and delivery. MongoDB models represent persistent marketplace state; orders bridge buyer checkout, seller/farmer fulfillment, and delivery assignment/tracking.
- Route layer: `backend/routes/cropRoutes.js`, `backend/routes/productRoutes.js`, `backend/routes/orderRoutes.js`
- Core persisted entities: `backend/models/Crop.js`, `backend/models/Product.js`, `backend/models/Order.js`
- Delivery/tracking domain: `backend/routes/deliveryRoutes.js`, `backend/routes/trackerRoutes.js`, `backend/models/Delivery.js`

#### Identity, authorization, and administration
JWT-backed authentication protects user and administrative operations. The main UI distinguishes operational roles, while the independently deployed admin app handles user, order, production, delivery-partner verification, and analytics oversight.
- User/auth endpoints: `backend/routes/userRoutes.js`, `backend/models/User.js`
- Authorization middleware: `backend/middleware/auth.js`, `backend/middleware/adminAuth.js`, `backend/middleware/admin.js`
- Admin application boundaries: `admin/src/api/api.js`, `admin/src/components/ProtectedRoute.js`, `admin/src/pages/AdminDashboard.js`
- Admin API/domain: `backend/routes/adminRoutes.js`, `backend/models/Admin.js`

#### Realtime communications and location
Socket.io support in the backend enables live negotiation/chat and delivery updates. The customer UI has dedicated buyer/seller/order chat components and a delivery socket hook; map views consume location-oriented delivery state.
- Server realtime entry point: `backend/server.js`
- Conversation APIs and persistence: `backend/routes/chatRoutes.js`, `backend/models/CustomerChat.js`, `backend/models/Message.js`
- Client realtime/location surfaces: `frontend/src/hooks/useDeliverySocket.js`, `frontend/src/components/LiveMap.js`, `frontend/src/components/OrderChat.js`

#### AI crop analysis and uploads
Crop disease analysis is a distinct backend capability: image input is accepted from crop-analysis UI flows, handled by analysis routes/services, and integrates Google Gemini. Python model files indicate a parallel/local ML prediction path.
- Gemini and AI routes: `backend/routes/geminiRoutes.js`, `backend/routes/aiRoutes.js`
- Alternate analysis implementation: `backend/routes/cropAnalysisSimple.js`, `backend/crop-analysis-server.js`
- Python ML assets: `backend/ai/cropModel.py`, `backend/ai/predict.py`
- UI entry points: `frontend/src/pages/CropDoctor.js`, `frontend/src/pages/PlantAnalysis.js`

#### Payments, persistence, and external infrastructure
Express runs against MongoDB through its database configuration and Mongoose-style model layer. Razorpay order creation/verification is isolated behind payment routes/configuration. Deployments target Vercel for both React clients and Render/Heroku-compatible hosting for the backend; environment variables supply MongoDB, JWT, Gemini, Razorpay, and Google Maps credentials.
- Database connection: `backend/config/db.js`, `backend/models/index.js`
- Payment boundary: `backend/routes/paymentRoutes.js`, `backend/config/razorpay.js`
- Checkout UI: `frontend/src/pages/Payment.js`, `frontend/src/pages/payment-status-helper.js`

Main entry points: [`backend/server.js`](https://github.com/amit2003dh/kisan-setu/blob/main/backend/server.js) (Express and Socket.io server), [`backend/models/Order.js`](https://github.com/amit2003dh/kisan-setu/blob/main/backend/models/Order.js) (Marketplace models), [`frontend/src/App.js`](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/App.js) (Role-based React app), [`backend/routes/geminiRoutes.js`](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/geminiRoutes.js) (Gemini crop analysis)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_customer["Customer Application"]
  node_customer_app["Role-based React app<br/>React SPA<br/>[App.js]"]
  node_customer_guard["Protected routes<br/>auth guard<br/>[ProtectedRoute.js]"]
  node_customer_api["Marketplace API client<br/>[api.js]"]
  node_cart_state["Cart state<br/>React context<br/>[CartContext.js]"]
  node_realtime_map["Delivery socket and map<br/>realtime UI"]
  node_crop_doctor["Crop analysis UI<br/>analysis pages<br/>[CropDoctor.js]"]
end

subgraph group_admin["Admin Application"]
  node_admin_app["Admin dashboard<br/>React SPA<br/>[AdminDashboard.js]"]
  node_admin_api["Admin API client<br/>[api.js]"]
end

subgraph group_backend["Backend Platform"]
  node_api_server{{"Express and Socket.io server<br/>backend entrypoint<br/>[server.js]"}}
  node_authz["JWT authorization<br/>middleware<br/>[auth.js]"]
  node_identity_admin["Identity and admin routes<br/>route layer<br/>[userRoutes.js]"]
  node_catalog_orders["Catalog and order routes<br/>route layer<br/>[orderRoutes.js]"]
  node_delivery_tracking["Delivery and tracking routes<br/>route layer<br/>[deliveryRoutes.js]"]
  node_chat["Chat routes and messages<br/>realtime domain<br/>[chatRoutes.js]"]
  node_payments["Razorpay payment routes<br/>payment boundary<br/>[paymentRoutes.js]"]
  node_crop_analysis["Gemini crop analysis<br/>AI route layer<br/>[geminiRoutes.js]"]
  node_ml_server["Alternate ML analysis server<br/>Python analysis bridge"]
  node_marketplace_models[("Marketplace models<br/>Mongoose models<br/>[Order.js]")]
end

subgraph group_external["External Services"]
  node_mongodb[("MongoDB<br/>database")]
  node_razorpay["Razorpay<br/>payment provider"]
  node_gemini["Google Gemini<br/>AI provider"]
end

node_customer_app -->|"guards role routes"| node_customer_guard
node_customer_app -->|"uses"| node_customer_api
node_customer_app -->|"manages checkout cart"| node_cart_state
node_customer_app -->|"delivery views"| node_realtime_map
node_crop_doctor -->|"submits images"| node_customer_api
node_admin_app -->|"uses"| node_admin_api
node_customer_api -->|"HTTP API"| node_api_server
node_admin_api -->|"admin API"| node_api_server
node_realtime_map -->|"Socket.io updates"| node_api_server
node_api_server -->|"applies"| node_authz
node_api_server -->|"mounts"| node_identity_admin
node_api_server -->|"mounts"| node_catalog_orders
node_api_server -->|"mounts"| node_delivery_tracking
node_api_server -->|"mounts"| node_chat
node_api_server -->|"mounts"| node_payments
node_api_server -->|"mounts"| node_crop_analysis
node_identity_admin -->|"users and admins"| node_marketplace_models
node_catalog_orders -->|"catalog and orders"| node_marketplace_models
node_delivery_tracking -->|"delivery state"| node_marketplace_models
node_chat -->|"conversations"| node_marketplace_models
node_marketplace_models -->|"persists"| node_mongodb
node_payments -->|"creates and verifies"| node_razorpay
node_crop_analysis -->|"analyzes images"| node_gemini
node_crop_analysis -.->|"alternate path"| node_ml_server

click node_customer_app "https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/App.js"
click node_customer_guard "https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/components/ProtectedRoute.js"
click node_customer_api "https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/api/api.js"
click node_cart_state "https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/context/CartContext.js"
click node_realtime_map "https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/hooks/useDeliverySocket.js"
click node_crop_doctor "https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/pages/CropDoctor.js"
click node_admin_app "https://github.com/amit2003dh/kisan-setu/blob/main/admin/src/pages/AdminDashboard.js"
click node_admin_api "https://github.com/amit2003dh/kisan-setu/blob/main/admin/src/api/api.js"
click node_api_server "https://github.com/amit2003dh/kisan-setu/blob/main/backend/server.js"
click node_authz "https://github.com/amit2003dh/kisan-setu/blob/main/backend/middleware/auth.js"
click node_identity_admin "https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/userRoutes.js"
click node_catalog_orders "https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/orderRoutes.js"
click node_delivery_tracking "https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/deliveryRoutes.js"
click node_chat "https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/chatRoutes.js"
click node_payments "https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/paymentRoutes.js"
click node_crop_analysis "https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/geminiRoutes.js"
click node_ml_server "https://github.com/amit2003dh/kisan-setu/blob/main/backend/crop-analysis-server.js"
click node_marketplace_models "https://github.com/amit2003dh/kisan-setu/blob/main/backend/models/Order.js"

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_customer_app,node_customer_guard,node_customer_api,node_cart_state,node_realtime_map,node_crop_doctor toneBlue
class node_admin_app,node_admin_api toneAmber
class node_api_server,node_authz,node_identity_admin,node_catalog_orders,node_delivery_tracking,node_chat,node_payments,node_crop_analysis,node_ml_server,node_marketplace_models toneMint
class node_mongodb,node_razorpay,node_gemini toneRose
```

## Components

### Customer Application

- [Role-based React app](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/App.js): React SPA · Farmer, seller, buyer, and delivery workflows. · `frontend/src/App.js`
- [Protected routes](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/components/ProtectedRoute.js): auth guard · `frontend/src/components/ProtectedRoute.js`
- [Marketplace API client](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/api/api.js): API client · `frontend/src/api/api.js`
- [Cart state](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/context/CartContext.js): React context · `frontend/src/context/CartContext.js`
- [Delivery socket and map](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/hooks/useDeliverySocket.js): realtime UI · Shows live delivery location and order updates. · `frontend/src/hooks/useDeliverySocket.js`
- [Crop analysis UI](https://github.com/amit2003dh/kisan-setu/blob/main/frontend/src/pages/CropDoctor.js): analysis pages · `frontend/src/pages/CropDoctor.js`

### Admin Application

- [Admin dashboard](https://github.com/amit2003dh/kisan-setu/blob/main/admin/src/pages/AdminDashboard.js): React SPA · Independent administration deployment. · `admin/src/pages/AdminDashboard.js`
- [Admin API client](https://github.com/amit2003dh/kisan-setu/blob/main/admin/src/api/api.js): API client · `admin/src/api/api.js`

### Backend Platform

- [Express and Socket.io server](https://github.com/amit2003dh/kisan-setu/blob/main/backend/server.js): backend entrypoint · Hosts HTTP APIs and realtime connections. · `backend/server.js`
- [JWT authorization](https://github.com/amit2003dh/kisan-setu/blob/main/backend/middleware/auth.js): middleware · Protects role and administrative operations. · `backend/middleware/auth.js`
- [Identity and admin routes](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/userRoutes.js): route layer · Users, authentication, and platform oversight. · `backend/routes/userRoutes.js`
- [Catalog and order routes](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/orderRoutes.js): route layer · Crop, product, cart, and order lifecycle APIs. · `backend/routes/orderRoutes.js`
- [Delivery and tracking routes](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/deliveryRoutes.js): route layer · `backend/routes/deliveryRoutes.js`
- [Chat routes and messages](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/chatRoutes.js): realtime domain · Persists conversations and messages. · `backend/routes/chatRoutes.js`
- [Razorpay payment routes](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/paymentRoutes.js): payment boundary · Creates and verifies checkout payments. · `backend/routes/paymentRoutes.js`
- [Gemini crop analysis](https://github.com/amit2003dh/kisan-setu/blob/main/backend/routes/geminiRoutes.js): AI route layer · Accepts plant imagery for disease analysis. · `backend/routes/geminiRoutes.js`
- [Alternate ML analysis server](https://github.com/amit2003dh/kisan-setu/blob/main/backend/crop-analysis-server.js): Python analysis bridge · Supports the local prediction path. · `backend/crop-analysis-server.js`
- [Marketplace models](https://github.com/amit2003dh/kisan-setu/blob/main/backend/models/Order.js): Mongoose models · Crop, product, order, delivery, user, and chat state. · `backend/models/Order.js`

### External Services

- **MongoDB**: database
- **Razorpay**: payment provider
- **Google Gemini**: AI provider

## Connections

- Role-based React app → Protected routes: guards role routes
- Role-based React app → Marketplace API client: uses
- Role-based React app → Cart state: manages checkout cart
- Role-based React app → Delivery socket and map: delivery views
- Crop analysis UI → Marketplace API client: submits images
- Admin dashboard → Admin API client: uses
- Marketplace API client → Express and Socket.io server: HTTP API
- Admin API client → Express and Socket.io server: admin API
- Delivery socket and map → Express and Socket.io server: Socket.io updates
- Express and Socket.io server → JWT authorization: applies
- Express and Socket.io server → Identity and admin routes: mounts
- Express and Socket.io server → Catalog and order routes: mounts
- Express and Socket.io server → Delivery and tracking routes: mounts
- Express and Socket.io server → Chat routes and messages: mounts
- Express and Socket.io server → Razorpay payment routes: mounts
- Express and Socket.io server → Gemini crop analysis: mounts
- Identity and admin routes → Marketplace models: users and admins
- Catalog and order routes → Marketplace models: catalog and orders
- Delivery and tracking routes → Marketplace models: delivery state
- Chat routes and messages → Marketplace models: conversations
- Marketplace models → MongoDB: persists
- Razorpay payment routes → Razorpay: creates and verifies
- Gemini crop analysis → Google Gemini: analyzes images
- Gemini crop analysis → Alternate ML analysis server: alternate path
