[31388f42] Deep CE Sync: Support contact creation and automated enrichment workflow
This commit is contained in:
@@ -64,10 +64,24 @@ def trigger_analysis(company_id: int) -> dict:
|
||||
def get_company_details(company_id: int) -> dict:
|
||||
"""Holt die vollständigen Details zu einem Unternehmen."""
|
||||
return _make_api_request("GET", f"/companies/{company_id}")
|
||||
|
||||
def create_contact(company_id: int, contact_data: dict) -> dict:
|
||||
"""Erstellt einen neuen Kontakt für ein Unternehmen im Company Explorer."""
|
||||
payload = {
|
||||
"company_id": company_id,
|
||||
"first_name": contact_data.get("first_name"),
|
||||
"last_name": contact_data.get("last_name"),
|
||||
"email": contact_data.get("email"),
|
||||
"job_title": contact_data.get("job_title"),
|
||||
"role": contact_data.get("role"),
|
||||
"is_primary": contact_data.get("is_primary", True)
|
||||
}
|
||||
return _make_api_request("POST", "/contacts", json_data=payload)
|
||||
|
||||
def handle_company_workflow(company_name: str) -> dict:
|
||||
def handle_company_workflow(company_name: str, contact_info: dict = None) -> dict:
|
||||
"""
|
||||
Haupt-Workflow: Prüft, erstellt und reichert ein Unternehmen an.
|
||||
Optional wird auch ein Kontakt angelegt.
|
||||
Gibt die finalen Unternehmensdaten zurück.
|
||||
"""
|
||||
print(f"Workflow gestartet für: '{company_name}'")
|
||||
@@ -75,70 +89,60 @@ def handle_company_workflow(company_name: str) -> dict:
|
||||
# 1. Prüfen, ob das Unternehmen existiert
|
||||
existence_check = check_company_existence(company_name)
|
||||
|
||||
company_id = None
|
||||
if existence_check.get("exists"):
|
||||
company_id = existence_check["company"]["id"]
|
||||
print(f"Unternehmen '{company_name}' (ID: {company_id}) existiert bereits.")
|
||||
final_company_data = get_company_details(company_id)
|
||||
return {"status": "found", "data": final_company_data}
|
||||
|
||||
if "error" in existence_check:
|
||||
elif "error" in existence_check:
|
||||
print(f"Fehler bei der Existenzprüfung: {existence_check['error']}")
|
||||
return {"status": "error", "message": existence_check['error']}
|
||||
else:
|
||||
# 2. Wenn nicht, Unternehmen erstellen
|
||||
print(f"Unternehmen '{company_name}' nicht gefunden. Erstelle es...")
|
||||
creation_response = create_company(company_name)
|
||||
|
||||
if "error" in creation_response:
|
||||
return {"status": "error", "message": creation_response['error']}
|
||||
|
||||
company_id = creation_response.get("id")
|
||||
print(f"Unternehmen '{company_name}' erfolgreich mit ID {company_id} erstellt.")
|
||||
|
||||
# 2. Wenn nicht, Unternehmen erstellen
|
||||
print(f"Unternehmen '{company_name}' nicht gefunden. Erstelle es...")
|
||||
creation_response = create_company(company_name)
|
||||
|
||||
if "error" in creation_response:
|
||||
print(f"Fehler bei der Erstellung: {creation_response['error']}")
|
||||
return {"status": "error", "message": creation_response['error']}
|
||||
|
||||
company_id = creation_response.get("id")
|
||||
if not company_id:
|
||||
print(f"Fehler: Konnte keine ID aus der Erstellungs-Antwort extrahieren: {creation_response}")
|
||||
return {"status": "error", "message": "Failed to get company ID after creation."}
|
||||
|
||||
print(f"Unternehmen '{company_name}' erfolgreich mit ID {company_id} erstellt.")
|
||||
# 2b. Kontakt anlegen/aktualisieren (falls Info vorhanden)
|
||||
if company_id and contact_info:
|
||||
print(f"Lege Kontakt für {contact_info.get('last_name')} an...")
|
||||
contact_res = create_contact(company_id, contact_info)
|
||||
if "error" in contact_res:
|
||||
print(f"Hinweis: Kontakt konnte nicht angelegt werden: {contact_res['error']}")
|
||||
|
||||
# 3. Discovery anstoßen
|
||||
print(f"Starte Discovery für ID {company_id}...")
|
||||
discovery_status = trigger_discovery(company_id)
|
||||
if "error" in discovery_status:
|
||||
print(f"Fehler beim Anstoßen der Discovery: {discovery_status['error']}")
|
||||
return {"status": "error", "message": discovery_status['error']}
|
||||
|
||||
# 4. Warten, bis Discovery eine Website gefunden hat (Polling)
|
||||
max_wait_time = 30
|
||||
start_time = time.time()
|
||||
website_found = False
|
||||
print("Warte auf Abschluss der Discovery (max. 30s)...")
|
||||
while time.time() - start_time < max_wait_time:
|
||||
details = get_company_details(company_id)
|
||||
if details.get("website") and details["website"] not in ["", "k.A."]:
|
||||
print(f"Website gefunden: {details['website']}")
|
||||
website_found = True
|
||||
break
|
||||
time.sleep(3)
|
||||
print(".")
|
||||
|
||||
if not website_found:
|
||||
print("Discovery hat nach 30s keine Website gefunden. Breche Analyse ab.")
|
||||
final_data = get_company_details(company_id)
|
||||
return {"status": "created_discovery_timeout", "data": final_data}
|
||||
|
||||
# 5. Analyse anstoßen
|
||||
print(f"Starte Analyse für ID {company_id}...")
|
||||
analysis_status = trigger_analysis(company_id)
|
||||
if "error" in analysis_status:
|
||||
print(f"Fehler beim Anstoßen der Analyse: {analysis_status['error']}")
|
||||
return {"status": "error", "message": analysis_status['error']}
|
||||
|
||||
print("Analyse-Prozess erfolgreich in die Warteschlange gestellt.")
|
||||
# 3. Discovery anstoßen (falls Status NEW)
|
||||
# Wir holen Details, um den Status zu prüfen
|
||||
details = get_company_details(company_id)
|
||||
if details.get("status") == "NEW":
|
||||
print(f"Starte Discovery für ID {company_id}...")
|
||||
trigger_discovery(company_id)
|
||||
|
||||
# 4. Warten, bis Discovery eine Website gefunden hat (Polling)
|
||||
max_wait_time = 30
|
||||
start_time = time.time()
|
||||
website_found = False
|
||||
print("Warte auf Abschluss der Discovery (max. 30s)...")
|
||||
while time.time() - start_time < max_wait_time:
|
||||
details = get_company_details(company_id)
|
||||
if details.get("website") and details["website"] not in ["", "k.A."]:
|
||||
print(f"Website gefunden: {details['website']}")
|
||||
website_found = True
|
||||
break
|
||||
time.sleep(3)
|
||||
print(".")
|
||||
|
||||
# 5. Analyse anstoßen (falls Website da, aber noch nicht ENRICHED)
|
||||
if details.get("website") and details["website"] not in ["", "k.A."] and details.get("status") != "ENRICHED":
|
||||
print(f"Starte Analyse für ID {company_id}...")
|
||||
trigger_analysis(company_id)
|
||||
|
||||
# 6. Finale Daten abrufen und zurückgeben
|
||||
final_company_data = get_company_details(company_id)
|
||||
|
||||
return {"status": "created_and_enriched", "data": final_company_data}
|
||||
return {"status": "synced", "data": final_company_data}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user