defmodule SimpleshopTheme.Webhooks do @moduledoc """ Handles incoming webhook events from POD providers. """ alias SimpleshopTheme.Products alias SimpleshopTheme.Sync.ProductSyncWorker alias SimpleshopTheme.Webhooks.ProductDeleteWorker require Logger @doc """ Handles a Printify webhook event. Returns :ok or {:ok, job} on success, {:error, reason} on failure. """ def handle_printify_event("product:updated", %{"id" => _product_id}) do enqueue_product_sync() end def handle_printify_event("product:publish:started", %{"id" => _product_id}) do enqueue_product_sync() end def handle_printify_event("product:deleted", %{"id" => product_id}) do ProductDeleteWorker.enqueue(product_id) end def handle_printify_event("shop:disconnected", _resource) do Logger.warning("Printify shop disconnected - manual intervention needed") :ok end def handle_printify_event(event_type, _resource) do Logger.info("Ignoring unhandled Printify event: #{event_type}") :ok end defp enqueue_product_sync do case Products.get_provider_connection_by_type("printify") do nil -> {:error, :no_connection} conn -> ProductSyncWorker.enqueue(conn.id) end end end