# Admin Dashboard Graph Data API

## Endpoint

```
GET /api/admin/dashboard/graph-data
```

## Authentication

Requires a valid admin JWT token.

**Headers:**
```http
Authorization: Bearer <token>
Content-Type: application/json
```

## Query Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `from` | String | — | Start date (`YYYY-MM-DD`). Use with `to` for a custom range |
| `to` | String | — | End date (`YYYY-MM-DD`). Use with `from` for a custom range |
| `days` | Integer | `7` | Number of days (used only when `from`/`to` not provided) |

**Note:** When `from` and `to` are both provided, `days` is ignored. When neither is provided, the last `days` days (default 7) up to today are used.

## Response Format (200 OK)

```json
{
  "status": "success",
  "data": {
    "dailyTrends": [
      {
        "date": "2026-07-09",
        "collectedAmount": 12000.0,
        "disbursedAmount": 15000.0
      }
    ],
    "agentPerformance": [
      {
        "agentId": 101,
        "agentName": "Rajesh Kumar",
        "collectedAmount": 45000.0
      }
    ]
  }
}
```

### Fields

- **dailyTrends**: Array of daily collection vs. disbursement data (one entry per day in the range, filled with zeros for days with no activity)
  - `date` (string, `YYYY-MM-DD`): The calendar date
  - `collectedAmount` (float): Total `paid_amnt` from `loan_receipt` for that date
  - `disbursedAmount` (float): Total `loan_amt` from `loan_entry` for that date
- **agentPerformance**: Array of active agents sorted by collected amount (descending)
  - `agentId` (int): Agent ID from `collection_agent` table
  - `agentName` (string): Agent name
  - `collectedAmount` (float): Total collected by this agent across the date range

## Error Response

```json
{
  "status": "error",
  "message": "Admin authentication required"
}
```

## Source Files

- **Controller**: `controllers/AdminController.js` — method `getDashboardGraphData` (line ~490)
- **Route**: `routes/auth.js` — line ~302

## Implementation Details

- Daily trends iterate over every date in the range so the Flutter graph always has a complete, continuous timeline
- Agent performance uses a `LEFT JOIN` between `collection_agent` and `loan_receipt` (matched by agent name) so agents with zero collection still appear in the list
- Only active agents (`status IN ('active', 'Active')`) are included
- Agent results are ordered by `collectedAmount DESC` (highest performer first)
