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

@@ -86,7 +86,9 @@ def get_operation_links(opportunity_id: str) -> List[Dict[str, Any]]:
"""), {"opportunity_id": opportunity_id}).mappings().all()
return [dict(r) for r in rows]
def _commercial_document_operation_fallbacks(opportunity_id: str) -> Dict[tuple, Dict[str, Any]]:
def _commercial_document_operation_fallbacks(
opportunity_id: str, resolved_documents: Optional[List[Dict[str, Any]]] = None,
) -> Dict[tuple, Dict[str, Any]]:
"""Infer operation cards from linked commercial_documents when operation_links lag.
Reconstructed opportunities often have commercial_documents imported from
@@ -95,7 +97,7 @@ def _commercial_document_operation_fallbacks(opportunity_id: str) -> Dict[tuple,
"""
try:
from app.document_reconciliation_service import resolve_document_links, select_valid_primary
resolved = resolve_document_links(opportunity_id)
resolved = resolved_documents if resolved_documents is not None else resolve_document_links(opportunity_id)
rows = [row for kind in ("invoice", "quotation", "quote", "proforma")
if (row := select_valid_primary(resolved, kind)) is not None]
except Exception:
@@ -132,10 +134,13 @@ def _commercial_document_operation_fallbacks(opportunity_id: str) -> Dict[tuple,
return fallbacks
def get_operation_snapshot(opportunity_id: str) -> Dict[str, Any]:
links = get_operation_links(opportunity_id)
def get_operation_snapshot(
opportunity_id: str, *, links: Optional[List[Dict[str, Any]]] = None,
resolved_documents: Optional[List[Dict[str, Any]]] = None,
) -> Dict[str, Any]:
links = list(links) if links is not None else get_operation_links(opportunity_id)
by_key = {(x["system"], x["external_type"]): x for x in links}
commercial_fallbacks = _commercial_document_operation_fallbacks(opportunity_id)
commercial_fallbacks = _commercial_document_operation_fallbacks(opportunity_id, resolved_documents)
cards = []
for card in OPERATION_CARDS:
link = by_key.get((card["system"], card["external_type"])) or commercial_fallbacks.get((card["system"], card["external_type"]))