# Agent Company Status API

This API endpoint allows collection agents to check if their company's registration status is active or not.

## Endpoint

```
GET /agent/company/status
```

## Authentication

Requires agent authentication via Bearer token in the Authorization header.

## Request Headers

```
authorization: Bearer YOUR_AGENT_JWT_TOKEN
content-type: application/json
```

## Request Parameters

No parameters required. The API automatically retrieves the company information based on the authenticated agent's `com_id`.

## Response Format

### Success Response (200 OK)

```json
{
  "status": "success",
  "message": "Company registration status retrieved successfully",
  "data": {
    "company": {
      "com_id": 1,
      "com_name": "ABC Finance Company",
      "email": "admin@abcfinance.com",
      "mob_num": "9876543210",
      "city": "Mumbai",
      "district": "Mumbai",
      "user_name": "admin",
      "created_at": "2024-01-01T00:00:00.000Z"
    },
    "registration": {
      "id": 1,
      "com_name": "ABC Finance Company",
      "email": "admin@abcfinance.com",
      "mob_num": "9876543210",
      "status": "active"
    },
    "agent": {
      "id": 5,
      "name": "John Doe",
      "mobile_no": "9123456789",
      "company_id": 1
    },
    "is_active": true
  }
}
```

### Success Response with Inactive Status (200 OK)

```json
{
  "status": "success",
  "message": "Company registration status retrieved successfully",
  "data": {
    "company": { ... },
    "registration": {
      "id": 1,
      "com_name": "ABC Finance Company",
      "email": "admin@abcfinance.com",
      "mob_num": "9876543210",
      "status": "inactive"
    },
    "agent": { ... },
    "is_active": false
  }
}
```

### Error Responses

#### Authentication Error (401 Unauthorized)

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

#### Company Not Found (404 Not Found)

```json
{
  "status": "error",
  "message": "Company not found"
}
```

#### Missing Registration ID (404 Not Found)

```json
{
  "status": "error",
  "message": "Company does not have a registration ID (reg_id)",
  "data": {
    "company": {
      "com_id": 1,
      "com_name": "ABC Finance Company",
      "email": "admin@abcfinance.com",
      "mob_num": "9876543210"
    }
  }
}
```

#### Registration Not Found (404 Not Found)

```json
{
  "status": "error",
  "message": "Registration not found for this company",
  "data": {
    "company": {
      "com_id": 1,
      "com_name": "ABC Finance Company",
      "reg_id": 1
    }
  }
}
```

#### Invalid Registration ID Format (400 Bad Request)

```json
{
  "status": "error",
  "message": "Invalid registration ID format in company record"
}
```

#### Server Error (500 Internal Server Error)

```json
{
  "status": "error",
  "message": "Failed to fetch company status",
  "error": "Error details..."
}
```

## cURL Examples

### Basic Request (Local Development)

```bash
curl -X GET 'http://localhost:3000/agent/company/status' \
  -H 'authorization: Bearer YOUR_AGENT_JWT_TOKEN' \
  -H 'content-type: application/json'
```

### Production Request

```bash
curl -X GET 'https://dayloanapp.xesstechlink.com/agent/company/status' \
  -H 'authorization: Bearer YOUR_AGENT_JWT_TOKEN' \
  -H 'content-type: application/json'
```

### Example with Actual Token

```bash
curl -X GET 'https://dayloanapp.xesstechlink.com/agent/company/status' \
  -H 'authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVpZCI6NSwidXNlcl90eXBlIjoiYWdlbnQifSwiaWF0IjoxNzM1MjAwMDAwfQ.example_signature' \
  -H 'content-type: application/json'
```

### Response Handling in JavaScript

```javascript
async function checkCompanyStatus() {
  try {
    const response = await fetch('https://dayloanapp.xesstechlink.com/agent/company/status', {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${agentToken}`,
        'Content-Type': 'application/json'
      }
    });

    const result = await response.json();
    
    if (result.status === 'success') {
      if (result.data.is_active) {
        console.log('✓ Company is active');
        console.log('Company Name:', result.data.company.com_name);
        console.log('Registration Status:', result.data.registration.status);
      } else {
        console.log('✗ Company is inactive');
        console.log('Please contact admin');
      }
    } else {
      console.error('Error:', result.message);
    }
  } catch (error) {
    console.error('Request failed:', error);
  }
}
```

## Use Cases

### 1. App Startup Check
When the agent opens the mobile app, check if the company is still active before allowing access to features.

### 2. Periodic Status Verification
Periodically check company status during the day to ensure the agent's company hasn't been deactivated.

### 3. Transaction Validation
Before processing important transactions (like receipts), verify that the company status is still active.

### 4. Admin Notification
If the company becomes inactive, show appropriate message to the agent to contact admin.

## Notes

- The `is_active` field in the response is a boolean that indicates whether the company status is "active" (case-insensitive comparison)
- The endpoint returns all relevant information about the company, registration, and agent for comprehensive status checking
- Agents can only check their own company's status (based on their `com_id`)
- The registration status is controlled by admins via the admin endpoints

## Related Endpoints

- **Admin Endpoint**: `GET /admin/company/registration` - Admins can view their company's registration status
- **Admin Update Endpoint**: `PUT /admin/registration/:registration_id/status` - Admins can update registration status

## Security

- Agent must be authenticated with a valid JWT token
- Only the agent's own company information is returned (no access to other companies)
- Inactive agents are automatically blocked by the authentication middleware before reaching this endpoint

