# Agent Company Status - Quick Reference Guide

## Summary

Two new features have been added to manage company registration status for agents:

1. **Login Validation**: Agents from inactive companies cannot login
2. **Status Check API**: Agents can check their company's registration status anytime

---

## Feature 1: Login with Registration Validation

### Test Case 1: Login with Active Company

**Step 1: Generate OTP**
```bash
curl -X POST 'https://dayloanapp.xesstechlink.com/?generate_otp' \
  -H 'content-type: application/json' \
  -d '{"mobile_no": "9123456789"}'
```

**Response:**
```json
{
  "status": "success",
  "message": "OTP generated successfully",
  "otp": "123456",
  "expires_in": 300,
  "user_type": "agent"
}
```

**Step 2: Verify OTP & Login**
```bash
curl -X POST 'https://dayloanapp.xesstechlink.com/?verify_otp' \
  -H 'content-type: application/json' \
  -d '{
    "mobile_no": "9123456789",
    "otp": "123456",
    "device_id": "device_123"
  }'
```

**Response:**
```json
{
  "status": "success",
  "message": "Login successful",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "jwt_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user_type": "agent",
  "agent": {
    "id": 5,
    "name": "Agent Name",
    "mobile_no": "9123456789",
    "status": "active",
    "com_id": 1
  }
}
```

---

### Test Case 2: Login Attempt with Inactive Company (BLOCKED)

**Step 1: Generate OTP (FAILS)**
```bash
curl -X POST 'https://dayloanapp.xesstechlink.com/?generate_otp' \
  -H 'content-type: application/json' \
  -d '{"mobile_no": "9123456789"}'
```

**Response (403 Forbidden):**
```json
{
  "status": "error",
  "message": "Your company registration is not active. Please contact administrator.",
  "registration_status": "inactive"
}
```

❌ **Agent cannot proceed - No OTP sent**

---

## Feature 2: Check Company Status API

### Endpoint
```
GET /agent/company/status
```

### Check Company Status (After Login)

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

### Response (Company Active)

```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-15T10:30:00.000Z"
    },
    "registration": {
      "id": 1,
      "com_name": "ABC Finance Company",
      "email": "admin@abcfinance.com",
      "mob_num": "9876543210",
      "status": "active"
    },
    "agent": {
      "id": 5,
      "name": "Rajesh Kumar",
      "mobile_no": "9123456789",
      "company_id": 1
    },
    "is_active": true
  }
}
```

### Response (Company Inactive)

```json
{
  "status": "success",
  "message": "Company registration status retrieved successfully",
  "data": {
    "company": { ... },
    "registration": {
      "id": 1,
      "com_name": "ABC Finance Company",
      "status": "inactive"
    },
    "agent": { ... },
    "is_active": false
  }
}
```

**Key Field to Check:** `data.is_active` (boolean)

---

## Admin API Reference

### Check Own Company Registration

```bash
curl -X GET 'https://dayloanapp.xesstechlink.com/admin/company/registration' \
  -H 'authorization: Bearer ADMIN_JWT_TOKEN' \
  -H 'content-type: application/json'
```

### Response

```json
{
  "status": "success",
  "message": "Current company registration details retrieved successfully",
  "data": {
    "company": {
      "com_id": 1,
      "com_name": "ABC Finance Company",
      "email": "admin@abcfinance.com",
      "mob_num": "9876543210"
    },
    "registration": {
      "id": 1,
      "com_name": "ABC Finance Company",
      "status": "active"
    },
    "admin": {
      "id": 100,
      "name": "Admin Name"
    }
  }
}
```

---

### Update Registration Status (Admin Only)

```bash
curl -X PUT 'https://dayloanapp.xesstechlink.com/admin/registration/1/status' \
  -H 'authorization: Bearer ADMIN_JWT_TOKEN' \
  -H 'content-type: application/json' \
  -d '{
    "status": "active"
  }'
```

Or using POST:

```bash
curl -X POST 'https://dayloanapp.xesstechlink.com/admin/registration/update-status' \
  -H 'authorization: Bearer ADMIN_JWT_TOKEN' \
  -H 'content-type: application/json' \
  -d '{
    "registration_id": 1,
    "status": "active"
  }'
```

---

## Complete Workflow Example

### 1. Admin Deactivates Company
```bash
# Admin marks company as inactive
curl -X POST 'https://dayloanapp.xesstechlink.com/admin/registration/update-status' \
  -H 'authorization: Bearer ADMIN_JWT' \
  -H 'content-type: application/json' \
  -d '{"registration_id": 1, "status": "inactive"}'
```

### 2. Agent Tries to Login (FAILS)
```bash
# Agent attempts to generate OTP
curl -X POST 'https://dayloanapp.xesstechlink.com/?generate_otp' \
  -H 'content-type: application/json' \
  -d '{"mobile_no": "9123456789"}'

# Response: Error - Company registration not active
```

### 3. Already Logged-in Agent Checks Status
```bash
# Agent checks company status
curl -X GET 'https://dayloanapp.xesstechlink.com/agent/company/status' \
  -H 'authorization: Bearer AGENT_JWT' \
  -H 'content-type: application/json'

# Response: is_active = false
```

### 4. Admin Reactivates Company
```bash
# Admin marks company as active
curl -X POST 'https://dayloanapp.xesstechlink.com/admin/registration/update-status' \
  -H 'authorization: Bearer ADMIN_JWT' \
  -H 'content-type: application/json' \
  -d '{"registration_id": 1, "status": "active"}'
```

### 5. Agent Can Now Login
```bash
# Agent generates OTP (SUCCESS)
curl -X POST 'https://dayloanapp.xesstechlink.com/?generate_otp' \
  -H 'content-type: application/json' \
  -d '{"mobile_no": "9123456789"}'

# Agent verifies OTP and logs in (SUCCESS)
curl -X POST 'https://dayloanapp.xesstechlink.com/?verify_otp' \
  -H 'content-type: application/json' \
  -d '{"mobile_no": "9123456789", "otp": "123456", "device_id": "device_123"}'
```

---

## API Endpoints Summary

| Endpoint | Method | Auth | Purpose |
|----------|--------|------|---------|
| `/?generate_otp` | POST | None | Generate OTP (validates company status) |
| `/?verify_otp` | POST | None | Verify OTP & Login (validates company status) |
| `/agent/company/status` | GET | Agent | Check company registration status |
| `/admin/company/registration` | GET | Admin | Get own company registration |
| `/admin/registration/:id/status` | PUT | Admin | Update registration status |
| `/admin/registration/update-status` | POST | Admin | Update registration status |

---

## Error Codes

| Status Code | Meaning | When |
|-------------|---------|------|
| 200 | Success | Request successful |
| 400 | Bad Request | Invalid parameters |
| 401 | Unauthorized | Missing or invalid token |
| 403 | Forbidden | Company inactive or agent inactive |
| 404 | Not Found | Company/Registration not found |
| 500 | Server Error | Internal server error |

---

## Mobile App Integration

### Example: Check Status on App Launch

```javascript
async function checkCompanyStatusOnLaunch() {
  try {
    const response = await fetch('/agent/company/status', {
      headers: {
        'Authorization': `Bearer ${agentToken}`,
        'Content-Type': 'application/json'
      }
    });
    
    const result = await response.json();
    
    if (result.status === 'success') {
      if (result.data.is_active === false) {
        // Show warning to agent
        showAlert(
          'Company Inactive',
          'Your company registration is not active. Please contact administrator.',
          'warning'
        );
        // Optionally disable certain features
        disableTransactionFeatures();
      }
    }
  } catch (error) {
    console.error('Status check failed:', error);
  }
}
```

### Example: Periodic Status Check

```javascript
// Check every 30 minutes
setInterval(async () => {
  const result = await checkCompanyStatus();
  if (!result.data.is_active) {
    logoutAgent();
    showAlert('Company registration has been deactivated. Please contact administrator.');
  }
}, 30 * 60 * 1000);
```

---

## Testing Checklist

- [ ] Agent can login with active company registration
- [ ] Agent cannot generate OTP with inactive company registration
- [ ] Agent cannot verify OTP if company deactivated between OTP generation and verification
- [ ] Agent can check company status via API endpoint
- [ ] Admin can view company registration status
- [ ] Admin can activate/deactivate company registration
- [ ] Error messages are clear and actionable
- [ ] Mobile app handles errors gracefully

---

## Local Testing

For local testing, replace `https://dayloanapp.xesstechlink.com` with `http://localhost:3000`:

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

---

## Support

For issues or questions:
- Check the detailed documentation: [AGENT_LOGIN_REGISTRATION_CHECK.md](./AGENT_LOGIN_REGISTRATION_CHECK.md)
- Review the API documentation: [AGENT_COMPANY_STATUS_API.md](./AGENT_COMPANY_STATUS_API.md)

