[31388f42] Deep CE Sync: Support contact creation and automated enrichment workflow

This commit is contained in:
2026-03-02 10:01:11 +00:00
parent c753c2feab
commit fdb4e2ff23
3 changed files with 143 additions and 73 deletions

View File

@@ -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