122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from typing import Dict, Any, Optional, List
|
|
import asyncio
|
|
|
|
from adapters.amocrm_client import AmoCRMClient
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/fetch/{entity_type}")
|
|
async def fetch_amocrm_entity(
|
|
entity_type: str,
|
|
limit: int = 10,
|
|
page: int = 1
|
|
) -> Dict[str, Any]:
|
|
"""Fetch data directly from AMO CRM API"""
|
|
|
|
try:
|
|
client = AmoCRMClient()
|
|
|
|
if entity_type == "users":
|
|
return await client.get_users(limit=limit)
|
|
elif entity_type == "pipelines":
|
|
return await client.get_pipelines()
|
|
elif entity_type == "companies":
|
|
return await client.get_companies(limit=limit, page=page)
|
|
elif entity_type == "contacts":
|
|
return await client.get_contacts(limit=limit, page=page)
|
|
elif entity_type == "deals":
|
|
return await client.get_deals(limit=limit, page=page)
|
|
elif entity_type == "events":
|
|
return await client.get_events(limit=limit, page=page)
|
|
else:
|
|
raise HTTPException(status_code=404, detail=f"Entity type '{entity_type}' not supported")
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Error fetching from AMO CRM: {str(e)}")
|
|
|
|
|
|
@router.get("/fetch/custom_fields/{entity_type}")
|
|
async def fetch_custom_fields(entity_type: str) -> Dict[str, Any]:
|
|
"""Fetch custom fields metadata from AMO CRM"""
|
|
|
|
valid_types = ["leads", "contacts", "companies"]
|
|
if entity_type not in valid_types:
|
|
raise HTTPException(status_code=400, detail=f"Entity type must be one of: {valid_types}")
|
|
|
|
try:
|
|
client = AmoCRMClient()
|
|
return await client.get_custom_fields(entity_type)
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Error fetching custom fields: {str(e)}")
|
|
|
|
|
|
@router.get("/fetch/all")
|
|
async def fetch_all_amocrm_data() -> Dict[str, Any]:
|
|
"""Fetch all data from AMO CRM (limited amounts for testing)"""
|
|
|
|
try:
|
|
client = AmoCRMClient()
|
|
return await client.fetch_all_data()
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Error fetching all data: {str(e)}")
|
|
|
|
|
|
@router.post("/sync/{entity_type}")
|
|
async def sync_entity_from_amocrm(
|
|
entity_type: str,
|
|
limit: int = 250,
|
|
page: int = 1
|
|
) -> Dict[str, Any]:
|
|
"""Fetch data from AMO CRM and store it in database"""
|
|
|
|
# This would integrate with the data ingestion endpoints
|
|
# For now, just fetch the data
|
|
try:
|
|
client = AmoCRMClient()
|
|
|
|
if entity_type == "users":
|
|
data = await client.get_users(limit=limit)
|
|
elif entity_type == "pipelines":
|
|
data = await client.get_pipelines()
|
|
elif entity_type == "companies":
|
|
data = await client.get_companies(limit=limit, page=page)
|
|
elif entity_type == "contacts":
|
|
data = await client.get_contacts(limit=limit, page=page)
|
|
elif entity_type == "deals":
|
|
data = await client.get_deals(limit=limit, page=page)
|
|
elif entity_type == "events":
|
|
data = await client.get_events(limit=limit, page=page)
|
|
else:
|
|
raise HTTPException(status_code=404, detail=f"Entity type '{entity_type}' not supported")
|
|
|
|
# TODO: Integrate with data ingestion endpoints
|
|
# For now, return the fetched data
|
|
return {
|
|
"message": f"Fetched {entity_type} from AMO CRM",
|
|
"data": data,
|
|
"sync_status": "fetched_only" # Would be "synced" when integrated
|
|
}
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Error syncing from AMO CRM: {str(e)}")
|
|
|
|
|
|
@router.get("/info")
|
|
async def amocrm_info() -> Dict[str, Any]:
|
|
"""Get AMO CRM connection info"""
|
|
from utils.config import settings
|
|
|
|
return {
|
|
"domain": settings.AMO_CRM_DOMAIN,
|
|
"has_token": bool(settings.AMO_CRM_ACCESS_TOKEN),
|
|
"base_url": f"https://{settings.AMO_CRM_DOMAIN}/api/v4",
|
|
"supported_entities": [
|
|
"users", "pipelines", "companies",
|
|
"contacts", "deals", "events"
|
|
]
|
|
}
|