from dataclasses import dataclass from anydi import singleton from dmp.adapters.db import DefaultDB, ReportingDB, transactional from dmp.adapters.fivetran import FivetranClient from dmp.app_connections.enums import AppConnectionStatus from dmp.meta.exceptions import MetaAdReportingConnectionNotFoundError from dmp.meta.models import MetaAdReportingConnection from dmp.meta.repositories import MetaAdReportingConnectionRepository from dmp.meta.services import MetaAdReportingConnectionService @dataclass class RefreshMetaAdReportingConnectionRequest: fivetran_connection_id: str @singleton class RefreshMetaAdReportingConnectionHandler: def __init__( self, db: DefaultDB, reporting_db: ReportingDB, fivetran_client: FivetranClient, ad_reporting_connection_repository: MetaAdReportingConnectionRepository, ad_reporting_connection_service: MetaAdReportingConnectionService, ) -> None: self.db = db self.reporting_db = reporting_db self.fivetran_client = fivetran_client self.ad_reporting_connection_repository = ad_reporting_connection_repository self.ad_reporting_connection_service = ad_reporting_connection_service @transactional def handle( self, request: RefreshMetaAdReportingConnectionRequest ) -> MetaAdReportingConnection: connection = ( self.ad_reporting_connection_repository.get_by_fivetran_connection_id( fivetran_connection_id=request.fivetran_connection_id ) ) if not connection or connection.status == AppConnectionStatus.DELETED: raise MetaAdReportingConnectionNotFoundError connection = self.ad_reporting_connection_service.refresh_connection(connection) return connection