From be9b528c5f58ad581bd8d2747c6fb008334298d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Trevi=C3=B1o=20Cervantes?= Date: Mon, 26 Oct 2026 01:42:27 -0600 Subject: [PATCH] integrating latest dashboard changes --- Dockerfile | 8 ++++++-- docker-entrypoint.sh | 7 +++++++ tests/test_imports_endpoints.py | 22 +++++++++++++++++++++- webapi/imports/schemas/registry.py | 3 ++- webapi/imports/schemas/tea/pacientes.py | 6 +++++- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index fe37a36..4af4448 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Build context is the repo root (see docker-compose.yml) so the sibling -# packages Core/, Persistance/, Extraction/, models/ and indicadores_kit/ -# can be copied. +# packages Core/, Persistance/, Extraction/, models/, indicadores_kit/ and +# Orquestacion/ can be copied. FROM python:3.12-slim # Tesseract (with Spanish data) for scanned documents; pytesseract only wraps the binary. @@ -16,12 +16,16 @@ COPY Persistance/ Persistance/ COPY Extraction/ Extraction/ COPY models/ models/ COPY indicadores_kit/ indicadores_kit/ +COPY Orquestacion/ Orquestacion/ COPY WebApi/ WebApi/ # Migrations need the MySQL/PostgreSQL drivers when *_DATABASE_URL points there. RUN cd Persistance && uv sync --no-dev --extra drivers # Drivers for the SQL import sources (MySQL/MariaDB, PostgreSQL, SQL Server). RUN cd WebApi && uv sync --no-dev --extra sql-drivers +# The indicator pipeline (staging -> facts -> reports), run with +# `python -m orquestacion ciclo ...` after an import. +RUN cd Orquestacion && uv sync --no-dev --extra drivers ENV CACU_DATABASE_URL=sqlite:////data/cacu.db ENV CAMA_DATABASE_URL=sqlite:////data/cama.db diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 9b54773..df4ca83 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -3,6 +3,13 @@ set -e mkdir -p /data +# SQLite won't create a missing parent directory for its database file. +for url in "$CACU_DATABASE_URL" "$CAMA_DATABASE_URL" "$TEA_DATABASE_URL" "$GOVERNANCE_DATABASE_URL"; do + case "$url" in + sqlite:///*) mkdir -p "$(dirname "${url#sqlite:///}")" ;; + esac +done + # --no-sync: the image's environments were synced at build time; syncing at # startup would try to reach the package index (and pull dev deps). cd /app/Persistance diff --git a/tests/test_imports_endpoints.py b/tests/test_imports_endpoints.py index 89146d7..9e8e436 100644 --- a/tests/test_imports_endpoints.py +++ b/tests/test_imports_endpoints.py @@ -159,11 +159,31 @@ def test_commit_on_conflict_reject_then_upsert(client): def test_commit_upsert_rejected_for_target_without_natural_key(client): - source = _json_source([{"pac_id_hospital": "T-NOKEY-1"}], target="tea.Pacientes") + source = _json_source( + [{"psi_pac_id": 1}], target="tea.EvaluacionPsicologica", column_mapping={"psi_pac_id": "psi_pac_id"} + ) response = client.post("/imports/commit", json={"sources": [source], "on_conflict": "upsert"}) assert response.status_code == 400 +def test_tea_patient_keeps_the_source_id_and_reimport_updates_it(client): + mapping = {"pac_id": "pac_id", "pac_id_hospital": "pac_id_hospital", "pac_sexo": "pac_sexo"} + first = _json_source([{"pac_id": 9001, "pac_id_hospital": "T-SRC-1", "pac_sexo": "F"}], + target="tea.Pacientes", column_mapping=mapping) + body = client.post("/imports/commit", json={"sources": [first], "on_conflict": "upsert"}).get_json() + assert body["results"][0]["created"] == 1 + + again = _json_source([{"pac_id": 9001, "pac_id_hospital": "T-SRC-1", "pac_sexo": "M"}], + target="tea.Pacientes", column_mapping=mapping) + body = client.post("/imports/commit", json={"sources": [again], "on_conflict": "upsert"}).get_json() + assert body["results"][0]["created"] == 0 + assert body["results"][0]["updated"] == 1 + + paciente = client.get("/tea/pacientes/9001").get_json() + assert paciente["pac_id_hospital"] == "T-SRC-1" + assert paciente["pac_sexo"] == "M" + + @pytest.fixture def sql_source_db(tmp_path): db_path = tmp_path / "external_source.db" diff --git a/webapi/imports/schemas/registry.py b/webapi/imports/schemas/registry.py index 418e3d3..0e7948c 100644 --- a/webapi/imports/schemas/registry.py +++ b/webapi/imports/schemas/registry.py @@ -114,7 +114,8 @@ ENTITY_REGISTRY: dict[str, EntitySpec] = { schema=PacientesImportSchema, model=TeaPacientes, repository=tea_pacientes_repository, - conflict_fields=None, + # The source's own id; TEA's child tables reference it directly. + conflict_fields=("pac_id",), ), "cacu.OrdenesEstudio": EntitySpec( schema=CacuOrdenesEstudioImportSchema, diff --git a/webapi/imports/schemas/tea/pacientes.py b/webapi/imports/schemas/tea/pacientes.py index f9670a3..5579079 100644 --- a/webapi/imports/schemas/tea/pacientes.py +++ b/webapi/imports/schemas/tea/pacientes.py @@ -1,5 +1,8 @@ """Import schema for `core.tea.models.Pacientes`. All fields optional, like TEA's -Core; system-managed `pac_created_at`/`pac_updated_at`/`pac_fuente_sistema` are excluded.""" +Core; system-managed `pac_created_at`/`pac_updated_at`/`pac_fuente_sistema` are excluded. + +`pac_id` is importable: every other TEA table references the patient by the +source system's integer id, so it has to be kept, not renumbered.""" from datetime import date from typing import Optional @@ -14,6 +17,7 @@ _PARENTESCO_CATALOG = Catalog(Parentesco) class PacientesImportSchema(ImportSchema): + pac_id: Optional[int] = None pac_id_hospital: Optional[str] = None pac_id_titular: Optional[str] = None pac_parentesco: CatalogValue(_PARENTESCO_CATALOG) = None