fix: deduplicate reconciliation loose items

This commit is contained in:
plx
2026-08-14 14:22:43 +00:00
parent c4d55d37ed
commit 9b3c3b32ef
3 changed files with 93 additions and 4 deletions

View File

@@ -355,6 +355,19 @@ def render_reconciliation_table(items: list[dict]) -> str:
"""
def loose_reconciliation_items(
items: list[dict], displayed_candidates: list[dict],
) -> list[dict]:
"""Exclude only exact evidence IDs represented by visible process cards."""
represented_ids = {
str(item_id)
for candidate in displayed_candidates
for item_id in (candidate.get("item_ids") or [])
if item_id is not None
}
return [item for item in items if str(item.get("id")) not in represented_ids]
@router.get("/reconciliation", response_class=HTMLResponse)
@router.get("/reconciliacao", response_class=HTMLResponse)
async def reconciliation_page(status: Optional[str] = "open", external_type: Optional[str] = "all", notice: Optional[str] = None, days: Optional[int] = 3):
@@ -363,7 +376,10 @@ async def reconciliation_page(status: Optional[str] = "open", external_type: Opt
item_type = None if external_type == "all" else external_type
recent_days = min(max(int(days or 3), 1), 90)
items = sort_items_for_operator(list_reconciliation_items(status=status, external_type=item_type, limit=100, days=recent_days))
process_candidates = list_reconciliation_process_candidates(status="open", days=recent_days, limit=8)
process_candidates = list_reconciliation_process_candidates(
status=status, external_type=item_type, days=recent_days, limit=8,
)
items = loose_reconciliation_items(items, process_candidates)
summary = reconciliation_summary(days=recent_days)
enrichment_summary = fiscal_enrichment_summary()

View File

@@ -2547,8 +2547,11 @@ def _record_reconciliation_decision(
logger.warning("failed to record reconciliation decision %s: %s", decision_type, exc)
def list_reconciliation_process_candidates(*, status: str = "open", days: int = 3, limit: int = 20) -> List[Dict[str, Any]]:
"""Return grouped process candidates built from recent open items.
def list_reconciliation_process_candidates(
*, status: str = "open", external_type: Optional[str] = None,
days: int = 3, limit: int = 20,
) -> List[Dict[str, Any]]:
"""Return process candidates built from items eligible in the active filter.
A candidate is shown when at least two pieces of evidence share the same
NIF/email/name, when one item already has a strong open-operation
@@ -2556,7 +2559,10 @@ def list_reconciliation_process_candidates(*, status: str = "open", days: int =
(for example an Odoo sale order without ClientFlow).
"""
try:
items = list_reconciliation_items(status=status, limit=300, days=days)
item_filters: Dict[str, Any] = {"status": status, "limit": 300, "days": days}
if external_type:
item_filters["external_type"] = external_type
items = list_reconciliation_items(**item_filters)
except Exception as exc: # pragma: no cover - production safety guard
logger.warning("failed to list reconciliation items for process candidates: %s", exc)
return []