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

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

Synced Dec 3, 19:30 UTC

feat(mapping): stage draft and insert-only system event in same caller transaction

davoodmehraban89 committed Oct 30, 2026 at 19:50 UTC eddfbf7c3807e004054b69c3bfdb0f9cd65084de
1 file changed +14
backend/src/aram_backend/mapping_draft_store.py
+14
@@ -102,6 +102,9 @@ def _read_row(session: Session, *, draft_id: int, year_id: int):
102 not isinstance(row["payload_json"], dict) or
103 _digest(row["payload_json"]) != row["payload_hash"]):
104 raise MappingValidationError("stored unreviewed Mapping draft corrupted")
105 + # Import locally: the audit schema refers to this module's own Core Table.
106 + from aram_backend.mapping_draft_audit import verify_draft_staging_event
107 + verify_draft_staging_event(session, draft_id=draft_id, year_id=year_id)
108 return row
109
110
@@ -134,6 +137,12 @@ def save_unreviewed_batch(
137 raise MappingValidationError("stored idempotency request key is corrupted")
138 if checksum != existing["payload_hash"] or payload != existing["payload_json"]:
139 raise MappingValidationError("idempotency request key already used for another proposal")
140 + # Existing request must have its original system event; never
141 + # append a second event on an idempotent replay.
142 + from aram_backend.mapping_draft_audit import verify_draft_staging_event
143 + verify_draft_staging_event(
144 + session, draft_id=existing["id"], year_id=snapshot.year_id,
145 + )
146 return _from_row(existing)
147 result = session.execute(
148 insert(mapping_review_drafts).values(
@@ -145,6 +154,11 @@ def save_unreviewed_batch(
154 # identical keys on independent DB connections. On IntegrityError the
155 # caller must rollback; do not recover by unguarded replay.
156 draft_id = result.inserted_primary_key[0]
157 + from aram_backend.mapping_draft_audit import record_draft_staging_event
158 + record_draft_staging_event(
159 + session, draft_id=draft_id, year_id=snapshot.year_id,
160 + request_key=request_key, payload_hash=checksum,
161 + )
162 row = _read_row(session, draft_id=draft_id, year_id=snapshot.year_id)
163 return _from_row(row)
164