|
| 1 | +""" |
| 2 | +CRM integration settings — HubSpot and Salesforce connection management. |
| 3 | +""" |
| 4 | +import logging |
| 5 | +from typing import Annotated, Optional |
| 6 | + |
| 7 | +from fastapi import APIRouter, Depends, HTTPException |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +from database import get_supabase |
| 11 | +from middleware.auth import get_current_user |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | +router = APIRouter(prefix="/integrations/crm", tags=["crm-integrations"]) |
| 15 | + |
| 16 | + |
| 17 | +# ─── Pydantic models ────────────────────────────────────────────────────────── |
| 18 | + |
| 19 | +class HubSpotSaveBody(BaseModel): |
| 20 | + api_key: str |
| 21 | + |
| 22 | + |
| 23 | +class SalesforceSaveBody(BaseModel): |
| 24 | + consumer_key: str |
| 25 | + consumer_secret: str |
| 26 | + username: str |
| 27 | + password: str |
| 28 | + security_token: str |
| 29 | + |
| 30 | + |
| 31 | +# ─── HubSpot endpoints ──────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +@router.get("/hubspot/status") |
| 34 | +async def hubspot_status(current_user: Annotated[dict, Depends(get_current_user)]): |
| 35 | + uid = current_user["id"] |
| 36 | + supabase = get_supabase() |
| 37 | + row = supabase.table("user_profiles").select("hubspot_connected, hubspot_api_key").eq("id", uid).single().execute() |
| 38 | + data = row.data or {} |
| 39 | + return { |
| 40 | + "connected": bool(data.get("hubspot_connected")), |
| 41 | + "has_key": bool(data.get("hubspot_api_key")), |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +@router.post("/hubspot/connect") |
| 46 | +async def hubspot_connect(body: HubSpotSaveBody, current_user: Annotated[dict, Depends(get_current_user)]): |
| 47 | + """Save HubSpot Private App token and verify it works.""" |
| 48 | + from services.hubspot_service import test_connection |
| 49 | + |
| 50 | + uid = current_user["id"] |
| 51 | + ok = await test_connection(body.api_key) |
| 52 | + if not ok: |
| 53 | + raise HTTPException(status_code=400, detail="Invalid HubSpot API key — connection test failed.") |
| 54 | + |
| 55 | + supabase = get_supabase() |
| 56 | + supabase.table("user_profiles").update({ |
| 57 | + "hubspot_api_key": body.api_key, |
| 58 | + "hubspot_connected": True, |
| 59 | + }).eq("id", uid).execute() |
| 60 | + return {"connected": True} |
| 61 | + |
| 62 | + |
| 63 | +@router.post("/hubspot/test") |
| 64 | +async def hubspot_test(current_user: Annotated[dict, Depends(get_current_user)]): |
| 65 | + """Send a test ping to HubSpot.""" |
| 66 | + from services.hubspot_service import test_connection |
| 67 | + |
| 68 | + uid = current_user["id"] |
| 69 | + supabase = get_supabase() |
| 70 | + row = supabase.table("user_profiles").select("hubspot_api_key").eq("id", uid).single().execute() |
| 71 | + api_key = (row.data or {}).get("hubspot_api_key", "") |
| 72 | + if not api_key: |
| 73 | + raise HTTPException(status_code=400, detail="HubSpot not connected.") |
| 74 | + |
| 75 | + ok = await test_connection(api_key) |
| 76 | + return {"success": ok} |
| 77 | + |
| 78 | + |
| 79 | +@router.delete("/hubspot/disconnect") |
| 80 | +async def hubspot_disconnect(current_user: Annotated[dict, Depends(get_current_user)]): |
| 81 | + uid = current_user["id"] |
| 82 | + supabase = get_supabase() |
| 83 | + supabase.table("user_profiles").update({ |
| 84 | + "hubspot_api_key": None, |
| 85 | + "hubspot_connected": False, |
| 86 | + }).eq("id", uid).execute() |
| 87 | + return {"connected": False} |
| 88 | + |
| 89 | + |
| 90 | +# ─── Salesforce endpoints ───────────────────────────────────────────────────── |
| 91 | + |
| 92 | +@router.get("/salesforce/status") |
| 93 | +async def salesforce_status(current_user: Annotated[dict, Depends(get_current_user)]): |
| 94 | + uid = current_user["id"] |
| 95 | + supabase = get_supabase() |
| 96 | + row = supabase.table("user_profiles").select("sf_connected, sf_username").eq("id", uid).single().execute() |
| 97 | + data = row.data or {} |
| 98 | + return { |
| 99 | + "connected": bool(data.get("sf_connected")), |
| 100 | + "username": data.get("sf_username"), |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | +@router.post("/salesforce/connect") |
| 105 | +async def salesforce_connect(body: SalesforceSaveBody, current_user: Annotated[dict, Depends(get_current_user)]): |
| 106 | + """Save Salesforce credentials and verify they work.""" |
| 107 | + from services.salesforce_service import test_connection |
| 108 | + |
| 109 | + uid = current_user["id"] |
| 110 | + ok = await test_connection( |
| 111 | + body.consumer_key, body.consumer_secret, |
| 112 | + body.username, body.password, body.security_token, |
| 113 | + ) |
| 114 | + if not ok: |
| 115 | + raise HTTPException(status_code=400, detail="Salesforce authentication failed. Check your credentials.") |
| 116 | + |
| 117 | + supabase = get_supabase() |
| 118 | + supabase.table("user_profiles").update({ |
| 119 | + "sf_consumer_key": body.consumer_key, |
| 120 | + "sf_consumer_secret": body.consumer_secret, |
| 121 | + "sf_username": body.username, |
| 122 | + "sf_password": body.password, |
| 123 | + "sf_security_token": body.security_token, |
| 124 | + "sf_connected": True, |
| 125 | + }).eq("id", uid).execute() |
| 126 | + return {"connected": True} |
| 127 | + |
| 128 | + |
| 129 | +@router.post("/salesforce/test") |
| 130 | +async def salesforce_test(current_user: Annotated[dict, Depends(get_current_user)]): |
| 131 | + """Test the saved Salesforce credentials.""" |
| 132 | + from services.salesforce_service import test_connection |
| 133 | + |
| 134 | + uid = current_user["id"] |
| 135 | + supabase = get_supabase() |
| 136 | + row = supabase.table("user_profiles").select( |
| 137 | + "sf_consumer_key, sf_consumer_secret, sf_username, sf_password, sf_security_token" |
| 138 | + ).eq("id", uid).single().execute() |
| 139 | + d = row.data or {} |
| 140 | + if not d.get("sf_username"): |
| 141 | + raise HTTPException(status_code=400, detail="Salesforce not connected.") |
| 142 | + |
| 143 | + ok = await test_connection( |
| 144 | + d.get("sf_consumer_key", ""), d.get("sf_consumer_secret", ""), |
| 145 | + d.get("sf_username", ""), d.get("sf_password", ""), d.get("sf_security_token", ""), |
| 146 | + ) |
| 147 | + return {"success": ok} |
| 148 | + |
| 149 | + |
| 150 | +@router.delete("/salesforce/disconnect") |
| 151 | +async def salesforce_disconnect(current_user: Annotated[dict, Depends(get_current_user)]): |
| 152 | + uid = current_user["id"] |
| 153 | + supabase = get_supabase() |
| 154 | + supabase.table("user_profiles").update({ |
| 155 | + "sf_consumer_key": None, "sf_consumer_secret": None, |
| 156 | + "sf_username": None, "sf_password": None, "sf_security_token": None, |
| 157 | + "sf_connected": False, |
| 158 | + }).eq("id", uid).execute() |
| 159 | + return {"connected": False} |
0 commit comments