Add: Debug endpoint for contact-company links
This commit is contained in:
parent
5bb6e73a26
commit
bd2de09076
@ -12,6 +12,56 @@ logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/contacts/debug", response_model=dict)
|
||||
async def debug_contacts():
|
||||
"""
|
||||
Debug endpoint: Check contact-company links
|
||||
"""
|
||||
try:
|
||||
# Count links
|
||||
links = execute_query("SELECT COUNT(*) as total FROM contact_companies")
|
||||
|
||||
# Get sample with links
|
||||
sample = execute_query("""
|
||||
SELECT
|
||||
c.id, c.first_name, c.last_name,
|
||||
COUNT(cc.customer_id) as company_count,
|
||||
ARRAY_AGG(cu.name) as company_names
|
||||
FROM contacts c
|
||||
LEFT JOIN contact_companies cc ON c.id = cc.contact_id
|
||||
LEFT JOIN customers cu ON cc.customer_id = cu.id
|
||||
GROUP BY c.id, c.first_name, c.last_name
|
||||
HAVING COUNT(cc.customer_id) > 0
|
||||
LIMIT 10
|
||||
""")
|
||||
|
||||
# Test the actual query used in get_contacts
|
||||
test_query = """
|
||||
SELECT
|
||||
c.id, c.first_name, c.last_name,
|
||||
COUNT(DISTINCT cc.customer_id) as company_count,
|
||||
ARRAY_AGG(DISTINCT cu.name ORDER BY cu.name) FILTER (WHERE cu.name IS NOT NULL) as company_names
|
||||
FROM contacts c
|
||||
LEFT JOIN contact_companies cc ON c.id = cc.contact_id
|
||||
LEFT JOIN customers cu ON cc.customer_id = cu.id
|
||||
GROUP BY c.id, c.first_name, c.last_name
|
||||
ORDER BY c.last_name, c.first_name
|
||||
LIMIT 10
|
||||
"""
|
||||
test_result = execute_query(test_query)
|
||||
|
||||
return {
|
||||
"total_links": links[0]['total'] if links else 0,
|
||||
"sample_contacts_with_companies": sample or [],
|
||||
"test_query_result": test_result or [],
|
||||
"note": "If company_count is 0, the JOIN might not be working"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Debug failed: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/contacts", response_model=dict)
|
||||
async def get_contacts(
|
||||
search: Optional[str] = None,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user