# kranthikiran885366/kisan-mitra-ai-farming-advisor architecture

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

- Interactive diagram: https://gitdiagram.com/kranthikiran885366/kisan-mitra-ai-farming-advisor
- Repository: https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor

## Overview

#### Purpose and Runtime Topology
Kisan Mitra is a multilingual farming platform covering advisory, weather, market intelligence, government schemes, crop disease guidance, community interaction, consultations, and a crop marketplace. It has two application stacks: a Next.js App Router frontend/API surface and a separate Express/MongoDB backend. A legacy React client also exists and should be treated as a distinct/possibly transitional frontend.
- `app/layout.tsx`
- `app/page.tsx`
- `server/server.js`
- `client/src/index.js`

#### Primary User Experience Shell
The Next.js application is the main route-based UI. The root layout establishes shared application framing; dashboard and feature pages compose the farmer workflow. Shared navigation, provider wiring, notifications, and domain widgets support cross-page interaction.
- `app/dashboard/page.tsx`
- `components/providers/Providers.tsx`
- `components/enhanced-sidebar.tsx`
- `components/BottomNavigation.tsx`

#### Identity, Onboarding, and Access Boundary
Authentication includes signup and OTP issuance/verification through Next route handlers, while the Express backend separately exposes auth routes and JWT middleware. Frontend auth state is held in a React context; protected backend requests should cross the JWT boundary rather than directly trusting client state.
- `app/api/auth/signup/route.ts`
- `app/api/auth/send-otp/route.ts`
- `app/api/auth/verify-otp/route.ts`
- `lib/contexts/AuthContext.tsx`
- `server/middleware/auth.js`
- `server/routes/auth.js`

#### Advisory and AI Domain
Crop advice, disease expertise/scanning, and conversational assistance are major decision-support stages. Page-level UI calls typed client API modules; Express routes orchestrate domain logic, persistence, and AI integration. AI conversations and knowledge-base content have dedicated models, indicating the assistant is intended to retain or retrieve agricultural context.
- `app/ai-assistant/page.tsx`
- `app/crop-advisor/page.tsx`
- `app/disease-expert/page.tsx`
- `lib/assistantApi.ts`
- `server/routes/assistant.js`
- `server/services/aiService.js`
- `server/models/AIConversation.js`

#### Agricultural Information Services
Weather, mandi/market data, and scheme discovery are independently routed service domains. Their service layer is the external-integration boundary: configured weather and market credentials suggest OpenWeather and Agmarknet-style providers, while scheme data can be curated or sourced externally. The UI consumes these through feature API clients.
- `app/weather/page.tsx`
- `app/market/page.tsx`
- `app/schemes/page.tsx`
- `lib/marketApi.ts`
- `lib/schemesApi.ts`
- `server/services/weatherService.js`
- `server/services/marketService.js`
- `server/services/schemeService.js`

#### Community, Consultations, and Messaging
Farmer engagement is modeled as server-managed social and expert-interaction data: communities/posts, discussions, consultations/appointments, messages, ratings, and WhatsApp integration. This separates collaboration and support workflows from the informational advisory modules.
- `app/community/page.tsx`
- `app/consultations/[id]/page.tsx`
- `app/messages/page.tsx`
- `server/routes/community.js`
- `server/routes/consultations.js`
- `server/routes/whatsapp.js`
- `server/models/CommunityPost.js`
- `server/models/Appointment.js`

#### Marketplace Transaction Flow
The marketplace supports product discovery, farmer crop listings, carts, checkout, orders, and negotiation. The frontend route hierarchy represents browse-to-purchase/sell flows; the Express marketplace route owns transaction-facing operations and MongoDB models store the durable commercial entities.
- `app/marketplace/page.tsx`
- `app/marketplace/product/[id]/page.tsx`
- `app/marketplace/cart/page.tsx`
- `app/marketplace/checkout/page.tsx`
- `server/routes/marketplace.js`
- `server/models/Cart.js`
- `server/models/Order.js`
- `server/models/CropListing.js`

#### Persistence, Localization, and Operational Dependencies
MongoDB is the central persistence layer, accessed from Next via a Mongo helper and from Express through Mongoose-style models. Data can be initialized with server seed scripts. The current Next UI owns TypeScript localization context, while the legacy client has a separate i18n catalog; avoid conflating their state providers. Browser Web Speech capabilities support voice UX, and environment configuration supplies MongoDB/JWT plus optional external API keys.
- `lib/mongodb.ts`
- `server/models/User.js`
- `server/scripts/seedAll.js`
- `lib/contexts/LanguageContext.tsx`
- `client/src/i18n/config.js`
- `.env.example`

Main entry points: [`app/dashboard/page.tsx`](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/dashboard/page.tsx) (Farmer dashboard), [`server/middleware/auth.js`](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/middleware/auth.js) (JWT auth boundary), [`server/routes/assistant.js`](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/assistant.js) (Assistant domain), [`lib/mongodb.ts`](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/mongodb.ts) (MongoDB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_next["Next.js application"]
  node_next_shell["App shell<br/>Next.js UI<br/>[layout.tsx]"]
  node_dashboard["Farmer dashboard<br/>Next.js page<br/>[page.tsx]"]
  node_auth_handlers["OTP signup API<br/>Next route handlers<br/>[route.ts]"]
  node_auth_state["Auth state<br/>React context<br/>[AuthContext.tsx]"]
  node_advisory_ui["AI and crop guidance<br/>Next.js pages<br/>[page.tsx]"]
  node_assistant_client["Assistant API client<br/>Typed client API<br/>[assistantApi.ts]"]
  node_info_ui["Weather, market, schemes<br/>Next.js pages<br/>[page.tsx]"]
  node_market_client["Market API client<br/>Typed client API<br/>[marketApi.ts]"]
  node_engagement_ui["Community and support<br/>Next.js pages<br/>[page.tsx]"]
  node_commerce_ui["Marketplace flow<br/>Next.js pages<br/>[page.tsx]"]
  node_language_context["Language context<br/>React context"]
end

subgraph group_express["Express backend"]
  node_express_server["Express server<br/>API runtime<br/>[server.js]"]
  node_jwt_boundary["JWT auth boundary<br/>Express middleware<br/>[auth.js]"]
  node_assistant_domain["Assistant domain<br/>Express route and service<br/>[assistant.js]"]
  node_info_services["Information services<br/>External integration services<br/>[weatherService.js]"]
  node_engagement_domain["Engagement domain<br/>Express routes<br/>[community.js]"]
  node_marketplace_domain["Marketplace domain<br/>Express route<br/>[marketplace.js]"]
end

subgraph group_data["Data and integrations"]
  node_mongodb[("MongoDB<br/>Database<br/>[mongodb.ts]")]
  node_external_providers{{"Agricultural providers<br/>External APIs<br/>[.env.example]"}}
end

subgraph group_legacy["Legacy client"]
  node_legacy_client["Legacy React client<br/>React frontend<br/>[index.js]"]
  node_legacy_i18n["Legacy i18n catalog<br/>Localization config<br/>[config.js]"]
end

node_next_shell -->|"frames"| node_dashboard
node_next_shell -->|"provides"| node_auth_state
node_next_shell -->|"provides"| node_language_context
node_dashboard -->|"links to"| node_advisory_ui
node_dashboard -->|"links to"| node_info_ui
node_dashboard -->|"links to"| node_commerce_ui
node_auth_state -->|"signup and OTP"| node_auth_handlers
node_auth_handlers -->|"persists identity"| node_mongodb
node_advisory_ui -->|"calls"| node_assistant_client
node_assistant_client -->|"API requests"| node_assistant_domain
node_info_ui -->|"uses feature APIs"| node_market_client
node_market_client -->|"data requests"| node_info_services
node_engagement_ui -->|"API requests"| node_engagement_domain
node_commerce_ui -->|"browse and checkout"| node_marketplace_domain
node_express_server -->|"applies"| node_jwt_boundary
node_jwt_boundary -->|"authorizes"| node_assistant_domain
node_jwt_boundary -->|"authorizes"| node_engagement_domain
node_jwt_boundary -->|"authorizes"| node_marketplace_domain
node_assistant_domain -->|"conversations and knowledge"| node_mongodb
node_engagement_domain -->|"posts and appointments"| node_mongodb
node_marketplace_domain -->|"listings, carts, orders"| node_mongodb
node_info_services -->|"fetches data"| node_external_providers
node_assistant_domain -->|"AI integration"| node_external_providers
node_legacy_client -->|"uses"| node_legacy_i18n

click node_next_shell "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/layout.tsx"
click node_dashboard "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/dashboard/page.tsx"
click node_auth_handlers "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/api/auth/signup/route.ts"
click node_auth_state "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/contexts/AuthContext.tsx"
click node_advisory_ui "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/ai-assistant/page.tsx"
click node_assistant_client "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/assistantApi.ts"
click node_info_ui "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/weather/page.tsx"
click node_market_client "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/marketApi.ts"
click node_engagement_ui "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/community/page.tsx"
click node_commerce_ui "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/marketplace/page.tsx"
click node_language_context "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/contexts/LanguageContext.tsx"
click node_express_server "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/server.js"
click node_jwt_boundary "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/middleware/auth.js"
click node_assistant_domain "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/assistant.js"
click node_info_services "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/services/weatherService.js"
click node_engagement_domain "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/community.js"
click node_marketplace_domain "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/marketplace.js"
click node_mongodb "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/mongodb.ts"
click node_external_providers "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/.env.example"
click node_legacy_client "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/client/src/index.js"
click node_legacy_i18n "https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/client/src/i18n/config.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_next_shell,node_dashboard,node_auth_handlers,node_auth_state,node_advisory_ui,node_assistant_client,node_info_ui,node_market_client,node_engagement_ui,node_commerce_ui,node_language_context toneBlue
class node_express_server,node_jwt_boundary,node_assistant_domain,node_info_services,node_engagement_domain,node_marketplace_domain toneAmber
class node_mongodb,node_external_providers toneMint
class node_legacy_client,node_legacy_i18n toneRose
```

## Components

### Next.js application

Primary App Router UI and API surface.

- [App shell](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/layout.tsx): Next.js UI · Shared layout, providers, and navigation frame. · `app/layout.tsx`
- [Farmer dashboard](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/dashboard/page.tsx): Next.js page · `app/dashboard/page.tsx`
- [OTP signup API](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/api/auth/signup/route.ts): Next route handlers · Signup and OTP issuance/verification endpoints. · `app/api/auth/signup/route.ts`
- [Auth state](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/contexts/AuthContext.tsx): React context · `lib/contexts/AuthContext.tsx`
- [AI and crop guidance](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/ai-assistant/page.tsx): Next.js pages · Assistant, crop-advisor, and disease-expert experiences. · `app/ai-assistant/page.tsx`
- [Assistant API client](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/assistantApi.ts): Typed client API · `lib/assistantApi.ts`
- [Weather, market, schemes](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/weather/page.tsx): Next.js pages · Agricultural information feature pages. · `app/weather/page.tsx`
- [Market API client](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/marketApi.ts): Typed client API · `lib/marketApi.ts`
- [Community and support](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/community/page.tsx): Next.js pages · Community, consultations, and messaging UI. · `app/community/page.tsx`
- [Marketplace flow](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/app/marketplace/page.tsx): Next.js pages · Browse, product, cart, and checkout flow. · `app/marketplace/page.tsx`
- [Language context](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/contexts/LanguageContext.tsx): React context · Current Next UI localization state. · `lib/contexts/LanguageContext.tsx`

### Express backend

Separate domain API and orchestration runtime.

- [Express server](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/server.js): API runtime · Hosts authenticated domain routes. · `server/server.js`
- [JWT auth boundary](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/middleware/auth.js): Express middleware · Protects backend requests independently of client state. · `server/middleware/auth.js`
- [Assistant domain](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/assistant.js): Express route and service · Orchestrates conversational agricultural AI. · `server/routes/assistant.js`
- [Information services](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/services/weatherService.js): External integration services · Weather, market, and scheme retrieval boundary. · `server/services/weatherService.js`
- [Engagement domain](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/community.js): Express routes · Community, consultations, and WhatsApp workflows. · `server/routes/community.js`
- [Marketplace domain](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/server/routes/marketplace.js): Express route · Transaction-facing listing, cart, order, and negotiation operations. · `server/routes/marketplace.js`

### Data and integrations

- [MongoDB](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/lib/mongodb.ts): Database · Central durable data store. · `lib/mongodb.ts`
- [Agricultural providers](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/.env.example): External APIs · Weather, mandi data, schemes, and optional AI providers. · `.env.example`

### Legacy client

Transitional React frontend with separate localization.

- [Legacy React client](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/client/src/index.js): React frontend · Separate client runtime and localization catalog. · `client/src/index.js`
- [Legacy i18n catalog](https://github.com/kranthikiran885366/kisan-mitra-ai-farming-advisor/blob/feature/seed-scripts/client/src/i18n/config.js): Localization config · `client/src/i18n/config.js`

## Connections

- App shell → Farmer dashboard: frames
- App shell → Auth state: provides
- App shell → Language context: provides
- Farmer dashboard → AI and crop guidance: links to
- Farmer dashboard → Weather, market, schemes: links to
- Farmer dashboard → Marketplace flow: links to
- Auth state → OTP signup API: signup and OTP
- OTP signup API → MongoDB: persists identity
- AI and crop guidance → Assistant API client: calls
- Assistant API client → Assistant domain: API requests
- Weather, market, schemes → Market API client: uses feature APIs
- Market API client → Information services: data requests
- Community and support → Engagement domain: API requests
- Marketplace flow → Marketplace domain: browse and checkout
- Express server → JWT auth boundary: applies
- JWT auth boundary → Assistant domain: authorizes
- JWT auth boundary → Engagement domain: authorizes
- JWT auth boundary → Marketplace domain: authorizes
- Assistant domain → MongoDB: conversations and knowledge
- Engagement domain → MongoDB: posts and appointments
- Marketplace domain → MongoDB: listings, carts, orders
- Information services → Agricultural providers: fetches data
- Assistant domain → Agricultural providers: AI integration
- Legacy React client → Legacy i18n catalog: uses
