[31388f42] Fix contact sync: separate first/last names and enable CE role mapping
This commit is contained in:
@@ -75,9 +75,73 @@ def enrich_contact_role(lead):
|
||||
|
||||
return role
|
||||
|
||||
def sync_single_lead(lead_id):
|
||||
"""
|
||||
Verarbeitet einen einzelnen Lead: Rolle suchen, CE-Sync, Analyse triggern.
|
||||
"""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT * FROM leads WHERE id = ?', (lead_id,))
|
||||
lead = c.fetchone()
|
||||
conn.close()
|
||||
|
||||
if not lead:
|
||||
return {"status": "error", "message": "Lead not found"}
|
||||
|
||||
lead_dict = dict(lead)
|
||||
company_name = lead_dict['company_name']
|
||||
print(f"\n--- Manually Syncing Lead ID: {lead_id}, Company: '{company_name}' ---")
|
||||
|
||||
# 1. Contact Enrichment (Role Lookup)
|
||||
role = enrich_contact_role(lead_dict)
|
||||
|
||||
# 2. Prepare Contact Info
|
||||
meta = {}
|
||||
if lead_dict.get('lead_metadata'):
|
||||
try: meta = json.loads(lead_dict['lead_metadata'])
|
||||
except: pass
|
||||
|
||||
# Smarter name splitting if meta is empty (for repaired leads)
|
||||
full_name = lead_dict.get('contact_name', '')
|
||||
first_name = meta.get('contact_first')
|
||||
last_name = meta.get('contact_last')
|
||||
|
||||
if not first_name and full_name:
|
||||
parts = full_name.strip().split(' ')
|
||||
if len(parts) > 1:
|
||||
first_name = parts[0]
|
||||
last_name = ' '.join(parts[1:])
|
||||
else:
|
||||
last_name = full_name
|
||||
first_name = ''
|
||||
|
||||
contact_info = {
|
||||
"first_name": first_name,
|
||||
"last_name": last_name,
|
||||
"email": lead_dict['email'],
|
||||
"job_title": meta.get('role', role),
|
||||
"role": None, # Set to None so CE can use its RoleMappingService
|
||||
"is_primary": True
|
||||
}
|
||||
|
||||
# 3. CE Workflow
|
||||
result = handle_company_workflow(company_name, contact_info=contact_info)
|
||||
|
||||
# 4. Save results
|
||||
enrichment_data = {
|
||||
"sync_status": result.get("status"),
|
||||
"ce_id": result.get("data", {}).get("id") if result.get("data") else None,
|
||||
"message": result.get("message", "Manual sync successful"),
|
||||
"ce_data": result.get("data")
|
||||
}
|
||||
|
||||
update_lead_enrichment(lead_id, enrichment_data, status='synced')
|
||||
return result
|
||||
|
||||
def run_sync():
|
||||
"""
|
||||
Haupt-Synchronisationsprozess.
|
||||
Haupt-Synchronisationsprozess (Batch).
|
||||
Holt alle neuen Leads und stößt den Company Explorer Workflow für jeden an.
|
||||
"""
|
||||
# Hole nur die Leads, die wirklich neu sind und noch nicht verarbeitet wurden
|
||||
|
||||
Reference in New Issue
Block a user