T5 reinterpretat: nu import DBF, ci editor web al maparii operatie ROAAUTO -> cod RAR, cu fuzzy lookup si validare de catre utilizator. - Contract hibrid: item prestatie accepta cod_prestatie (RAR direct, back-compat) SAU cod_op_service+denumire (mapat de gateway prin operations_mapping). - Ingestie: op intern necunoscut -> submission needs_mapping (nu pleaca la RAR); codul rezolvat se scrie inapoi in payload_json -> payload builder + worker neatinse. - Editor HTMX (_mapari.html + GET /_fragments/mapari, POST /mapari): listeaza op-urile nemapate, fuzzy preselecteaza codul RAR, save -> re-rezolvare automata (queued / needs_data). - Fuzzy: rapidfuzz.token_sort_ratio pe denumire normalizata (fara diacritice). - Nomenclator: seed fallback 18 coduri la boot (offline) + refresh live din worker. - Cont default id=1 cat timp auth API-key (CORE) nu exista (account_id NULL). - Endpointuri API: GET /v1/mapari/pending, POST /v1/mapari (respinge cod inexistent). - 15 teste noi (tests/test_mapping.py); 69 pass total. - Contract actualizat (docs/api-rar-contract.md), rapidfuzz==3.14.5 in requirements. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
"""Cheie de idempotenta = hash de continut canonic.
|
|
|
|
RAR nu are camp nr. comanda si accepta duplicate -> dedup-ul e in sarcina noastra
|
|
(plan.md sect. 14). Hash stabil peste o reprezentare canonica a prezentarii.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from typing import Any
|
|
|
|
|
|
def _op_identity(p: Any) -> str:
|
|
"""Cod RAR (normalizat) daca exista, altfel codul intern ROAAUTO."""
|
|
get = p.get if isinstance(p, dict) else (lambda k, d=None: getattr(p, k, d))
|
|
cod = (get("cod_prestatie", "") or "").strip().upper()
|
|
if cod:
|
|
return cod
|
|
return (get("cod_op_service", "") or "").strip()
|
|
|
|
|
|
def idempotency_key(account_id: int | None, prezentare: dict[str, Any]) -> str:
|
|
"""SHA-256 peste (account_id + campurile semnificative ale prezentarii).
|
|
|
|
Exclude obs si b64Image (cosmetice, nu definesc unicitatea declaratiei).
|
|
"""
|
|
canonic = {
|
|
"account_id": account_id,
|
|
"vin": (prezentare.get("vin") or "").strip().upper(),
|
|
"nr_inmatriculare": (prezentare.get("nr_inmatriculare") or "").strip().upper(),
|
|
"data_prestatie": prezentare.get("data_prestatie"),
|
|
"odometru_final": str(prezentare.get("odometru_final") or "").strip(),
|
|
# Identitatea operatiei = codul RAR daca exista, altfel codul intern ROAAUTO
|
|
# (hibrid): doua trimiteri ale aceleiasi comenzi dedup corect indiferent de
|
|
# forma in care vin codurile.
|
|
"prestatii": sorted(_op_identity(p) for p in (prezentare.get("prestatii") or [])),
|
|
}
|
|
blob = json.dumps(canonic, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
|
|
return hashlib.sha256(blob.encode("utf-8")).hexdigest()
|