# LeadSquared CRM Integration (Python Port)

This folder contains a Python conversion of the LeadSquared integration logic from:

- `/var/www/html/tatsat2dev/leadsquared/app/Services/LeadSquaredService.php`
- `/var/www/html/tatsat2dev/leadsquared/app/Http/Controllers/Api/LeadSquaredIntegrationController.php`
- `/var/www/html/tatsat2dev/leadsquared/database/migrations/...`

## Files

- `leadsquared_service.py`
  - Python service class for LeadSquared API
  - Supports:
    - `test_connection`
    - `search_leads`
    - `get_lead`
    - `create_lead`
    - `update_lead`
    - `delete_lead`

## API Behavior Ported

- Uses `accessKey` and `secretKey` as query params.
- Uses default API host: `https://api-in21.leadsquared.com/v2/`.
- Uses the same core LeadSquared endpoints from PHP:
  - `LeadManagement.svc/Leads.Get`
  - `LeadManagement.svc/Leads.GetById`
  - `LeadManagement.svc/Lead.Create`
  - `LeadManagement.svc/Lead.Update`
  - `LeadManagement.svc/Lead.Delete`

## Dashboard Backend Integration

The working integration is wired into the dashboard backend:

- Flask routes in `dashboard-backend/app.py`
- Encrypted credential storage per business in `dashboard-backend/db_handler.py`
- Service implementation in `dashboard-backend/leadsquared_service.py`

### Backend routes

- `GET /crm/<bid>/leadsquared/integration`
- `POST /crm/<bid>/leadsquared/integration`
- `POST /crm/<bid>/leadsquared/integration/test`
- `DELETE /crm/<bid>/leadsquared/integration`
- `POST /crm/<bid>/leadsquared/leads/search`
- `GET /crm/<bid>/leadsquared/leads/<lead_id>`
- `POST /crm/<bid>/leadsquared/leads`
- `PUT /crm/<bid>/leadsquared/leads/<lead_id>`
- `DELETE /crm/<bid>/leadsquared/leads/<lead_id>`

All routes are protected with auth and business access checks.
