fix(mapping): never count stale or foreign-year note targets as mapped
davoodmehraban89 committed
Oct 30, 2026 at 17:28 UTC
f858b44b3a3f79bcc7bd00b264ffdf11272fb1a9
1 file changed
+53
-4
backend/src/aram_backend/mapping_coverage.py
+53
-4
@@ -10,7 +10,7 @@ from sqlalchemy import select
10
from sqlalchemy.orm import Session
11
12
from aram_backend.mapping_service import MappingValidationError
13
-from aram_backend.models import AccountVersion, FiscalYear, MappingRule
13
+from aram_backend.models import AccountVersion, FiscalYear, MappingRule, NoteDefinition
14
15
16
@dataclass(frozen=True)
@@ -21,6 +21,10 @@ class MappingCoverage:
21
unmapped_accounts: tuple[str, ...]
22
ambiguous_accounts: tuple[str, ...]
23
orphan_rule_keys: tuple[str, ...]
24
+ # (rule_key, account_code, failure reason), stable sorted order.
25
+ # Keeping a dedicated queue means invalid targets are never hidden behind
26
+ # a valid rule for the same account.
27
+ invalid_target_rules: tuple[tuple[str, str, str], ...] = ()
28
29
30
def inspect_mapping_coverage(session: Session, *, year_id: int) -> MappingCoverage:
@@ -55,19 +59,64 @@ def inspect_mapping_coverage(session: Session, *, year_id: int) -> MappingCovera
59
for rule in rules:
60
current_rules.setdefault(rule.rule_key, rule)
61
62
+ # Resolve every currently referenced note in one bounded query and
63
+ # compute the latest version *within this year*. A cross-year or stale
64
+ # note reference must never count toward successful coverage.
65
+ note_ids = {r.target_note_id for r in current_rules.values()}
66
+ referenced_notes = {
67
+ note.id: note for note in session.scalars(
68
+ select(NoteDefinition).where(NoteDefinition.id.in_(note_ids))
69
+ )
70
+ }
71
+ year_notes = session.scalars(
72
+ select(NoteDefinition).where(NoteDefinition.year_id == year_id)
73
+ .order_by(NoteDefinition.note_number, NoteDefinition.version.desc(), NoteDefinition.id.desc())
74
+ ).all()
75
+ latest_notes: dict[str, NoteDefinition] = {}
76
+ for note in year_notes:
77
+ latest_notes.setdefault(note.note_number, note)
78
+
79
targets: dict[str, set[int]] = {code: set() for code in current_accounts}
80
orphan_keys: list[str] = []
81
+ invalid: list[tuple[str, str, str]] = []
82
+ invalid_accounts: set[str] = set()
83
for key, rule in current_rules.items():
84
if rule.account_code not in targets:
85
orphan_keys.append(key)
86
+ continue
87
+ note = referenced_notes.get(rule.target_note_id)
88
+ if note is None:
89
+ reason = "target_note_missing"
90
+ elif note.year_id != year_id:
91
+ reason = "target_note_wrong_year"
92
+ elif latest_notes[note.note_number].id != note.id:
93
+ reason = "target_note_stale"
94
+ else:
95
+ reason = None
96
+ if reason is not None:
97
+ invalid.append((key, rule.account_code, reason))
98
+ invalid_accounts.add(rule.account_code)
99
else:
100
targets[rule.account_code].add(rule.target_note_id)
101
66
- mapped = tuple(sorted(code for code, notes in targets.items() if len(notes) == 1))
67
- unmapped = tuple(sorted(code for code, notes in targets.items() if not notes))
68
- ambiguous = tuple(sorted(code for code, notes in targets.items() if len(notes) > 1))
102
+ # A partially valid account still needs human review, not a green
103
+ # "mapped" classification. Put it in the unmapped/action queue and
104
+ # expose precise invalid rules separately.
105
+ mapped = tuple(sorted(
106
+ code for code, notes in targets.items()
107
+ if code not in invalid_accounts and len(notes) == 1
108
+ ))
109
+ unmapped = tuple(sorted(
110
+ code for code, notes in targets.items()
111
+ if code in invalid_accounts or not notes
112
+ ))
113
+ ambiguous = tuple(sorted(
114
+ code for code, notes in targets.items()
115
+ if code not in invalid_accounts and len(notes) > 1
116
+ ))
117
return MappingCoverage(
118
year_id=year_id, total_accounts=len(current_accounts),
119
mapped_accounts=mapped, unmapped_accounts=unmapped,
120
ambiguous_accounts=ambiguous, orphan_rule_keys=tuple(sorted(orphan_keys)),
121
+ invalid_target_rules=tuple(sorted(invalid)),
122
)