# Admin View Agent Check-In/Check-Out API - cURL Examples

Quick reference for admins to view agent check-ins and check-outs using cURL.

## Base URL
```
http://192.168.1.3:3000
```

## Authentication
Replace `YOUR_ADMIN_JWT_TOKEN` with your actual admin JWT token.

---

## 1. Get Agent Check-In/Check-Out Records

View all agent check-ins and check-outs with pairing (check-in matched with check-out).

### Endpoint
```
GET /admin/agents/check-in-out
```

### Query Parameters
- `agent_id` (optional): Filter by specific agent ID
- `start_date` (optional): Start date filter (YYYY-MM-DD)
- `end_date` (optional): End date filter (YYYY-MM-DD)
- `status` (optional): Filter by `check_in` or `check_out`
- `limit` (optional): Maximum number of records (default: 1000)

### Basic Request
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Check-Ins/Outs for Specific Agent
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?agent_id=5" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Check-Ins/Outs with Date Range
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?start_date=2025-01-01&end_date=2025-01-31" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Only Check-Ins
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?status=check_in" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Only Check-Outs
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?status=check_out" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Today's Check-Ins/Outs
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?start_date=${TODAY}&end_date=${TODAY}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Success Response
```json
{
  "status": "success",
  "message": "Agent check-in/out records retrieved successfully",
  "data": {
    "filters": {
      "agent_id": null,
      "start_date": "2025-01-01",
      "end_date": "2025-01-31",
      "status": null,
      "limit": null
    },
    "summary": {
      "total_records": 150,
      "check_ins": 75,
      "check_outs": 75,
      "active_check_ins": 5,
      "total_agents": 10
    },
    "agents": [
      {
        "agent": {
          "id": 5,
          "name": "John Doe",
          "mobile_no": "9876543210",
          "line": "Line 1"
        },
        "records": [
          {
            "check_in": {
              "id": 123,
              "timestamp": "2025-01-15T09:00:00.000Z",
              "latitude": 11.3410,
              "longitude": 77.7172,
              "address": "Office, Erode",
              "remark": "Morning check-in"
            },
            "check_out": {
              "id": 124,
              "timestamp": "2025-01-15T18:00:00.000Z",
              "latitude": 11.3410,
              "longitude": 77.7172,
              "address": "Office, Erode",
              "remark": "End of day"
            },
            "duration": {
              "hours": 9,
              "minutes": 0,
              "total_minutes": 540,
              "total_seconds": 32400
            }
          },
          {
            "check_in": {
              "id": 125,
              "timestamp": "2025-01-16T09:00:00.000Z",
              "latitude": 11.3410,
              "longitude": 77.7172,
              "address": "Office, Erode",
              "remark": "Morning check-in"
            },
            "check_out": null,
            "duration": null,
            "status": "checked_in"
          }
        ]
      }
    ]
  }
}
```

---

## 2. Get Current Agent Check-In Status

View which agents are currently checked in (have an active check-in without a matching check-out).

### Endpoint
```
GET /admin/agents/check-in-status
```

### Query Parameters
- `agent_id` (optional): Filter by specific agent ID
- `line` (optional): Filter by agent line

### Basic Request
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Status for Specific Agent
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status?agent_id=5" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Get Status for Specific Line
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status?line=Line%201" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### Success Response
```json
{
  "status": "success",
  "message": "Current agent check-in status retrieved successfully",
  "data": {
    "filters": {
      "agent_id": null,
      "line": null
    },
    "summary": {
      "total_agents": 10,
      "checked_in": 3,
      "checked_out": 7
    },
    "agents": [
      {
        "agent": {
          "id": 5,
          "name": "John Doe",
          "mobile_no": "9876543210",
          "line": "Line 1"
        },
        "is_checked_in": true,
        "check_in": {
          "id": 123,
          "timestamp": "2025-01-15T09:00:00.000Z",
          "latitude": 11.3410,
          "longitude": 77.7172,
          "address": "Office, Erode",
          "remark": "Morning check-in"
        },
        "current_duration": {
          "hours": 5,
          "minutes": 30,
          "total_minutes": 330,
          "total_seconds": 19800
        },
        "last_check_out": {
          "id": 120,
          "timestamp": "2025-01-14T18:00:00.000Z",
          "latitude": 11.3410,
          "longitude": 77.7172,
          "address": "Office, Erode",
          "remark": "End of day"
        }
      },
      {
        "agent": {
          "id": 6,
          "name": "Jane Smith",
          "mobile_no": "9876543211",
          "line": "Line 2"
        },
        "is_checked_in": false,
        "check_in": null,
        "current_duration": null,
        "last_check_out": {
          "id": 121,
          "timestamp": "2025-01-15T17:00:00.000Z",
          "latitude": 11.3410,
          "longitude": 77.7172,
          "address": "Office, Erode",
          "remark": "End of day"
        }
      }
    ],
    "checked_in_agents": [
      {
        "agent": {
          "id": 5,
          "name": "John Doe",
          "mobile_no": "9876543210",
          "line": "Line 1"
        },
        "is_checked_in": true,
        "check_in": {
          "id": 123,
          "timestamp": "2025-01-15T09:00:00.000Z",
          "latitude": 11.3410,
          "longitude": 77.7172,
          "address": "Office, Erode",
          "remark": "Morning check-in"
        },
        "current_duration": {
          "hours": 5,
          "minutes": 30,
          "total_minutes": 330,
          "total_seconds": 19800
        }
      }
    ],
    "timestamp": "2025-01-15T14:30:00.000Z"
  }
}
```

---

## Complete Workflow Examples

### 1. Check Who's Currently Checked In
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 2. View Today's Check-Ins/Outs for All Agents
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?start_date=${TODAY}&end_date=${TODAY}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 3. View Specific Agent's Check-In/Out History
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-out?agent_id=5&start_date=2025-01-01&end_date=2025-01-31" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 4. View Only Currently Checked-In Agents
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool | grep -A 20 "checked_in_agents"
```

---

## Pretty Print JSON Response

```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

Or with `jq`:
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/check-in-status" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | jq
```

---

## Key Features

1. **Paired Records**: Check-ins are automatically paired with their corresponding check-outs
2. **Duration Calculation**: Automatic calculation of work duration for paired check-ins/outs
3. **Active Check-Ins**: Identifies agents who are currently checked in (no matching check-out)
4. **Current Duration**: Shows how long agents have been checked in
5. **Filtering**: Filter by agent, date range, status, and line
6. **Summary Statistics**: Total records, check-ins, check-outs, and active check-ins

---

## Response Structure

### Check-In/Out Records
- **Paired Records**: Check-in matched with check-out, includes duration
- **Unpaired Check-Ins**: Check-in without matching check-out (currently checked in)
- **Unpaired Check-Outs**: Check-out without matching check-in (rare, data inconsistency)

### Current Status
- **is_checked_in**: Boolean indicating if agent is currently checked in
- **check_in**: Latest check-in details (if checked in)
- **current_duration**: How long agent has been checked in
- **last_check_out**: Most recent check-out (if exists)

---

## Notes

1. Agent check-ins/outs are tracked through the location tracking system with `activity_type` = `check_in` or `check_out`
2. Records are automatically paired based on timestamp (check-out must be after check-in)
3. Agents can have multiple check-in/out pairs per day
4. Active check-ins are those without a matching check-out
5. Duration is calculated only for paired check-ins/outs
6. All timestamps are in UTC

---

## Error Handling

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

### Invalid Agent ID
```json
{
  "status": "error",
  "message": "Agent not found"
}
```

