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

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

Synced Dec 3, 20:45 UTC

test(integration): assemble Mapping core and XLSX modules for combined CI

davoodmehraban89 committed Oct 30, 2026 at 17:34 UTC a2907775be5241ee0e722bf0635532840eb69c25
1 file changed +65
backend/src/aram_backend/mapping_service.py new
+65
@@ -0,0 +1,65 @@
1 +"""Governed, year-isolated mapping persistence; never infer financial targets."""
2 +from __future__ import annotations
3 +
4 +from sqlalchemy import select
5 +from sqlalchemy.orm import Session
6 +
7 +from aram_backend.models import AccountVersion, FiscalYear, FiscalYearStatus, MappingRule, NoteDefinition
8 +
9 +
10 +class MappingValidationError(ValueError):
11 + """Mapping would violate a fiscal-year or explicit-review boundary."""
12 +
13 +
14 +def save_mapping_rule(
15 + session: Session, *, year_id: int, rule_key: str, account_code: str,
16 + target_note_id: int, mapping_type: str, version: int = 1,
17 +) -> MappingRule:
18 + """Persist an explicit mapping for a draft year, leaving commit to the caller.
19 +
20 + Repeated identical requests are idempotent; conflicting versions are rejected.
21 + No account, note, or mapping target is guessed from ledger data.
22 + """
23 + if isinstance(year_id, bool) or not isinstance(year_id, int) or year_id <= 0:
24 + raise MappingValidationError("year_id must be a positive integer")
25 + if isinstance(version, bool) or not isinstance(version, int) or version <= 0:
26 + raise MappingValidationError("version must be a positive integer")
27 + for label, value, limit in (
28 + ("rule_key", rule_key, 128), ("account_code", account_code, 64),
29 + ("mapping_type", mapping_type, 32),
30 + ):
31 + if not isinstance(value, str) or not value or value != value.strip() or len(value) > limit:
32 + raise MappingValidationError(f"{label} must be nonempty, trimmed and at most {limit} characters")
33 + if isinstance(target_note_id, bool) or not isinstance(target_note_id, int) or target_note_id <= 0:
34 + raise MappingValidationError("target_note_id must be a positive integer")
35 +
36 + year = session.get(FiscalYear, year_id)
37 + if year is None or year.status is not FiscalYearStatus.DRAFT:
38 + raise MappingValidationError("fiscal year must exist and be Draft")
39 + note = session.get(NoteDefinition, target_note_id)
40 + if note is None or note.year_id != year_id:
41 + raise MappingValidationError("target note must belong to the same fiscal year")
42 + account = session.scalar(select(AccountVersion.id).where(
43 + AccountVersion.year_id == year_id, AccountVersion.account_code == account_code,
44 + ).limit(1))
45 + if account is None:
46 + raise MappingValidationError("account code must exist in the same fiscal year")
47 +
48 + existing = session.scalar(select(MappingRule).where(
49 + MappingRule.year_id == year_id,
50 + MappingRule.rule_key == rule_key,
51 + MappingRule.version == version,
52 + ))
53 + if existing is not None:
54 + if (existing.account_code, existing.target_note_id, existing.mapping_type) != (
55 + account_code, target_note_id, mapping_type
56 + ):
57 + raise MappingValidationError("mapping version conflicts with an existing rule")
58 + return existing
59 +
60 + rule = MappingRule(year_id=year_id, rule_key=rule_key, version=version,
61 + account_code=account_code, target_note_id=target_note_id,
62 + mapping_type=mapping_type)
63 + session.add(rule)
64 + session.flush()
65 + return rule