[31388f42] Deep CE Sync: Support contact creation and automated enrichment workflow
This commit is contained in:
@@ -6,7 +6,8 @@ import sqlite3
|
||||
# Füge das Hauptverzeichnis zum Python-Pfad hinzu, damit der Connector gefunden wird
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
from company_explorer_connector import handle_company_workflow, get_company_details
|
||||
from db import get_leads, DB_PATH
|
||||
from db import get_leads, DB_PATH, update_lead_metadata
|
||||
from lookup_role import lookup_person_role
|
||||
|
||||
def update_lead_enrichment(lead_id, data, status):
|
||||
"""Aktualisiert einen Lead in der Datenbank mit neuen Enrichment-Daten und einem neuen Status."""
|
||||
@@ -26,18 +27,54 @@ def refresh_ce_data(lead_id, ce_id):
|
||||
print(f"Refreshing data for CE ID {ce_id}...")
|
||||
ce_data = get_company_details(ce_id)
|
||||
|
||||
# Bestehende Enrichment-Daten holen, um nichts zu überschreiben
|
||||
# (Vereinfachung: Wir bauen das dict neu auf)
|
||||
enrichment_data = {
|
||||
# Bestehende Enrichment-Daten holen
|
||||
leads = get_leads()
|
||||
lead = next((l for l in leads if l['id'] == lead_id), None)
|
||||
|
||||
enrichment_data = {}
|
||||
if lead and lead.get('enrichment_data'):
|
||||
try:
|
||||
enrichment_data = json.loads(lead['enrichment_data'])
|
||||
except:
|
||||
pass
|
||||
|
||||
enrichment_data.update({
|
||||
"sync_status": "refreshed",
|
||||
"ce_id": ce_id,
|
||||
"message": "Data refreshed from CE",
|
||||
"ce_data": ce_data
|
||||
}
|
||||
})
|
||||
|
||||
update_lead_enrichment(lead_id, enrichment_data, status='synced')
|
||||
return ce_data
|
||||
|
||||
def enrich_contact_role(lead):
|
||||
"""
|
||||
Versucht, die Rolle des Kontakts via SerpAPI zu finden und speichert sie in den Metadaten.
|
||||
"""
|
||||
meta = {}
|
||||
if lead.get('lead_metadata'):
|
||||
try:
|
||||
meta = json.loads(lead.get('lead_metadata'))
|
||||
except:
|
||||
pass
|
||||
|
||||
# Skip if we already have a role (and it's not None/Unknown)
|
||||
if meta.get('role') and meta.get('role') != "Unbekannt":
|
||||
return meta.get('role')
|
||||
|
||||
print(f"Looking up role for {lead['contact_name']} at {lead['company_name']}...")
|
||||
role = lookup_person_role(lead['contact_name'], lead['company_name'])
|
||||
|
||||
if role:
|
||||
print(f" -> Found role: {role}")
|
||||
meta['role'] = role
|
||||
update_lead_metadata(lead['id'], meta)
|
||||
else:
|
||||
print(" -> No role found.")
|
||||
|
||||
return role
|
||||
|
||||
def run_sync():
|
||||
"""
|
||||
Haupt-Synchronisationsprozess.
|
||||
@@ -56,15 +93,33 @@ def run_sync():
|
||||
company_name = lead['company_name']
|
||||
print(f"\n--- Processing Lead ID: {lead['id']}, Company: '{company_name}' ---")
|
||||
|
||||
# Rufe den zentralen Workflow auf, den wir im Connector definiert haben
|
||||
# Diese Funktion kümmert sich um alles: prüfen, erstellen, discovern, pollen, analysieren
|
||||
result = handle_company_workflow(company_name)
|
||||
# 1. Contact Enrichment (Role Lookup via SerpAPI)
|
||||
role = enrich_contact_role(lead)
|
||||
|
||||
# 2. Prepare Contact Info for CE
|
||||
meta = {}
|
||||
if lead.get('lead_metadata'):
|
||||
try:
|
||||
meta = json.loads(lead.get('lead_metadata'))
|
||||
except:
|
||||
pass
|
||||
|
||||
contact_info = {
|
||||
"first_name": meta.get('contact_first', ''),
|
||||
"last_name": meta.get('contact_last', lead['contact_name'].split(' ')[-1] if lead['contact_name'] else ''),
|
||||
"email": lead['email'],
|
||||
"job_title": meta.get('role', role), # The raw title or Gemini result
|
||||
"role": meta.get('role', role) # Currently mapped to same field
|
||||
}
|
||||
|
||||
# 3. Company Enrichment (CE Workflow with Contact)
|
||||
result = handle_company_workflow(company_name, contact_info=contact_info)
|
||||
|
||||
# Bereite die Daten für die Speicherung in der DB vor
|
||||
enrichment_data = {
|
||||
"sync_status": result.get("status"),
|
||||
"ce_id": result.get("data", {}).get("id") if result.get("data") else None,
|
||||
"message": result.get("message"),
|
||||
"message": result.get("message", "Sync successful"),
|
||||
"ce_data": result.get("data")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user