@davoodmehraban89 / Aram-2 Mirror · read-only / commits / f50784f
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 3, 20:45 UTC

test(integration): combine latest-note review module and tests

davoodmehraban89 committed Oct 30, 2026 at 17:35 UTC f50784fd29088ae6a5ff3059d61203fa2f60ad8b
1 file changed +93
backend/src/aram_backend/note_mapping_review.py new
+93
@@ -0,0 +1,93 @@
1 +"""Read-only note-level mapping review; no financial amounts or inferred targets."""
2 +from __future__ import annotations
3 +
4 +from dataclasses import dataclass
5 +
6 +from sqlalchemy import select
7 +from sqlalchemy.orm import Session
8 +
9 +from aram_backend.mapping_service import MappingValidationError
10 +from aram_backend.models import AccountVersion, FiscalYear, MappingRule, NoteDefinition
11 +
12 +
13 +@dataclass(frozen=True)
14 +class NoteMappingReview:
15 + note_id: int
16 + note_number: str
17 + note_title: str
18 + account_codes: tuple[str, ...]
19 + ambiguous_account_codes: tuple[str, ...]
20 +
21 +
22 +@dataclass(frozen=True)
23 +class NoteMappingOverview:
24 + year_id: int
25 + notes: tuple[NoteMappingReview, ...]
26 + unmapped_account_codes: tuple[str, ...]
27 + orphan_rule_keys: tuple[str, ...]
28 + stale_note_rule_keys: tuple[str, ...]
29 +
30 +
31 +def review_note_mappings(session: Session, *, year_id: int) -> NoteMappingOverview:
32 + """Report current note assignments; reject stale note versions rather than guessing.
33 +
34 + Current = greatest version per account, rule key and note number. If an
35 + account has multiple distinct current targets, show it as ambiguous.
36 + Queries deliberately avoid flushing pending changes.
37 + """
38 + if type(year_id) is not int or year_id <= 0:
39 + raise MappingValidationError("year_id must be a positive integer")
40 + with session.no_autoflush:
41 + if session.get(FiscalYear, year_id) is None:
42 + raise MappingValidationError("fiscal year does not exist")
43 +
44 + accounts = session.scalars(select(AccountVersion).where(
45 + AccountVersion.year_id == year_id
46 + ).order_by(AccountVersion.account_code, AccountVersion.version.desc(), AccountVersion.id.desc())).all()
47 + current_accounts = {a.account_code for a in accounts}
48 +
49 + notes = session.scalars(select(NoteDefinition).where(
50 + NoteDefinition.year_id == year_id
51 + ).order_by(NoteDefinition.note_number, NoteDefinition.version.desc(), NoteDefinition.id.desc())).all()
52 + current_notes: dict[str, NoteDefinition] = {}
53 + for note in notes:
54 + current_notes.setdefault(note.note_number, note)
55 + current_note_by_id = {n.id: n for n in current_notes.values()}
56 +
57 + rules = session.scalars(select(MappingRule).where(
58 + MappingRule.year_id == year_id
59 + ).order_by(MappingRule.rule_key, MappingRule.version.desc(), MappingRule.id.desc())).all()
60 + current_rules: dict[str, MappingRule] = {}
61 + for rule in rules:
62 + current_rules.setdefault(rule.rule_key, rule)
63 +
64 + account_targets: dict[str, set[int]] = {code: set() for code in current_accounts}
65 + orphan: list[str] = []
66 + stale: list[str] = []
67 + for key, rule in current_rules.items():
68 + if rule.account_code not in current_accounts:
69 + orphan.append(key)
70 + elif rule.target_note_id not in current_note_by_id:
71 + stale.append(key)
72 + else:
73 + account_targets[rule.account_code].add(rule.target_note_id)
74 +
75 + ambiguous = {code for code, targets in account_targets.items() if len(targets) > 1}
76 + reviews = tuple(NoteMappingReview(
77 + note_id=note.id, note_number=note.note_number, note_title=note.title,
78 + account_codes=tuple(sorted(
79 + code for code, targets in account_targets.items()
80 + if note.id in targets and code not in ambiguous
81 + )),
82 + ambiguous_account_codes=tuple(sorted(
83 + code for code in ambiguous if note.id in account_targets[code]
84 + )),
85 + ) for note in sorted(current_notes.values(), key=lambda n: (n.note_number, n.id)))
86 + return NoteMappingOverview(
87 + year_id=year_id, notes=reviews,
88 + unmapped_account_codes=tuple(sorted(
89 + code for code, targets in account_targets.items() if not targets
90 + )),
91 + orphan_rule_keys=tuple(sorted(orphan)),
92 + stale_note_rule_keys=tuple(sorted(stale)),
93 + )