# purvapatil11/kruhsi-mitr architecture

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

- Interactive diagram: https://gitdiagram.com/purvapatil11/kruhsi-mitr
- Repository: https://github.com/purvapatil11/kruhsi-mitr

## Overview

1. **Overall shape**
   - This is a **Next.js App Router** application that combines two product flows in one repo: a **Farmer mandi price tracker** and an **MSME credit scorer**.
   - The architecture is split between **UI routes under `app/`**, **server API routes under `app/api/`**, and a small **shared backend layer under `lib/` and `models/`**.

2. **Primary user-facing boundaries**
   - `app/page.js` is the landing page that routes users into the two main experiences.
   - `app/farmer/page.js` is the mandi-prices experience.
   - `app/msme/page.js` is the credit-scorer experience.
   - `app/about/page.js` and `app/auth/page.js` are additional supporting pages, likely informational and auth-related, but they are not the core product boundary.

3. **Farmer mandi price flow**
   - The farmer UI depends on `app/api/prices/route.js` to fetch mandi data.
   - That API route delegates to `lib/agmarknet.js`, which acts as the integration layer for **Agmarknet / data.gov.in** mandi price data.
   - The API returns current prices and related trend data to the frontend, which can then display the “best mandi” ranking and historical trend visualization described in the README.

4. **Price data integration**
   - `lib/agmarknet.js` is the external-data adapter for mandi information.
   - This is the key boundary between the app and the public data source, and it isolates the rest of the system from API-specific details, parsing, and normalization.
   - From an architecture perspective, this is the main read-only data ingestion path in the repo.

5. **Alerting flow**
   - `app/api/alerts/route.js` is the alert lifecycle endpoint.
   - The README indicates it supports **creating alerts** and **checking them later**, which implies a server-side stored alert record plus a background check mechanism.
   - When alert conditions are met, the route uses `lib/whatsapp.js` to send a WhatsApp notification.

6. **Persistence layer**
   - `lib/mongodb.js` is the database connection helper, so MongoDB is the persistence backend for alert data and any related records.
   - `models/index.js` contains the Mongoose schemas/models, likely for **alerts** and **business/MSME-related entities**.
   - This gives the architecture a clear separation: routes orchestrate, `lib/` connects to services, and `models/` defines stored domain data.

7. **MSME credit-scoring flow**
   - `app/msme/page.js` is the UI for the MSME product.
   - `app/api/score/route.js` is the computation endpoint for the credit scorer.
   - That route delegates to `lib/scorer.js`, which implements the **rule-based 5-signal scoring engine** described in the README.

8. **Scoring and scheme eligibility**
   - `lib/scorer.js` is a pure business-logic module, separate from persistence and external integrations.
   - The output is used to show a score, tips for improvement, and likely scheme eligibility guidance for programs such as **PM Mudra**, **CGTMSE**, **PSB Loans**, and **Stand-Up India**.
   - This subsystem is computation-heavy but does not appear to depend on external APIs; it is local business logic.

9. **WhatsApp messaging boundary**
   - `lib/whatsapp.js` encapsulates outbound messaging, likely via **Twilio WhatsApp** as noted in the README.
   - This keeps notification transport outside of the alerting route logic and makes messaging a separate infrastructure concern.
   - The alert system therefore has a clear chain: store alert in MongoDB, evaluate later, then send via WhatsApp.

10. **Scheduled/background execution**
   - The README references a **Vercel cron job** that checks alerts daily at 7am IST.
   - That means the alert system has both interactive API entry points and a scheduled server-side execution path.
   - This is important architecturally because alert delivery is not purely user-triggered; it is also time-driven.

11. **Request/response orchestration**
   - The `app/api/*/route.js` files are the main orchestration layer.
   - They accept frontend requests, call the shared service modules in `lib/`, and return data to the UI.
   - The app is therefore organized as a thin API layer over reusable domain modules rather than embedding business logic directly in pages.

12. **Front-end composition**
   - The `app/layout.js` and `app/globals.css` files define the shared application shell and styling.
   - `tailwind.config.js` and `postcss.config.js` indicate the UI is styled with **Tailwind CSS**.
   - The frontend is a standard Next.js server/client page structure, but the repo’s distinctive architecture is the split between the two product areas and the reusable backend services.

13. **Key external systems**
   - **Agmarknet/data.gov.in**: source of mandi prices.
   - **MongoDB**: persistence for alerts and related records.
   - **Twilio WhatsApp**: outbound alert delivery.
   - **Vercel Cron**: scheduled alert checks.
   - These external services are central to the product, so any architecture diagram should show them as first-class dependencies rather than incidental tooling.

14. **Most important architectural boundaries to diagram**
   - Separate the app into:
     - **User-facing pages**
     - **API routes**
     - **Shared domain logic in `lib/`**
     - **Persistence models in `models/`**
     - **External services**
   - The two product lines share infrastructure but not core business logic: mandi prices/alerts on one side, MSME scoring on the other.

15. **High-level data flow summary**
   - Farmer flow: user page → prices API → Agmarknet adapter → external mandi data → UI.
   - Alert flow: user page → alerts API → MongoDB → scheduled check → WhatsApp notification.
   - MSME flow: user page → score API → scoring engine → score/scheme guidance in UI.

Main entry points: [`app/page.js`](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/page.js) (Home), [`app/farmer/page.js`](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/farmer/page.js) (Farmer), [`app/api/alerts/route.js`](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/alerts/route.js) (Alerts API), [`app/layout.js`](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/layout.js) (Layout)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["User UI"]
  node_app_page["Home<br/>Next page<br/>[page.js]"]
  node_app_farmer_page["Farmer<br/>Next page<br/>[page.js]"]
  node_app_msme_page["MSME<br/>Next page<br/>[page.js]"]
  node_app_about_page["About<br/>Next page<br/>[page.js]"]
  node_app_auth_page["Auth<br/>Next page<br/>[page.js]"]
  node_app_layout["Layout<br/>App shell<br/>[layout.js]"]
end

subgraph group_api["API Routes"]
  node_api_prices["Prices API<br/>Route handler<br/>[route.js]"]
  node_api_alerts["Alerts API<br/>Route handler<br/>[route.js]"]
  node_api_score["Score API<br/>Route handler<br/>[route.js]"]
end

subgraph group_lib["Shared Logic"]
  node_lib_agmarknet["Agmarknet<br/>Data adapter<br/>[agmarknet.js]"]
  node_lib_scorer["Scorer<br/>Rule engine<br/>[scorer.js]"]
  node_lib_whatsapp["WhatsApp<br/>Notifier<br/>[whatsapp.js]"]
  node_lib_mongodb[("MongoDB<br/>DB connector<br/>[mongodb.js]")]
end

subgraph group_data["Persistence"]
  node_models_index["Models<br/>[index.js]"]
end

subgraph group_ext["External Systems"]
  node_ext_agmarknet(("Data.gov<br/>External data source"))
  node_ext_mongodb[("MongoDB<br/>External database")]
  node_ext_twilio(("Twilio<br/>Messaging provider"))
  node_ext_cron(("Cron<br/>Scheduler"))
end

node_app_page -->|"entry"| node_app_farmer_page
node_app_page -->|"entry"| node_app_msme_page
node_app_page -->|"support"| node_app_about_page
node_app_page -->|"support"| node_app_auth_page
node_app_farmer_page -->|"fetch"| node_api_prices
node_api_prices -->|"adapts"| node_lib_agmarknet
node_lib_agmarknet -.->|"reads"| node_ext_agmarknet
node_app_msme_page -->|"submit"| node_api_score
node_api_score -->|"computes"| node_lib_scorer
node_app_farmer_page -->|"manage"| node_api_alerts
node_api_alerts -->|"stores"| node_lib_mongodb
node_lib_mongodb -->|"uses"| node_models_index
node_models_index -.->|"writes"| node_ext_mongodb
node_api_alerts -->|"notifies"| node_lib_whatsapp
node_lib_whatsapp -.->|"sends"| node_ext_twilio
node_ext_cron -.->|"checks"| node_api_alerts
node_app_layout -->|"wraps"| node_app_page
node_app_layout -->|"wraps"| node_app_farmer_page
node_app_layout -->|"wraps"| node_app_msme_page

click node_app_page "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/page.js"
click node_app_farmer_page "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/farmer/page.js"
click node_app_msme_page "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/msme/page.js"
click node_app_about_page "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/about/page.js"
click node_app_auth_page "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/auth/page.js"
click node_app_layout "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/layout.js"
click node_api_prices "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/prices/route.js"
click node_api_alerts "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/alerts/route.js"
click node_api_score "https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/score/route.js"
click node_lib_agmarknet "https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/agmarknet.js"
click node_lib_scorer "https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/scorer.js"
click node_lib_whatsapp "https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/whatsapp.js"
click node_lib_mongodb "https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/mongodb.js"
click node_models_index "https://github.com/purvapatil11/kruhsi-mitr/blob/main/models/index.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_app_page,node_app_farmer_page,node_app_msme_page,node_app_about_page,node_app_auth_page,node_app_layout toneBlue
class node_api_prices,node_api_alerts,node_api_score toneAmber
class node_lib_agmarknet,node_lib_scorer,node_lib_whatsapp,node_lib_mongodb toneMint
class node_models_index toneRose
class node_ext_agmarknet,node_ext_mongodb,node_ext_twilio,node_ext_cron toneIndigo
```

## Components

### User UI

Next.js pages and shared shell

- [Home](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/page.js): Next page · Landing page that routes into the two product flows · `app/page.js`
- [Farmer](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/farmer/page.js): Next page · Mandi price tracker experience · `app/farmer/page.js`
- [MSME](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/msme/page.js): Next page · Credit scoring experience · `app/msme/page.js`
- [About](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/about/page.js): Next page · Supporting informational page · `app/about/page.js`
- [Auth](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/auth/page.js): Next page · Supporting authentication page · `app/auth/page.js`
- [Layout](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/layout.js): App shell · Shared application shell and global wrapper · `app/layout.js`

### API Routes

Server-side request handlers

- [Prices API](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/prices/route.js): Route handler · Serves mandi price data to the farmer flow · `app/api/prices/route.js`
- [Alerts API](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/alerts/route.js): Route handler · Creates and checks alert records · `app/api/alerts/route.js`
- [Score API](https://github.com/purvapatil11/kruhsi-mitr/blob/main/app/api/score/route.js): Route handler · Computes MSME credit scores and guidance · `app/api/score/route.js`

### Shared Logic

Adapters and business rules

- [Agmarknet](https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/agmarknet.js): Data adapter · Normalizes mandi price data from the public source · `lib/agmarknet.js`
- [Scorer](https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/scorer.js): Rule engine · Implements the MSME 5-signal scoring logic · `lib/scorer.js`
- [WhatsApp](https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/whatsapp.js): Notifier · Outbound alert delivery via messaging provider · `lib/whatsapp.js`
- [MongoDB](https://github.com/purvapatil11/kruhsi-mitr/blob/main/lib/mongodb.js): DB connector · Database connection helper for persisted alert data · `lib/mongodb.js`

### Persistence

Database models and connection

- [Models](https://github.com/purvapatil11/kruhsi-mitr/blob/main/models/index.js): Mongoose models · Stored domain schemas and collections · `models/index.js`

### External Systems

Third-party and scheduled dependencies

- **Data.gov**: External data source · Public mandi price data source
- **MongoDB**: External database · Persistent storage for alerts and related records
- **Twilio**: Messaging provider · WhatsApp delivery infrastructure
- **Cron**: Scheduler · Daily scheduled alert checks

## Connections

- Home → Farmer: entry · Routes users into the farmer experience
- Home → MSME: entry · Routes users into the MSME experience
- Home → About: support · Secondary informational navigation
- Home → Auth: support · Authentication entry point
- Farmer → Prices API: fetch · Requests current mandi prices and trends
- Prices API → Agmarknet: adapts · Delegates mandi data retrieval and normalization
- Agmarknet → Data.gov: reads · Pulls data from the public mandi source
- MSME → Score API: submit · Sends business inputs for scoring
- Score API → Scorer: computes · Runs the rule-based credit scoring engine
- Farmer → Alerts API: manage · Creates or checks price alerts
- Alerts API → MongoDB: stores · Persists alert records
- MongoDB → Models: uses · Shares Mongoose schema definitions
- Models → MongoDB: writes · Maps domain models onto MongoDB
- Alerts API → WhatsApp: notifies · Triggers WhatsApp delivery when alert conditions are met
- WhatsApp → Twilio: sends · Uses messaging infrastructure for WhatsApp alerts
- Cron → Alerts API: checks · Scheduled daily alert evaluation
- Layout → Home: wraps · Provides the shared app shell
- Layout → Farmer: wraps · Provides the shared app shell
- Layout → MSME: wraps · Provides the shared app shell
