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

@@ -1210,8 +1210,11 @@ def _outbox_items_for_opportunity(opportunity_id: str, *, target_system: str | N
if len(filtered) >= limit:
break
return filtered
def opportunity_integrations_panel_html(opportunity_id: str) -> str:
items = _outbox_items_for_opportunity(opportunity_id, target_system=None, limit=16)
def opportunity_integrations_panel_html(
opportunity_id: str, *, preloaded_items: Optional[list[dict]] = None,
) -> str:
items = (list(preloaded_items)[:16] if preloaded_items is not None
else _outbox_items_for_opportunity(opportunity_id, target_system=None, limit=16))
counts = {"pending": 0, "failed": 0, "blocked": 0, "dry_run": 0}
for item in items:
status = str(item.get("status") or "pending")
@@ -1270,8 +1273,13 @@ def opportunity_integrations_panel_html(opportunity_id: str) -> str:
</div>
</section>
'''
def opportunity_outbox_panel_html(opportunity_id: str, *, target_system: str = "jasmin") -> str:
items = _outbox_items_for_opportunity(opportunity_id, target_system=target_system, limit=12)
def opportunity_outbox_panel_html(
opportunity_id: str, *, target_system: str = "jasmin",
preloaded_items: Optional[list[dict]] = None,
) -> str:
items = ([item for item in preloaded_items if item.get("target_system") == target_system][:12]
if preloaded_items is not None
else _outbox_items_for_opportunity(opportunity_id, target_system=target_system, limit=12))
if not items:
return ""
rows = ""
@@ -1306,10 +1314,17 @@ def opportunity_outbox_panel_html(opportunity_id: str, *, target_system: str = "
</div>
</div>
'''
def jasmin_documents_html(opportunity_id: str, *, notice: str = "", error_notice: str = "") -> str:
def jasmin_documents_html(
opportunity_id: str, *, notice: str = "", error_notice: str = "",
preloaded_documents: Optional[list[dict]] = None,
preloaded_candidates: Optional[list[dict]] = None,
preloaded_customer: Optional[dict] = None,
preloaded_outbox: Optional[list[dict]] = None,
) -> str:
try:
from app.commercial_service import list_commercial_documents
docs = list_commercial_documents(opportunity_id=opportunity_id, limit=20)
docs = (list(preloaded_documents)[:20] if preloaded_documents is not None
else list_commercial_documents(opportunity_id=opportunity_id, limit=20))
except Exception as exc:
docs = []
error = str(exc)
@@ -1317,7 +1332,8 @@ def jasmin_documents_html(opportunity_id: str, *, notice: str = "", error_notice
error = ""
try:
from app.jasmin_backfill_service import find_jasmin_document_candidates_for_opportunity
jasmin_candidates = find_jasmin_document_candidates_for_opportunity(opportunity_id, limit=8)
jasmin_candidates = (list(preloaded_candidates)[:8] if preloaded_candidates is not None
else find_jasmin_document_candidates_for_opportunity(opportunity_id, limit=8))
except Exception as exc:
jasmin_candidates = []
if not error:
@@ -1325,7 +1341,8 @@ def jasmin_documents_html(opportunity_id: str, *, notice: str = "", error_notice
linked_tax_id = ""
try:
from app.commercial_service import get_customer_for_opportunity, normalize_tax_id
linked_customer = get_customer_for_opportunity(opportunity_id)
linked_customer = (preloaded_customer if preloaded_customer is not None
else get_customer_for_opportunity(opportunity_id))
linked_tax_id = normalize_tax_id((linked_customer or {}).get("tax_id"))
except Exception:
linked_tax_id = ""
@@ -1539,7 +1556,9 @@ def jasmin_documents_html(opportunity_id: str, *, notice: str = "", error_notice
notice_html = f'<div class="alert alert-info py-2 small mb-2">{esc(notice)}</div>' if notice else ''
error_notice_html = f'<div class="alert alert-danger py-2 small mb-2"><strong>Não foi possível pedir a ação.</strong><br>{esc(error_notice).replace(chr(10), "<br>")}</div>' if error_notice else ''
error_html = f'<div class="alert alert-warning py-2 small mb-2">{esc(error)}</div>' if error else ''
outbox_html = opportunity_outbox_panel_html(opportunity_id, target_system="jasmin")
outbox_html = opportunity_outbox_panel_html(
opportunity_id, target_system="jasmin", preloaded_items=preloaded_outbox,
)
refreshed_at = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
if invoice_source_exists:
convert_invoice_button_html = (
@@ -1762,10 +1781,16 @@ def opportunity_add_item_form_html(opportunity_id: str, products: list[dict]) ->
</div>
</form>
'''
def opportunity_products_panel_html(opportunity_id: str, *, notice: str = "", error_notice: str = "") -> str:
def opportunity_products_panel_html(
opportunity_id: str, *, notice: str = "", error_notice: str = "",
preloaded_items: Optional[list[dict]] = None,
preloaded_products: Optional[list[dict]] = None,
) -> str:
try:
items = list_opportunity_items(opportunity_id)
active_products = list_products(active="true", limit=300)
items = (list(preloaded_items) if preloaded_items is not None
else list_opportunity_items(opportunity_id))
active_products = (list(preloaded_products) if preloaded_products is not None
else list_products(active="true", limit=300))
except Exception as exc:
return f'<section id="opportunity-products-panel" class="card cf-card"><div class="card-body"><div class="alert alert-danger">Erro ao carregar produtos: {esc(exc)}</div></div></section>'
total = sum(