# ceowsc/namma_sandhai architecture

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

- Interactive diagram: https://gitdiagram.com/ceowsc/namma_sandhai
- Repository: https://github.com/ceowsc/namma_sandhai

## Overview

#### Purpose and Deployable Surfaces
This is a multi-client agricultural marketplace and decision-support system: farmer, customer, merchant, district-market, and administrator workflows cover catalog/order operations, supply-demand visibility, communities, pricing, harvest planning, and communications.

Primary entry points:
- Node/Express API: `backend/server.js`
- Flutter cross-platform app: `frontend/lib/main.dart`
- React/Vite public marketplace portal: `landing_page/src/main.jsx`
- React/Vite operations console: `admin_panel/src/main.jsx`

#### Backend API and Persistence Boundary
The backend is the central integration boundary, with route modules exposing domain APIs and middleware enforcing authenticated access. Environment and database setup are separated from route/controller logic.

Key paths:
- `backend/server.js`
- `backend/config/database.js`
- `backend/middleware/auth.js`
- `database/schema.sql`

The relational schema, seed SQL, migration scripts, and backend maintenance scripts indicate an application-managed SQL database. API consumers include the Flutter app and both React applications.

#### Marketplace Transaction Domain
Product discovery, carts, wishlists, inventory, requirements, offers/LOIs, and order fulfillment are distinct API domains. Customer and business clients present role-specific flows over these shared backend resources.

Key paths:
- `backend/routes/products.js`
- `backend/routes/cart.js`
- `backend/routes/orders.js`
- `backend/routes/inventory.js`
- `backend/routes/loi.js`

Client flow generally progresses from catalog/search to cart or requirement/offer creation, then checkout/order management. Merchant and farmer interfaces add inventory, selling, wholesale, and LOI workflows.

#### Agricultural Intelligence Pipeline
The platform combines deterministic agricultural calculations with AI-assisted recommendations and external market/weather inputs. Harvest planning is a primary decision-support workflow, exposed through APIs and dedicated farmer screens.

Key paths:
- `backend/routes/harvestPlan.js`
- `backend/services/aiHarvestPlanner.js`
- `backend/services/harvestCalculator.js`
- `backend/services/aiPricePredictor.js`
- `backend/services/mandiService.js`
- `backend/services/weatherService.js`

Inputs such as crop, location, agronomy, market prices, and weather feed forecast/planning services. The mobile planner UI is centered at `frontend/lib/features/farmer/features/harvest_planner_screen.dart`; deterministic client-side preview widgets coexist with server-side planning logic.

#### Market, Demand, and Geographic Operations
Demand, mandi prices, supply-demand mapping, delivery routes, and district-level monitoring form a geographic market-intelligence subsystem. Tamil Nadu administrative and district geospatial data are packaged for map visualization.

Key paths:
- `backend/routes/demand.js`
- `backend/routes/mandiPrices.js`
- `backend/routes/map.js`
- `backend/routes/deliveryRoutes.js`
- `admin_panel/src/components/SupplyDemandMap.jsx`
- `admin_panel/public/tn_districts.geojson`

District officer screens in `frontend/lib/features/market/district_officer/` and admin analytics pages consume this operational layer for price verification, stock intelligence, and supply-demand oversight.

#### Identity, Roles, and Client State
Authentication is server-mediated, while each client organizes its own UI state and role routing. Flutter uses provider-oriented state modules; React portals use local stores/components and protected routes.

Key paths:
- `backend/routes/auth.js`
- `backend/services/authService.js`
- `frontend/lib/providers/auth_provider.dart`
- `landing_page/src/components/ProtectedRoute.jsx`
- `landing_page/src/store/useAuthStore.js`

Role-specific application shells separate customer, farmer, merchant, admin, and district workflows rather than treating the system as a single generic storefront.

#### Communications and External Integrations
Notification, email, WhatsApp, uploads, Firebase, Google indexing, maps, and Gemini-backed AI are explicit integration boundaries. Batch utilities suggest bulk outbound communications rather than only synchronous request handling.

Key paths:
- `backend/routes/notifications.js`
- `backend/routes/emailManager.js`
- `backend/routes/whatsapp.js`
- `backend/services/geminiService.js`
- `backend/utils/firebaseUtils.js`
- `backend/utils/emailBatcher.js`

The Flutter client also includes HTTP, socket, location, map, and notification service boundaries under `frontend/lib/services/`, keeping device/platform integrations outside screen components.

Main entry points: [`backend/server.js`](https://github.com/ceowsc/namma_sandhai/blob/main/backend/server.js) (Express API), [`backend/services/aiHarvestPlanner.js`](https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/aiHarvestPlanner.js) (AI harvest planner), [`backend/routes/demand.js`](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/demand.js) (Market operations APIs), [`backend/routes/harvestPlan.js`](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/harvestPlan.js) (Harvest planning API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client surfaces"]
  node_flutter["Flutter role-based app<br/>mobile client<br/>[main.dart]"]
  node_farmer_planner_ui["Harvest planner UI<br/>farmer screen"]
  node_marketplace_portal["Public marketplace portal<br/>React/Vite client<br/>[main.jsx]"]
  node_portal_auth["Portal auth state<br/>protected routing<br/>[ProtectedRoute.jsx]"]
  node_admin_console["Operations console<br/>React/Vite client<br/>[main.jsx]"]
end

subgraph group_api["Backend API"]
  node_api_server{{"Express API<br/>integration boundary<br/>[server.js]"}}
  node_auth["Authentication and roles<br/>API domain<br/>[auth.js]"]
  node_auth_middleware["Auth middleware<br/>access control<br/>[auth.js]"]
  node_sql_database[("Application SQL database<br/>relational persistence<br/>[schema.sql]")]
end

subgraph group_commerce["Marketplace"]
  node_commerce_api["Commerce APIs<br/>route domains<br/>[orders.js]"]
end

subgraph group_intelligence["Decision support"]
  node_harvest_api["Harvest planning API<br/>route domain<br/>[harvestPlan.js]"]
  node_harvest_planner["AI harvest planner<br/>planning service"]
  node_harvest_calculator["Harvest calculator<br/>deterministic service"]
  node_price_weather["Price and weather inputs<br/>external-data services<br/>[mandiService.js]"]
end

subgraph group_operations["Market operations"]
  node_supply_demand_map["Supply-demand map<br/>admin component"]
  node_market_ops_api["Market operations APIs<br/>route domains<br/>[demand.js]"]
  node_district_geojson["Tamil Nadu district geography<br/>geospatial data"]
end

subgraph group_integrations["Integrations"]
  node_communications["Communications API<br/>notification domain<br/>[notifications.js]"]
  node_external_services{{"External platform services<br/>integration boundary<br/>[geminiService.js]"}}
end

node_flutter -->|"API calls"| node_api_server
node_farmer_planner_ui -->|"planning requests"| node_harvest_api
node_marketplace_portal -->|"guards role flows"| node_portal_auth
node_marketplace_portal -->|"API calls"| node_api_server
node_admin_console -->|"API calls"| node_api_server
node_admin_console -->|"renders"| node_supply_demand_map
node_supply_demand_map -->|"district boundaries"| node_district_geojson
node_api_server -->|"mounts"| node_auth
node_api_server -->|"enforces access"| node_auth_middleware
node_api_server -->|"mounts"| node_commerce_api
node_api_server -->|"mounts"| node_harvest_api
node_api_server -->|"mounts"| node_market_ops_api
node_api_server -->|"mounts"| node_communications
node_auth -->|"identities and roles"| node_sql_database
node_commerce_api -->|"transactions"| node_sql_database
node_market_ops_api -->|"market and demand data"| node_sql_database
node_harvest_api -->|"generates plans"| node_harvest_planner
node_harvest_planner -->|"deterministic calculations"| node_harvest_calculator
node_harvest_planner -->|"market and weather inputs"| node_price_weather
node_harvest_planner -->|"AI recommendations"| node_external_services
node_market_ops_api -->|"mandi prices"| node_price_weather
node_communications -->|"outbound delivery"| node_external_services

click node_flutter "https://github.com/ceowsc/namma_sandhai/blob/main/frontend/lib/main.dart"
click node_farmer_planner_ui "https://github.com/ceowsc/namma_sandhai/blob/main/frontend/lib/features/farmer/features/harvest_planner_screen.dart"
click node_marketplace_portal "https://github.com/ceowsc/namma_sandhai/blob/main/landing_page/src/main.jsx"
click node_portal_auth "https://github.com/ceowsc/namma_sandhai/blob/main/landing_page/src/components/ProtectedRoute.jsx"
click node_admin_console "https://github.com/ceowsc/namma_sandhai/blob/main/admin_panel/src/main.jsx"
click node_supply_demand_map "https://github.com/ceowsc/namma_sandhai/blob/main/admin_panel/src/components/SupplyDemandMap.jsx"
click node_api_server "https://github.com/ceowsc/namma_sandhai/blob/main/backend/server.js"
click node_auth "https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/auth.js"
click node_auth_middleware "https://github.com/ceowsc/namma_sandhai/blob/main/backend/middleware/auth.js"
click node_sql_database "https://github.com/ceowsc/namma_sandhai/blob/main/database/schema.sql"
click node_commerce_api "https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/orders.js"
click node_harvest_api "https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/harvestPlan.js"
click node_harvest_planner "https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/aiHarvestPlanner.js"
click node_harvest_calculator "https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/harvestCalculator.js"
click node_price_weather "https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/mandiService.js"
click node_market_ops_api "https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/demand.js"
click node_district_geojson "https://github.com/ceowsc/namma_sandhai/blob/main/admin_panel/public/tn_districts.geojson"
click node_communications "https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/notifications.js"
click node_external_services "https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/geminiService.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_flutter,node_farmer_planner_ui,node_marketplace_portal,node_portal_auth,node_admin_console toneBlue
class node_api_server,node_auth,node_auth_middleware,node_sql_database toneAmber
class node_commerce_api toneMint
class node_harvest_api,node_harvest_planner,node_harvest_calculator,node_price_weather toneRose
class node_supply_demand_map,node_market_ops_api,node_district_geojson toneIndigo
class node_communications,node_external_services toneTeal
```

## Components

### Client surfaces

- [Flutter role-based app](https://github.com/ceowsc/namma_sandhai/blob/main/frontend/lib/main.dart): mobile client · Farmer, customer, merchant, and district workflows. · `frontend/lib/main.dart`
- [Harvest planner UI](https://github.com/ceowsc/namma_sandhai/blob/main/frontend/lib/features/farmer/features/harvest_planner_screen.dart): farmer screen · Farmer planning flow with local previews. · `frontend/lib/features/farmer/features/harvest_planner_screen.dart`
- [Public marketplace portal](https://github.com/ceowsc/namma_sandhai/blob/main/landing_page/src/main.jsx): React/Vite client · `landing_page/src/main.jsx`
- [Portal auth state](https://github.com/ceowsc/namma_sandhai/blob/main/landing_page/src/components/ProtectedRoute.jsx): protected routing · `landing_page/src/components/ProtectedRoute.jsx`
- [Operations console](https://github.com/ceowsc/namma_sandhai/blob/main/admin_panel/src/main.jsx): React/Vite client · `admin_panel/src/main.jsx`

### Backend API

- [Express API](https://github.com/ceowsc/namma_sandhai/blob/main/backend/server.js): integration boundary · Central API for all clients and domain routes. · `backend/server.js`
- [Authentication and roles](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/auth.js): API domain · Server-mediated authentication and access control. · `backend/routes/auth.js`
- [Auth middleware](https://github.com/ceowsc/namma_sandhai/blob/main/backend/middleware/auth.js): access control · `backend/middleware/auth.js`
- [Application SQL database](https://github.com/ceowsc/namma_sandhai/blob/main/database/schema.sql): relational persistence · Schema-managed transactional data store. · `database/schema.sql`

### Marketplace

- [Commerce APIs](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/orders.js): route domains · Catalog, carts, orders, inventory, and LOI workflows. · `backend/routes/orders.js`

### Decision support

- [Harvest planning API](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/harvestPlan.js): route domain · `backend/routes/harvestPlan.js`
- [AI harvest planner](https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/aiHarvestPlanner.js): planning service · Combines agronomy and forecast inputs into plans. · `backend/services/aiHarvestPlanner.js`
- [Harvest calculator](https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/harvestCalculator.js): deterministic service · `backend/services/harvestCalculator.js`
- [Price and weather inputs](https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/mandiService.js): external-data services · Mandi market and weather data inform recommendations. · `backend/services/mandiService.js`

### Market operations

- [Supply-demand map](https://github.com/ceowsc/namma_sandhai/blob/main/admin_panel/src/components/SupplyDemandMap.jsx): admin component · Renders district-level operational visibility. · `admin_panel/src/components/SupplyDemandMap.jsx`
- [Market operations APIs](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/demand.js): route domains · Demand, mandi pricing, maps, and delivery routing. · `backend/routes/demand.js`
- [Tamil Nadu district geography](https://github.com/ceowsc/namma_sandhai/blob/main/admin_panel/public/tn_districts.geojson): geospatial data · `admin_panel/public/tn_districts.geojson`

### Integrations

- [Communications API](https://github.com/ceowsc/namma_sandhai/blob/main/backend/routes/notifications.js): notification domain · Notification, email, and WhatsApp delivery endpoints. · `backend/routes/notifications.js`
- [External platform services](https://github.com/ceowsc/namma_sandhai/blob/main/backend/services/geminiService.js): integration boundary · AI, Firebase, email, WhatsApp, maps, and device services. · `backend/services/geminiService.js`

## Connections

- Flutter role-based app → Express API: API calls
- Harvest planner UI → Harvest planning API: planning requests
- Public marketplace portal → Portal auth state: guards role flows
- Public marketplace portal → Express API: API calls
- Operations console → Express API: API calls
- Operations console → Supply-demand map: renders
- Supply-demand map → Tamil Nadu district geography: district boundaries
- Express API → Authentication and roles: mounts
- Express API → Auth middleware: enforces access
- Express API → Commerce APIs: mounts
- Express API → Harvest planning API: mounts
- Express API → Market operations APIs: mounts
- Express API → Communications API: mounts
- Authentication and roles → Application SQL database: identities and roles
- Commerce APIs → Application SQL database: transactions
- Market operations APIs → Application SQL database: market and demand data
- Harvest planning API → AI harvest planner: generates plans
- AI harvest planner → Harvest calculator: deterministic calculations
- AI harvest planner → Price and weather inputs: market and weather inputs
- AI harvest planner → External platform services: AI recommendations
- Market operations APIs → Price and weather inputs: mandi prices
- Communications API → External platform services: outbound delivery
