Source code for cadrumo.adapters.persistence.profile.state_projection

"""Persistence adapter for the operator state-projection read contract.

Core types:
:class:`~cadrumo.adapters.persistence.profile.transactions.TransactionCatalogueRepository`.
"""

from __future__ import annotations

from ....application.diagnostics_ports import DiagnosticsPorts
from ....application.state_projection_ports import (
    StateProjectionProfileRead,
    StateProjectionReadError,
    StateProjectionWorkspaceRead,
)
from ....application.user_profile.profile_record_repository import ProfileRecordRepository
from ....application.workflow.profile_bucket_scan import read_profile_bucket_by_id
from ....domain.modelos.work_unit import WorkUnitState
from ..storage.runtime import inspect_bucket_storage_runtime
from .filing_drafts import ModeloDraftRepository
from .invoices import InvoiceCatalogueRepository
from .modelos_calculation import CalculationRevisionCatalogueRepository
from .modelos_work_units import WorkUnitCatalogueRepository
from .transactions import TransactionCatalogueRepository


[docs] class StateProjectionPersistenceAdapter: """Translate profile persistence records into projection-owned DTOs.""" def __init__(self, *, diagnostics_ports: DiagnosticsPorts) -> None: """Bind the required secure-object diagnostic capability.""" self._diagnostics_ports = diagnostics_ports
[docs] def read_workspace(self, *, bucket_id: str) -> StateProjectionWorkspaceRead: """Read and count every workspace catalogue for ``bucket_id``.""" try: inspect_bucket_storage_runtime(bucket_id).require_ready() transactions = TransactionCatalogueRepository(bucket_id=bucket_id).load() invoices = InvoiceCatalogueRepository(bucket_id=bucket_id).load() drafts = tuple(ModeloDraftRepository(bucket_id=bucket_id).iter_drafts()) work_units = WorkUnitCatalogueRepository(bucket_id=bucket_id).load() revisions = CalculationRevisionCatalogueRepository(bucket_id=bucket_id).load() from ....application.diagnostics import secure_object_unreadable_total active_work_units = sum(1 for unit in work_units.values() if unit.state is WorkUnitState.BORRADOR) discarded_work_units = sum(1 for unit in work_units.values() if unit.state is WorkUnitState.DESCARTADO) return StateProjectionWorkspaceRead( transactions=len(transactions.transactions), invoices=len(invoices), drafts=len(drafts), work_units=active_work_units, discarded_work_units=discarded_work_units, calculation_revisions=len(revisions), unreadable_rows=secure_object_unreadable_total(ports=self._diagnostics_ports), ) except Exception as exc: raise StateProjectionReadError("workspace") from exc
[docs] def read_profile(self, *, profile_id: str) -> StateProjectionProfileRead | None: """Read the profile pointer and its authenticated current record.""" try: pointer = read_profile_bucket_by_id(profile_id) if pointer is None: return None from ....domain.calculations.registry.authority import bundled_indexed_authority with bundled_indexed_authority().operation() as operation: record = ProfileRecordRepository.for_current_session( pointer.bucket_id, profile_decode_context=operation.profile_decode_context(), ).load(pointer.bucket_id) return StateProjectionProfileRead(bucket_id=pointer.bucket_id, record=record) except Exception as exc: raise StateProjectionReadError("profile") from exc