@davoodmehraban89 / Aram-1 / commits / 531cb38
Fork 0

feat(mapping): read-only unmapped, ambiguous and orphan account queue

davoodmehraban89 committed Oct 23, 2026 at 02:46 UTC 531cb3888e0966cc886ef54646aeafb6ff9522d4
1 file changed +71
backend/src/aram_backend/mapping_coverage.py new
+71
@@ -0,0 +1,71 @@
1 +"""Read-only fiscal-year mapping coverage and exception queue.
2 +
3 +No financial amounts, target guesses, or persistence operations are performed.
4 +"""
5 +from __future__ import annotations
6 +
7 +from dataclasses import dataclass
8 +
9 +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
14 +
15 +
16 +@dataclass(frozen=True)
17 +class MappingCoverage:
18 + year_id: int
19 + total_accounts: int
20 + mapped_accounts: tuple[str, ...]
21 + unmapped_accounts: tuple[str, ...]
22 + ambiguous_accounts: tuple[str, ...]
23 + orphan_rule_keys: tuple[str, ...]
24 +
25 +
26 +def inspect_mapping_coverage(session: Session, *, year_id: int) -> MappingCoverage:
27 + """Return deterministic account queues for a single year without modifying it.
28 +
29 + Only latest version per account and per mapping rule key is considered.
30 + Multiple current rules pointing one account to distinct notes are ambiguous,
31 + not silently assigned to a note.
32 + """
33 + if type(year_id) is not int or year_id <= 0:
34 + raise MappingValidationError("year_id must be a positive integer")
35 + if session.get(FiscalYear, year_id) is None:
36 + raise MappingValidationError("fiscal year does not exist")
37 +
38 + accounts = session.scalars(
39 + select(AccountVersion)
40 + .where(AccountVersion.year_id == year_id)
41 + .order_by(AccountVersion.account_code, AccountVersion.version.desc(), AccountVersion.id.desc())
42 + ).all()
43 + current_accounts: dict[str, AccountVersion] = {}
44 + for account in accounts:
45 + current_accounts.setdefault(account.account_code, account)
46 +
47 + rules = session.scalars(
48 + select(MappingRule)
49 + .where(MappingRule.year_id == year_id)
50 + .order_by(MappingRule.rule_key, MappingRule.version.desc(), MappingRule.id.desc())
51 + ).all()
52 + current_rules: dict[str, MappingRule] = {}
53 + for rule in rules:
54 + current_rules.setdefault(rule.rule_key, rule)
55 +
56 + targets: dict[str, set[int]] = {code: set() for code in current_accounts}
57 + orphan_keys: list[str] = []
58 + for key, rule in current_rules.items():
59 + if rule.account_code not in targets:
60 + orphan_keys.append(key)
61 + else:
62 + targets[rule.account_code].add(rule.target_note_id)
63 +
64 + mapped = tuple(sorted(code for code, notes in targets.items() if len(notes) == 1))
65 + unmapped = tuple(sorted(code for code, notes in targets.items() if not notes))
66 + ambiguous = tuple(sorted(code for code, notes in targets.items() if len(notes) > 1))
67 + return MappingCoverage(
68 + year_id=year_id, total_accounts=len(current_accounts),
69 + mapped_accounts=mapped, unmapped_accounts=unmapped,
70 + ambiguous_accounts=ambiguous, orphan_rule_keys=tuple(sorted(orphan_keys)),
71 + )