"""Reference Data Registry (Missão 5, Fase 1): the NEW admin-approved ingestion workflow, separate from central/app/datasets/service.py's existing auto-activate methods (untouched, still used by the 9 pre-existing dataset kinds). A dataset registered here lands as DRAFT and only becomes VIGENTE (superseding the previous VIGENTE of the same kind) after an explicit ADMIN approval of the diff - "Nenhuma versão de dataset vira 'vigente' sem aprovação ADMIN do diff" (mission rule). """ from __future__ import annotations from datetime import datetime, timezone from sqlalchemy import select, update from .models import Dataset STATUSES = ("DRAFT", "APPROVED", "VIGENTE", "OBSOLETO", "REJECTED") class RegistryError(Exception): pass def register_draft(session, *, kind: str, provider: str, source_url: str, sha256: str, version_label: str, act: str = "", valid_from: str | None = None, valid_to: str | None = None, record_count: int = 0, parse_errors: dict | None = None, licence_terms: str | None = None, raw_file_path: str | None = None, file_size_bytes: int | None = None, submitted_by: str | None = None) -> Dataset: """Registers a new dataset submission as DRAFT. If this exact (provider, sha256) already exists under ANY status, returns it unchanged (idempotent re-submission of an unchanged official file - e.g. re-downloading the same official NCM table produces byte-identical content) instead of creating a duplicate row (the (provider, sha256) constraint would refuse it anyway).""" existing = session.scalar(select(Dataset).where(Dataset.provider == provider, Dataset.sha256 == sha256)) if existing: return existing dataset = Dataset(kind=kind, provider=provider, sha256=sha256, version_label=version_label, act=act, source_url=source_url, downloaded_at=datetime.now(timezone.utc), status="DRAFT", record_count=record_count, licence_terms=licence_terms, valid_from=valid_from, valid_to=valid_to, file_size_bytes=file_size_bytes, raw_file_path=raw_file_path, parse_errors=parse_errors or {}, submitted_by=submitted_by) session.add(dataset) session.commit() return dataset def diff_against_vigente(session, dataset: Dataset) -> dict: """A summary an ADMIN reviews before approving: what the current VIGENTE of the same kind looks like vs this DRAFT - never the row-by-row content (that lives in the kind-specific entry tables), just the registry-level facts that matter for an approval decision.""" current = session.scalar(select(Dataset).where(Dataset.kind == dataset.kind, Dataset.status == "VIGENTE")) return { "kind": dataset.kind, "draft": {"id": dataset.id, "version_label": dataset.version_label, "sha256": dataset.sha256, "record_count": dataset.record_count, "valid_from": dataset.valid_from, "valid_to": dataset.valid_to, "parse_errors": dataset.parse_errors}, "current_vigente": None if current is None else {"id": current.id, "version_label": current.version_label, "sha256": current.sha256, "record_count": current.record_count, "valid_from": current.valid_from, "valid_to": current.valid_to}, "record_count_delta": None if current is None else dataset.record_count - current.record_count, "is_first_version": current is None, "sha256_unchanged": current is not None and current.sha256 == dataset.sha256, } def approve_draft(session, dataset_id: int, *, admin_name: str) -> Dataset: """DRAFT -> VIGENTE, superseding the previous VIGENTE of the same kind (OBSOLETO, never deleted). Refuses a dataset that isn't DRAFT, and refuses a dataset with recorded parse errors (must be re-submitted clean - an ADMIN cannot approve past a known parsing problem).""" dataset = session.get(Dataset, dataset_id) if dataset is None: raise RegistryError("dataset not found") if dataset.status != "DRAFT": raise RegistryError(f"only a DRAFT dataset can be approved (current status: {dataset.status})") if dataset.parse_errors: raise RegistryError("dataset has recorded parse errors - re-submit a clean version") session.execute(update(Dataset).where(Dataset.kind == dataset.kind, Dataset.status == "VIGENTE").values(status="OBSOLETO")) dataset.status = "VIGENTE" dataset.approved_by = admin_name dataset.approved_at = datetime.now(timezone.utc) session.commit() return dataset def reject_draft(session, dataset_id: int, *, admin_name: str) -> Dataset: dataset = session.get(Dataset, dataset_id) if dataset is None: raise RegistryError("dataset not found") if dataset.status != "DRAFT": raise RegistryError(f"only a DRAFT dataset can be rejected (current status: {dataset.status})") dataset.status = "REJECTED" dataset.approved_by = admin_name dataset.approved_at = datetime.now(timezone.utc) session.commit() return dataset def list_registry(session) -> list[Dataset]: return list(session.scalars(select(Dataset).where(Dataset.status.in_(STATUSES)).order_by(Dataset.kind, Dataset.downloaded_at.desc())))