diff --git a/app/jobs/droplet_installed_job.rb b/app/jobs/droplet_installed_job.rb index d0d8f899..06b25779 100644 --- a/app/jobs/droplet_installed_job.rb +++ b/app/jobs/droplet_installed_job.rb @@ -37,12 +37,17 @@ def process_webhook end register_active_callbacks(company) + create_webhooks(company) end -private + private + + def fluid_client(company) + @fluid_client ||= FluidClient.new(company.authentication_token) + end def register_active_callbacks(company) - client = FluidClient.new(company.authentication_token) + client = fluid_client(company) active_callbacks = ::Callback.active installed_callback_ids = [] @@ -74,4 +79,27 @@ def register_active_callbacks(company) company.update(installed_callback_ids: installed_callback_ids) end end + + def create_webhooks(company) + webhooks = Setting.fluid_webhook.values["events"] + return if webhooks.blank? + + client = fluid_client(company) + installed_webhook_ids = [] + + begin + webhooks.each do |event| + resource, event = event.split("_") + webhook_response = client.webhooks.create(resource: resource, event: event, auth_token: company.authentication_token) + + installed_webhook_ids << webhook_response["id"] + end + rescue FluidClient::Error => e + Rails.logger.error "Failed to create #{resource} #{event} webhook: #{e.message}" + end + + if installed_webhook_ids.any? + company.update(installed_webhook_ids: installed_webhook_ids) + end + end end diff --git a/app/jobs/droplet_uninstalled_job.rb b/app/jobs/droplet_uninstalled_job.rb index 8f0cc22f..c8ce7616 100644 --- a/app/jobs/droplet_uninstalled_job.rb +++ b/app/jobs/droplet_uninstalled_job.rb @@ -7,6 +7,7 @@ def process_webhook if company.present? delete_installed_callbacks(company) + delete_installed_webhooks(company) company.update(uninstalled_at: Time.current) else @@ -16,10 +17,14 @@ def process_webhook private + def fluid_client(company) + @fluid_client ||= FluidClient.new(company.authentication_token) + end + def delete_installed_callbacks(company) return unless company.installed_callback_ids.present? - client = FluidClient.new(company.authentication_token) + client = fluid_client(company) company.installed_callback_ids.each do |callback_id| begin @@ -31,4 +36,20 @@ def delete_installed_callbacks(company) company.update(installed_callback_ids: []) end + + def delete_installed_webhooks(company) + return unless company.installed_webhook_ids.present? + + client = fluid_client(company) + + company.installed_webhook_ids.each do |webhook_id| + begin + client.webhooks.delete(webhook_id) + rescue => e + Rails.logger.error("[DropletUninstalledJob] Failed to delete webhook #{webhook_id}: #{e.message}") + end + end + + company.update(installed_webhook_ids: []) + end end diff --git a/db/migrate/20250903133322_add_installed_webhook_ids_to_company.rb b/db/migrate/20250903133322_add_installed_webhook_ids_to_company.rb new file mode 100644 index 00000000..849aa008 --- /dev/null +++ b/db/migrate/20250903133322_add_installed_webhook_ids_to_company.rb @@ -0,0 +1,5 @@ +class AddInstalledWebhookIdsToCompany < ActiveRecord::Migration[8.0] + def change + add_column :companies, :installed_webhook_ids, :jsonb, default: [] + end +end diff --git a/db/schema.rb b/db/schema.rb index 89ad9a5f..bf74a87f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_08_11_182801) do +ActiveRecord::Schema[8.0].define(version: 2025_09_03_133322) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -39,6 +39,7 @@ t.datetime "updated_at", null: false t.string "droplet_installation_uuid" t.jsonb "installed_callback_ids", default: [] + t.jsonb "installed_webhook_ids", default: [] t.index ["active"], name: "index_companies_on_active" t.index ["authentication_token"], name: "index_companies_on_authentication_token", unique: true t.index ["company_droplet_uuid"], name: "index_companies_on_company_droplet_uuid" diff --git a/lib/tasks/settings.rb b/lib/tasks/settings.rb index 6335a39e..5613d40c 100644 --- a/lib/tasks/settings.rb +++ b/lib/tasks/settings.rb @@ -1,6 +1,32 @@ module Tasks class Settings class << self + + AVAILABLE_WEBHOOK_EVENTS = %w[ + cart_abandoned + cart_updated + contact_created + contact_updated + event_created + event_deleted + event_updated + order_cancelled + order_completed + order_updated + order_shipped + order_refunded + popup_submitted + product_created + product_updated + product_destroyed + subscription_started + subscription_paused + subscription_cancelled + user_created + user_updated + user_deactivated + ] + def create_defaults create_host_server_setting create_fluid_api_setting @@ -192,6 +218,10 @@ def create_fluid_webhook_setting webhook_uninstallation_id: { type: "string", }, + events: { + type: "array", + items: { enum: AVAILABLE_WEBHOOK_EVENTS }, + }, }, } setting.values = {