feat(api): stage signer-scoped unreviewed Mapping proposal with atomic author evidence
davoodmehraban89 committed
Oct 30, 2026 at 23:05 UTC
ed9703da28d04bef7d09692a974322987280f401
1 file changed
+66
-1
backend/src/aram_backend/main.py
+66
-1
@@ -1,8 +1,14 @@
1
-from fastapi import Depends, FastAPI
1
+from fastapi import Depends, FastAPI, HTTPException
2
+from pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr
3
from aram_backend.authenticated_identity import VerifiedPrincipal, require_verified_principal
4
from sqlalchemy.orm import Session
5
from aram_backend.database import get_session
6
from aram_backend.mapping_year_authorization import require_year_access
7
+from aram_backend.mapping_batch_review import ProposedMappingRow, preview_mapping_batch
8
+from aram_backend.authored_mapping_draft import (
9
+ MappingAuthorConflict, stage_authored_unreviewed_batch,
10
+)
11
+from aram_backend.mapping_service import MappingValidationError
12
13
from aram_backend import __version__
14
from aram_backend.config import get_settings
@@ -47,6 +53,65 @@ def authenticated_year_access(
53
}
54
55
56
+class MappingStagingRow(BaseModel):
57
+ model_config = ConfigDict(extra="forbid")
58
+ source_locator: StrictStr
59
+ rule_key: StrictStr
60
+ account_code: StrictStr
61
+ target_note_id: StrictInt
62
+ mapping_type: StrictStr
63
+ expected_base_version: StrictInt
64
+
65
+
66
+class MappingStagingRequest(BaseModel):
67
+ model_config = ConfigDict(extra="forbid")
68
+ year_id: StrictInt = Field(gt=0)
69
+ request_key: StrictStr
70
+ rows: list[MappingStagingRow] = Field(min_length=1, max_length=10000)
71
+
72
+
73
+@app.post("/api/v1/mapping/unreviewed-drafts", status_code=201, tags=["mapping"])
74
+def stage_unreviewed_mapping_proposal(
75
+ request: MappingStagingRequest,
76
+ principal: VerifiedPrincipal = Depends(require_verified_principal),
77
+ session: Session = Depends(get_session),
78
+) -> dict[str, object]:
79
+ """Verified signer+year-scoped proposal staging; NEVER a financial apply.
80
+
81
+ All three records (draft, system staging event, signed author evidence)
82
+ are committed together or rolled back together. Request-supplied actor
83
+ identity is forbidden by the strict body model.
84
+ """
85
+ try:
86
+ with session.begin():
87
+ # Security order: check company/year scope BEFORE even classifying
88
+ # user-supplied Mapping data, to avoid cross-entity information leaks.
89
+ require_year_access(
90
+ session, principal=principal, year_id=request.year_id,
91
+ required_role="mapping_proposer",
92
+ )
93
+ snapshot = preview_mapping_batch(
94
+ session, year_id=request.year_id,
95
+ rows=tuple(
96
+ ProposedMappingRow(**row.model_dump()) for row in request.rows
97
+ ),
98
+ )
99
+ saved = stage_authored_unreviewed_batch(
100
+ session, snapshot=snapshot, request_key=request.request_key,
101
+ principal=principal,
102
+ )
103
+ return {
104
+ "draft_id": saved.id, "year_id": saved.year_id,
105
+ "state": saved.state, "request_key": saved.request_key,
106
+ }
107
+ except MappingAuthorConflict as exc:
108
+ raise HTTPException(
109
+ status_code=409, detail="Mapping proposal request identity conflict"
110
+ ) from exc
111
+ except MappingValidationError as exc:
112
+ raise HTTPException(status_code=422, detail="Invalid Mapping proposal") from exc
113
+
114
+
115
@app.get("/api/v1/health", response_model=HealthResponse, tags=["system"])
116
def health() -> HealthResponse:
117
return HealthResponse(status="ok", service=settings.app_name, version=__version__)