import logging import config from app.connectors import ows_dmp from app.connectors.ows_dmp import TikTokAdReportingConnectionNotFoundError from app.enums import ConnectorType, WebhookEventType from app.exceptions import WebhookHandleError from app.models import WebhookEvent logger = logging.getLogger(__name__) TEST_DESTINATION_GROUP_ID = "_destination_1" def handle_connector_event(webhook_event: WebhookEvent) -> None: if webhook_event.destination_group_id == TEST_DESTINATION_GROUP_ID: return None if webhook_event.destination_group_id != config.FIVETRAN_REPORTING_GROUP_ID: raise WebhookHandleError( f"Invalid destination group id: {webhook_event.destination_group_id}", code="INVALID_GROUP_ID", ) if ( webhook_event.connector_id and webhook_event.connector_type and webhook_event.connector_type == ConnectorType.TIKTOK_ADS ): try: ows_dmp.refresh_tiktok_ad_reporting_connection( connector_id=webhook_event.connector_id ) except TikTokAdReportingConnectionNotFoundError: logger.info("Association not found, skipping...") except Exception as exc: raise WebhookHandleError( f"Failed to refresh Meta ad reporting connection: {str(exc)}", code="TIKTOK_AD_REPORTING_CONNECTION_REFRESH_FAILED", ) from exc def handle_webhook_event(webhook_event: WebhookEvent) -> None: if webhook_event.event in {WebhookEventType.TEST}: logger.info("Test webhook event received, skipping...") return None if webhook_event.event not in WebhookEventType.ad_reporting_events(): logger.warning("Not supportable event type for , skipping...") return None if webhook_event.event in { WebhookEventType.SYNC_START, WebhookEventType.SYNC_END, WebhookEventType.EDIT_CONNECTOR, }: handle_connector_event(webhook_event)