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__importannotationsfrom....application.diagnostics_portsimportDiagnosticsPortsfrom....application.state_projection_portsimport(StateProjectionProfileRead,StateProjectionReadError,StateProjectionWorkspaceRead,)from....application.user_profile.profile_record_repositoryimportProfileRecordRepositoryfrom....application.workflow.profile_bucket_scanimportread_profile_bucket_by_idfrom....domain.modelos.work_unitimportWorkUnitStatefrom..storage.runtimeimportinspect_bucket_storage_runtimefrom.filing_draftsimportModeloDraftRepositoryfrom.invoicesimportInvoiceCatalogueRepositoryfrom.modelos_calculationimportCalculationRevisionCatalogueRepositoryfrom.modelos_work_unitsimportWorkUnitCatalogueRepositoryfrom.transactionsimportTransactionCatalogueRepository
[docs]classStateProjectionPersistenceAdapter:"""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]defread_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.diagnosticsimportsecure_object_unreadable_totalactive_work_units=sum(1forunitinwork_units.values()ifunit.stateisWorkUnitState.BORRADOR)discarded_work_units=sum(1forunitinwork_units.values()ifunit.stateisWorkUnitState.DESCARTADO)returnStateProjectionWorkspaceRead(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),)exceptExceptionasexc:raiseStateProjectionReadError("workspace")fromexc
[docs]defread_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)ifpointerisNone:returnNonefrom....domain.calculations.registry.authorityimportbundled_indexed_authoritywithbundled_indexed_authority().operation()asoperation:record=ProfileRecordRepository.for_current_session(pointer.bucket_id,profile_decode_context=operation.profile_decode_context(),).load(pointer.bucket_id)returnStateProjectionProfileRead(bucket_id=pointer.bucket_id,record=record)exceptExceptionasexc:raiseStateProjectionReadError("profile")fromexc