[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")
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ def parse_tradingtwins_html(html_body):
|
||||
|
||||
return data
|
||||
|
||||
def process_leads():
|
||||
def process_leads(auto_sync=True):
|
||||
init_db()
|
||||
new_count = 0
|
||||
try:
|
||||
@@ -120,11 +120,21 @@ def process_leads():
|
||||
logger.info(f"Found {len(emails)} Tradingtwins emails.")
|
||||
|
||||
for email in emails:
|
||||
# ... (parsing logic remains same)
|
||||
body = email.get('body', {}).get('content', '')
|
||||
lead_data = parse_tradingtwins_html(body)
|
||||
received_at_str = email.get('receivedDateTime')
|
||||
|
||||
# Add raw body for reference
|
||||
# Convert ISO string to datetime object
|
||||
received_at = None
|
||||
if received_at_str:
|
||||
try:
|
||||
received_at = datetime.fromisoformat(received_at_str.replace('Z', '+00:00'))
|
||||
except:
|
||||
pass
|
||||
|
||||
lead_data = parse_tradingtwins_html(body)
|
||||
lead_data['raw_body'] = body
|
||||
lead_data['received_at'] = received_at
|
||||
|
||||
company_name = lead_data.get('company')
|
||||
if not company_name or company_name == '-':
|
||||
@@ -132,17 +142,18 @@ def process_leads():
|
||||
lead_data['company'] = company_name
|
||||
|
||||
if not company_name:
|
||||
logger.warning(f"Skipping email {email['id']}: No company or contact name found.")
|
||||
continue
|
||||
|
||||
logger.info(f"Ingesting Lead: {company_name} (ID: {lead_data.get('id')})")
|
||||
lead_data['id'] = lead_data.get('source_id') or f"tt_{int(datetime.now().timestamp())}"
|
||||
|
||||
# Save to local DB (status=new)
|
||||
if insert_lead(lead_data):
|
||||
logger.info(f" -> Successfully saved to DB.")
|
||||
logger.info(f" -> Ingested: {company_name}")
|
||||
new_count += 1
|
||||
else:
|
||||
logger.info(f" -> Lead already exists (skipped).")
|
||||
|
||||
if new_count > 0 and auto_sync:
|
||||
logger.info(f"Triggering auto-sync for {new_count} new leads...")
|
||||
from enrich import run_sync
|
||||
run_sync()
|
||||
|
||||
return new_count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user