perf: add opportunity detail read context

This commit is contained in:
plx
2026-08-14 13:24:37 +00:00
parent 07dc9db198
commit 7d138eccdc
13 changed files with 484 additions and 121 deletions

View File

@@ -36,7 +36,11 @@ from app.commercial_service import (
upsert_customer,
)
from app.opportunity_service import ensure_opportunity_schema, get_opportunity, list_opportunities
from app.email_identity_extraction_service import extract_identity_for_opportunity, is_plausible_company_mention
from app.email_identity_extraction_service import (
extract_identity_for_opportunity,
is_plausible_company_mention,
latest_identity_for_opportunity,
)
CACHE_SOURCE = "informa_pipeline_api"
@@ -823,7 +827,10 @@ def _identity_conflicts_with_linked_customer(identity: Optional[Dict[str, Any]],
return not _identity_mentions_match_name(identity, linked_name)
def email_identity_review_for_opportunity(opportunity_id: str, *, refresh: bool = False) -> Dict[str, Any]:
def email_identity_review_for_opportunity(
opportunity_id: str, *, refresh: bool = False,
opportunity: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""Return the operator-facing identity review for an opportunity.
This is read-only except when refresh=True, where it stores a new extraction.
@@ -831,16 +838,18 @@ def email_identity_review_for_opportunity(opportunity_id: str, *, refresh: bool
can show cases like: email mentions Dietimport S.A. but the opportunity is
linked to Fmrl - Imobiliária S.A.
"""
ensure_fiscal_enrichment_schema()
opportunity = get_opportunity(opportunity_id)
opportunity = opportunity or get_opportunity(opportunity_id)
if not opportunity:
return {"ok": False, "reason": "opportunity_not_found"}
try:
identity = extract_identity_for_opportunity(
opportunity_id,
refresh=refresh,
use_llm=bool(getattr(settings, "email_identity_extraction_use_llm", True)),
)
if refresh:
identity = extract_identity_for_opportunity(
opportunity_id,
refresh=True,
use_llm=bool(getattr(settings, "email_identity_extraction_use_llm", True)),
)
else:
identity = latest_identity_for_opportunity(opportunity_id)
except Exception as exc:
return {"ok": False, "reason": f"email_identity_extraction_failed: {exc}"}
@@ -1266,7 +1275,6 @@ def reject_fiscal_suggestion(suggestion_id: str, *, actor: str = "operator", rea
def list_fiscal_suggestions_for_opportunity(opportunity_id: str, *, limit: int = 5) -> List[Dict[str, Any]]:
ensure_fiscal_enrichment_schema()
with engine.begin() as conn:
rows = conn.execute(text("""
SELECT