[30e88f42] Einfügen

Einfügen
This commit is contained in:
2026-02-22 14:31:00 +00:00
parent abdbb84938
commit e4ed1073f6
11 changed files with 290 additions and 174 deletions

View File

@@ -243,41 +243,45 @@ def provision_superoffice_contact(
# 1c. Update CRM Snapshot Data (The Double Truth)
changed = False
if req.crm_name:
company.crm_name = req.crm_name
changed = True
if req.crm_website:
company.crm_website = req.crm_website
changed = True
name_changed_significantly = False
# NEW: Handle Vertical Override from SuperOffice
if req.crm_industry_name:
# Check if valid industry
valid_industry = db.query(Industry).filter(Industry.name == req.crm_industry_name).first()
if valid_industry:
if company.industry_ai != req.crm_industry_name:
logger.info(f"Overriding Industry for {company.name}: {company.industry_ai} -> {req.crm_industry_name} (from CRM)")
company.industry_ai = req.crm_industry_name
# Trigger metric re-extraction? Maybe later. For now, just update.
changed = True
else:
logger.warning(f"CRM provided industry '{req.crm_industry_name}' not found in DB. Ignoring.")
# Simple Mismatch Check
if company.website and company.crm_website:
def norm(u): return str(u).lower().replace("https://", "").replace("http://", "").replace("www.", "").strip("/")
if norm(company.website) != norm(company.crm_website):
company.data_mismatch_score = 0.8 # High mismatch
if req.crm_name and req.crm_name != company.crm_name:
logger.info(f"CRM Name Change detected for ID {company.crm_id}: {company.crm_name} -> {req.crm_name}")
company.crm_name = req.crm_name
# If the name changes, we should potentially re-evaluate the whole company
# especially if the status was already ENRICHED
if company.status == "ENRICHED":
name_changed_significantly = True
changed = True
if req.crm_website:
if company.crm_website != req.crm_website:
company.crm_website = req.crm_website
changed = True
else:
if company.data_mismatch_score != 0.0:
company.data_mismatch_score = 0.0
changed = True
# ...
if changed:
company.updated_at = datetime.utcnow()
if name_changed_significantly:
logger.info(f"Triggering FRESH discovery for {company.name} due to CRM name change.")
company.status = "NEW"
# We don't change the internal 'name' yet, Discovery will do that or we keep it as anchor.
# But we must clear old results to avoid stale data.
company.industry_ai = None
company.ai_opener = None
company.ai_opener_secondary = None
background_tasks.add_task(run_discovery_task, company.id)
db.commit()
# If we just triggered a fresh discovery, tell the worker to wait.
if name_changed_significantly:
return ProvisioningResponse(
status="processing",
company_name=company.crm_name
)
# 2. Find Contact (Person)
if req.so_person_id is None:
# Just a company sync, but return all company-level metadata
@@ -316,13 +320,14 @@ def provision_superoffice_contact(
mappings = db.query(JobRoleMapping).all()
found_role = None
for m in mappings:
# Check pattern type (Regex vs Simple) - simplified here
pattern_clean = m.pattern.replace("%", "").lower()
if pattern_clean in req.job_title.lower():
found_role = m.role
break
if found_role:
# ALWAYS update role, even if to None, to avoid 'sticking' old roles
if found_role != person.role:
logger.info(f"Role Change for {person.so_person_id}: {person.role} -> {found_role}")
person.role = found_role
db.commit()