feat(5.7): raspuns API onest la blocaje + mapare inline din detaliu
Raportat din client VFP: POST /v1/prezentari raspundea submission_id+status
fara motiv pe randuri blocate (erori se popula doar pe on_unmapped_error=True),
deci un needs_data/needs_mapping parea succes.
API (aditiv): SubmissionResult += nemapate + motiv. create_prezentari
populeaza erori (validare continut, 3 niveluri) / nemapate (coduri fara
mapare, COD_NEMAPAT) / motiv (rezumat uman) pe TOATE caile non-queued —
enqueue, respins (on_unmapped_error=True) si reactivare dedup peste error,
prin helperele _rezultat_enqueue / _rezultat_respins / _motiv_clasificare.
on_unmapped_error=True pastreaza erori=COD_NEMAPAT (compat clienti vechi).
Web: mapare inline in panoul de detaliu trimitere — ruta
POST /trimitere/{id}/mapeaza (reuse save_mapping + reresolve_account, scoped
sesiune + CSRF, re-rezolva pe batch_id-ul randului), helper
_nemapate_pentru_submission + context in _detaliu_ctx, sectiune in
_trimitere_detaliu.html (selector cod RAR cu sugestie fuzzy preselectata).
Apare doar pe operatii nemapate reale (nu pe auto_send=0).
/code-review high: reparat raspuns neonest la reactivare + dublu
load_nomenclator in _detaliu_ctx.
Teste: pytest -q 765 passed. Backend trimitere (worker/masina stari/
idempotenta) si schema NEATINSE. PRD: docs/prd/prd-5.7-*.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -77,13 +77,61 @@ def _classify_modal(content, mapping, mapping_meta, valid_codes, error_mode) ->
|
||||
|
||||
|
||||
def _erori_nemapate(unmapped: list[dict]) -> list[dict]:
|
||||
"""Erori 3 niveluri (COD_NEMAPAT) pentru raspunsul on_unmapped_error=True."""
|
||||
"""Coduri nemapate imbogatite cu 3 niveluri (COD_NEMAPAT), pentru raspuns onest."""
|
||||
return [
|
||||
{**u, **err_eroare("COD_NEMAPAT", cauza=f"cod {u.get('cod_op_service')} necunoscut/fara mapare RAR")}
|
||||
for u in unmapped
|
||||
]
|
||||
|
||||
|
||||
def _motiv_clasificare(cl: dict) -> str | None:
|
||||
"""Rezumat uman pe o linie pentru un rezultat de clasificare (PRD 5.7).
|
||||
|
||||
None cand status='queued'. Acopera toate ramurile de blocaj: erori de continut
|
||||
(needs_data), coduri nemapate (needs_mapping) si auto_send oprit (needs_mapping).
|
||||
"""
|
||||
if cl["status"] == "queued":
|
||||
return None
|
||||
if cl["errors"]:
|
||||
return "; ".join(
|
||||
(e.get("problema") or e.get("message") or "") for e in cl["errors"]
|
||||
).strip("; ") or "Date incomplete (respinse de RAR)."
|
||||
if cl["unmapped"]:
|
||||
coduri = ", ".join((u.get("cod_op_service") or "") for u in cl["unmapped"])
|
||||
return f"Coduri fara mapare RAR: {coduri}"
|
||||
if cl["status"] == "needs_mapping":
|
||||
return "Cod cu trimitere automata oprita; confirmare manuala inainte de trimitere."
|
||||
return None
|
||||
|
||||
|
||||
def _rezultat_enqueue(submission_id: int | None, cl: dict, **extra) -> SubmissionResult:
|
||||
"""SubmissionResult onest dintr-un rezultat de clasificare (PRD 5.7).
|
||||
|
||||
Populeaza erori (validare continut), nemapate (coduri fara mapare) si motiv (uman)
|
||||
pentru orice status != 'queued'. Aditiv: pe 'queued' toate raman goale/None.
|
||||
"""
|
||||
return SubmissionResult(
|
||||
submission_id=submission_id,
|
||||
status=cl["status"],
|
||||
erori=list(cl["errors"]),
|
||||
nemapate=_erori_nemapate(cl["unmapped"]),
|
||||
motiv=_motiv_clasificare(cl),
|
||||
**extra,
|
||||
)
|
||||
|
||||
|
||||
def _rezultat_respins(submission_id: int | None, cl: dict) -> SubmissionResult:
|
||||
"""Rezultat pentru on_unmapped_error=True: status='error', fara enqueue/reactivare.
|
||||
|
||||
`erori` pastreaza COD_NEMAPAT (compat clienti vechi); `nemapate` + `motiv` adaugate.
|
||||
"""
|
||||
nem = _erori_nemapate(cl["unmapped"])
|
||||
return SubmissionResult(
|
||||
submission_id=submission_id, status="error",
|
||||
erori=nem, nemapate=nem, motiv=_motiv_clasificare(cl),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/prezentari", response_model=PrezentariResponse)
|
||||
def create_prezentari(
|
||||
req: PrezentareRequest,
|
||||
@@ -142,10 +190,7 @@ def create_prezentari(
|
||||
cl = _classify_modal(content, mapping, mapping_meta, valid_codes, error_mode)
|
||||
if cl["blocked_error"]:
|
||||
# on_unmapped_error=True: nu reactivam; randul ramane 'error'.
|
||||
results.append(SubmissionResult(
|
||||
submission_id=existing["id"], status="error",
|
||||
erori=_erori_nemapate(cl["unmapped"]),
|
||||
))
|
||||
results.append(_rezultat_respins(existing["id"], cl))
|
||||
continue
|
||||
cur = conn.execute(
|
||||
"UPDATE submissions SET status=?, payload_json=?, rar_error=?, "
|
||||
@@ -163,9 +208,9 @@ def create_prezentari(
|
||||
"UPDATE accounts SET rar_creds_enc=? WHERE id=?",
|
||||
(encrypt_creds(req.rar_credentials.model_dump()), acct),
|
||||
)
|
||||
results.append(SubmissionResult(
|
||||
submission_id=existing["id"], status=cl["status"], reactivated=True,
|
||||
))
|
||||
# Raspuns onest si la reactivare (PRD 5.7): daca re-clasificarea
|
||||
# cade pe needs_data/needs_mapping, expune motivul (nu doar status).
|
||||
results.append(_rezultat_enqueue(existing["id"], cl, reactivated=True))
|
||||
continue
|
||||
# Cursa: alt POST/requeue a schimbat starea intre SELECT si UPDATE
|
||||
# (rowcount==0) -> raspuns dedup pe starea CURENTA.
|
||||
@@ -188,16 +233,15 @@ def create_prezentari(
|
||||
cl = _classify_modal(content, mapping, mapping_meta, valid_codes, error_mode)
|
||||
if cl["blocked_error"]:
|
||||
# on_unmapped_error=True: respinge fara enqueue (cod necunoscut/nemapat).
|
||||
results.append(SubmissionResult(
|
||||
submission_id=None, status="error", erori=_erori_nemapate(cl["unmapped"]),
|
||||
))
|
||||
results.append(_rezultat_respins(None, cl))
|
||||
continue
|
||||
cur = conn.execute(
|
||||
"INSERT INTO submissions (idempotency_key, account_id, status, payload_json, rar_error, rar_creds_enc) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||
(key, acct, cl["status"], json.dumps(cl["content"], ensure_ascii=False), cl["rar_error"], creds_enc),
|
||||
)
|
||||
results.append(SubmissionResult(submission_id=int(cur.lastrowid), status=cl["status"]))
|
||||
# Raspuns onest (PRD 5.7): pe needs_data/needs_mapping expune erori/nemapate/motiv.
|
||||
results.append(_rezultat_enqueue(int(cur.lastrowid), cl))
|
||||
|
||||
# US-004: audit cerere API per cont. Doar metadate (count + distributie status),
|
||||
# NICIUN camp de payload PII integral. Reuse conn (T4 — fara contentie WAL).
|
||||
|
||||
@@ -106,9 +106,15 @@ class SubmissionResult(BaseModel):
|
||||
# cheie de continut a fost RE-ACTIVAT (re-clasificat + creds actualizate) la resubmit.
|
||||
# `deduped` pastreaza semantica actuala (clientii vechi care testeaza `deduped` nu se sparg).
|
||||
reactivated: bool = False
|
||||
# Populat cand status='error' din cauza on_unmapped='error': erori 3 niveluri
|
||||
# (COD_NEMAPAT) pentru fiecare cod necunoscut/nemapat. Gol altfel.
|
||||
# Raspuns ONEST pentru randuri blocate (PRD 5.7): orice status != 'queued' isi
|
||||
# expune motivul, ca integratorul sa nu trateze un needs_data/needs_mapping drept succes.
|
||||
# erori = validare de continut (needs_data), 3 niveluri [{field, cod, problema, cauza, fix, message}].
|
||||
# Pe ramura on_unmapped_error='error' pastreaza COD_NEMAPAT (compat).
|
||||
# nemapate = coduri fara mapare RAR (needs_mapping / respins), 3 niveluri + cod_op_service/denumire.
|
||||
# motiv = rezumat uman pe o linie (None cand status='queued').
|
||||
erori: list[dict] = []
|
||||
nemapate: list[dict] = []
|
||||
motiv: str | None = None
|
||||
|
||||
|
||||
class PrezentariResponse(BaseModel):
|
||||
|
||||
@@ -771,10 +771,57 @@ def _payload_form_values(payload_json) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _nemapate_pentru_submission(row, nomenclator: list[dict]) -> list[dict]:
|
||||
"""Operatiile nemapate ale UNUI submission needs_mapping, cu sugestii fuzzy (PRD 5.7).
|
||||
|
||||
Echivalentul `pending_unmapped` restrans la un singur rand: parseaza payload_json,
|
||||
aduna prestatiile fara cod_prestatie (cu cod_op_service) si ataseaza sugestii din
|
||||
`nomenclator` (pasat de apelant — evita un SELECT redundant in _detaliu_ctx). Goala
|
||||
daca randul nu e needs_mapping sau nu are operatii nemapate reale (ex. needs_mapping
|
||||
din auto_send=0 — codul exista deja, doar trimiterea e oprita).
|
||||
"""
|
||||
if row["status"] != "needs_mapping":
|
||||
return []
|
||||
try:
|
||||
content = json.loads(row["payload_json"]) if row["payload_json"] else {}
|
||||
if not isinstance(content, dict):
|
||||
content = {}
|
||||
except (ValueError, TypeError):
|
||||
content = {}
|
||||
seen: set[str] = set()
|
||||
out: list[dict] = []
|
||||
for item in content.get("prestatii") or []:
|
||||
if not isinstance(item, dict) or (item.get("cod_prestatie") or ""):
|
||||
continue
|
||||
op = (item.get("cod_op_service") or "").strip()
|
||||
if not op or op in seen:
|
||||
continue
|
||||
seen.add(op)
|
||||
out.append({
|
||||
"cod_op_service": op,
|
||||
"denumire": item.get("denumire"),
|
||||
"suggestions": suggest_codes(item.get("denumire"), nomenclator, limit=5),
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def _detaliu_ctx(request: Request, row, *, message: str | None = None,
|
||||
error: bool = False, corectie_errors: list | None = None) -> dict:
|
||||
"""Context pentru _trimitere_detaliu.html dintr-un rand de submission."""
|
||||
error: bool = False, corectie_errors: list | None = None,
|
||||
conn=None, account_id: int | None = None) -> dict:
|
||||
"""Context pentru _trimitere_detaliu.html dintr-un rand de submission.
|
||||
|
||||
`conn`+`account_id` (optional): cand sunt date si randul e needs_mapping, expune
|
||||
`nemapate_inline` + `nomenclator` pentru maparea inline din panou (PRD 5.7).
|
||||
"""
|
||||
eticheta = eticheta_stare(row["status"])
|
||||
nemapate_inline: list[dict] = []
|
||||
nomenclator: list[dict] = []
|
||||
if conn is not None and row["status"] == "needs_mapping":
|
||||
# Un singur SELECT pe nomenclator: il refolosim si pentru sugestii si pentru dropdown.
|
||||
nomenclator = load_nomenclator(conn)
|
||||
nemapate_inline = _nemapate_pentru_submission(row, nomenclator)
|
||||
if not nemapate_inline:
|
||||
nomenclator = [] # nu expunem dropdown-ul cand nu exista operatii de mapat
|
||||
ctx = {
|
||||
"request": request,
|
||||
"csrf_token": get_csrf_token(request),
|
||||
@@ -797,6 +844,9 @@ def _detaliu_ctx(request: Request, row, *, message: str | None = None,
|
||||
"editabil": row["status"] in _CORECTABILE,
|
||||
# US-011: error/needs_data/needs_mapping pot fi sterse / re-puse in coada
|
||||
"gestionabil": row["status"] in _GESTIONABILE_WEB,
|
||||
# PRD 5.7: mapare inline (operatii nemapate ale acestui rand + nomenclator)
|
||||
"nemapate_inline": nemapate_inline,
|
||||
"nomenclator": nomenclator,
|
||||
"corectie_msg": message,
|
||||
"corectie_error": error,
|
||||
"corectie_errors": corectie_errors or [],
|
||||
@@ -829,7 +879,64 @@ def fragment_trimitere_detaliu(request: Request, submission_id: int) -> HTMLResp
|
||||
row = _fetch_submission_scoped(conn, account_id, submission_id)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="trimitere inexistenta")
|
||||
return templates.TemplateResponse("_trimitere_detaliu.html", _detaliu_ctx(request, row))
|
||||
return templates.TemplateResponse(
|
||||
"_trimitere_detaliu.html",
|
||||
_detaliu_ctx(request, row, conn=conn, account_id=account_id),
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
@router.post("/trimitere/{submission_id}/mapeaza", response_class=HTMLResponse)
|
||||
async def post_mapeaza_inline(request: Request, submission_id: int) -> HTMLResponse:
|
||||
"""Mapare inline din panoul de detaliu (PRD 5.7): alege cod RAR pentru o operatie nemapata.
|
||||
|
||||
Reutilizeaza EXACT save_mapping + reresolve_account (ca tab-ul Mapari) — fara logica
|
||||
noua de clasificare. Re-rezolva scoped pe batch-ul randului (canal API batch_id IS NULL
|
||||
SAU import batch), deblocand si randurile-frate cu aceeasi operatie. Scoped pe sesiune
|
||||
(404 cross-account/inexistent), CSRF obligatoriu, gard pe status needs_mapping.
|
||||
"""
|
||||
account_id = require_login(request)
|
||||
form = await request.form()
|
||||
verify_csrf(request, str(form.get("csrf_token") or ""))
|
||||
cod_op_service = str(form.get("cod_op_service") or "").strip()
|
||||
cod_prestatie = str(form.get("cod_prestatie") or "").strip().upper()
|
||||
auto_send = str(form.get("auto_send") or "") not in ("", "false", "0", "off")
|
||||
conn = get_connection()
|
||||
try:
|
||||
row = _fetch_submission_scoped(conn, account_id, submission_id)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="trimitere inexistenta")
|
||||
if row["status"] != "needs_mapping":
|
||||
raise HTTPException(status_code=403, detail="trimitere fara operatii de mapat")
|
||||
if not cod_op_service or not cod_prestatie:
|
||||
return templates.TemplateResponse(
|
||||
"_trimitere_detaliu.html",
|
||||
_detaliu_ctx(request, row, conn=conn, account_id=account_id, error=True,
|
||||
message="Alege un cod RAR pentru operatie."),
|
||||
)
|
||||
exists = conn.execute(
|
||||
"SELECT 1 FROM nomenclator_rar WHERE cod_prestatie=?", (cod_prestatie,)
|
||||
).fetchone()
|
||||
if not exists:
|
||||
return templates.TemplateResponse(
|
||||
"_trimitere_detaliu.html",
|
||||
_detaliu_ctx(request, row, conn=conn, account_id=account_id, error=True,
|
||||
message=f"Cod necunoscut in nomenclator: {cod_prestatie}."),
|
||||
)
|
||||
save_mapping(conn, account_id, cod_op_service, cod_prestatie, auto_send)
|
||||
# Re-rezolva scoped pe canalul randului: batch_id None (API) sau batch import.
|
||||
reresolve_account(conn, account_id, batch_id=row["batch_id"])
|
||||
row2 = _fetch_submission_scoped(conn, account_id, submission_id)
|
||||
eticheta = eticheta_stare(row2["status"])
|
||||
resp = templates.TemplateResponse(
|
||||
"_trimitere_detaliu.html",
|
||||
_detaliu_ctx(request, row2, conn=conn, account_id=account_id,
|
||||
message=f"Mapat {cod_op_service} -> {cod_prestatie}. "
|
||||
f"Stare noua: {eticheta[0]}."),
|
||||
)
|
||||
resp.headers["HX-Trigger"] = "trimiteriChanged"
|
||||
return resp
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@@ -895,8 +1002,8 @@ async def post_corectie_trimitere(request: Request, submission_id: int) -> HTMLR
|
||||
row2 = _fetch_submission_scoped(conn, account_id, submission_id)
|
||||
return templates.TemplateResponse(
|
||||
"_trimitere_detaliu.html",
|
||||
_detaliu_ctx(request, row2, error=True,
|
||||
message="Lipseste inca un cod RAR — rezolva operatia in tab-ul Mapari."),
|
||||
_detaliu_ctx(request, row2, conn=conn, account_id=account_id, error=True,
|
||||
message="Lipseste inca un cod RAR — alege-l mai jos sau in tab-ul Mapari."),
|
||||
)
|
||||
|
||||
if has_no_auto_send(resolved, mapping_meta):
|
||||
@@ -999,7 +1106,8 @@ async def post_repune_trimitere(request: Request, submission_id: int) -> HTMLRes
|
||||
row = _fetch_submission_scoped(conn, account_id, submission_id)
|
||||
resp = templates.TemplateResponse(
|
||||
"_trimitere_detaliu.html",
|
||||
_detaliu_ctx(request, row, message="Re-pus in coada — pleaca la urmatoarea trimitere."),
|
||||
_detaliu_ctx(request, row, conn=conn, account_id=account_id,
|
||||
message="Re-pus in coada — pleaca la urmatoarea trimitere."),
|
||||
)
|
||||
resp.headers["HX-Trigger"] = "trimiteriChanged"
|
||||
return resp
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{% from "_eroare.html" import card_erori %}
|
||||
{% import '_macros.html' as ui %}
|
||||
<div class="card" id="detaliu-card-{{ id }}" style="border-color:var(--accent);">
|
||||
<div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap; margin:0 0 12px;">
|
||||
<h2 style="font-size:15px; margin:0;">Detaliu trimitere #{{ id }}</h2>
|
||||
@@ -65,6 +66,53 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{# === Mapare inline (PRD 5.7): alege cod RAR pentru operatiile nemapate ale acestui rand === #}
|
||||
{% if nemapate_inline %}
|
||||
<div style="margin-top:14px; padding-top:12px; border-top:1px solid var(--line);">
|
||||
<h3 style="font-size:14px; margin:0 0 4px;">Mapeaza codul operatiei</h3>
|
||||
<p class="muted" style="margin:0 0 10px; font-size:13px;">
|
||||
Alege codul RAR pentru fiecare operatie. La salvare, randul se re-rezolva pe loc
|
||||
(si celelalte randuri cu aceeasi operatie).
|
||||
</p>
|
||||
{% for op in nemapate_inline %}
|
||||
{% set top = op.suggestions[0] if op.suggestions else None %}
|
||||
{% set preselect = top.cod_prestatie if (top and top.score >= 60) else '' %}
|
||||
<form hx-post="/trimitere/{{ id }}/mapeaza" hx-target="#trimitere-detaliu" hx-swap="innerHTML"
|
||||
style="margin:0 0 12px; padding:10px; border:1px solid var(--line); border-radius:8px;">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
|
||||
<input type="hidden" name="cod_op_service" value="{{ op.cod_op_service }}">
|
||||
<div style="margin-bottom:6px;">
|
||||
<strong>{{ op.cod_op_service }}</strong>
|
||||
{% if op.denumire and op.denumire != op.cod_op_service %}
|
||||
<span class="muted">— {{ op.denumire }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if op.suggestions %}
|
||||
<div class="muted" style="font-size:12px; margin-bottom:6px;">
|
||||
Sugestii:
|
||||
{% for s in op.suggestions[:3] %}
|
||||
<span class="sugg">{{ s.cod_prestatie }} ({{ s.score|round|int }}%)</span>{% if not loop.last %}, {% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
<div style="display:flex; gap:10px; flex-wrap:wrap; align-items:center;">
|
||||
<select name="cod_prestatie" required aria-label="Cod RAR pentru {{ op.cod_op_service }}"
|
||||
style="flex:1; min-width:220px; max-width:380px;">
|
||||
<option value="">— alege cod RAR —</option>
|
||||
{% for n in nomenclator %}
|
||||
<option value="{{ n.cod_prestatie }}" {% if n.cod_prestatie == preselect %}selected{% endif %}>
|
||||
{{ n.cod_prestatie }} — {{ n.nume_prestatie }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
{{ ui.autosend_toggle(checked=True) }}
|
||||
<button type="submit">Salveaza maparea</button>
|
||||
</div>
|
||||
</form>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{# === Corectie inline (US-010): doar randuri ne-trimise blocate === #}
|
||||
{% if editabil %}
|
||||
{% set err_map = {} %}
|
||||
|
||||
Reference in New Issue
Block a user