import logging import config from app.connectors import ows_dmp from app.connectors.ows_dmp import StoreNotFoundError 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_SHOPIFY_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.SHOPIFY ): try: ows_dmp.refresh_shopify_store(connector_id=webhook_event.connector_id) except StoreNotFoundError: logger.info("Store not found, skipping...") except Exception as exc: raise WebhookHandleError( f"Failed to refresh Shopify store: {str(exc)}", code="SHOPIFY_STORE_REFRESH_FAILED", ) from exc def handle_destination_event(webhook_event: WebhookEvent) -> None: if webhook_event.event == WebhookEventType.TRANSFORMATION_SUCCEEDED: ows_dmp.handle_transformation_success() pass elif webhook_event.event == WebhookEventType.TRANSFORMATION_FAILED: # todo send alert message to Slack pass def handle_webhook_event(webhook_event: WebhookEvent) -> None: if webhook_event.event in { WebhookEventType.SYNC_START, WebhookEventType.SYNC_END, WebhookEventType.EDIT_CONNECTOR, }: handle_connector_event(webhook_event) elif webhook_event.event in { WebhookEventType.TRANSFORMATION_START, WebhookEventType.TRANSFORMATION_SUCCEEDED, WebhookEventType.TRANSFORMATION_FAILED, }: handle_destination_event(webhook_event) elif webhook_event.event in {WebhookEventType.TEST}: logger.info("Test webhook event received, skipping...")