Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions app/jobs/droplet_installed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@
end

register_active_callbacks(company)
create_webhooks(company)
end

private
private

Check failure on line 43 in app/jobs/droplet_installed_job.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/AccessModifierIndentation: Outdent access modifiers like `private`.

def fluid_client(company)

Check failure on line 45 in app/jobs/droplet_installed_job.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation.
@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 = []

Expand Down Expand Up @@ -74,4 +79,27 @@
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)

Check failure on line 93 in app/jobs/droplet_installed_job.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/LineLength: Line is too long. [125/120]

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
23 changes: 22 additions & 1 deletion app/jobs/droplet_uninstalled_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Comment on lines +45 to +47

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rescue => e
Rails.logger.error("[DropletUninstalledJob] Failed to delete webhook #{webhook_id}: #{e.message}")
end
end

company.update(installed_webhook_ids: [])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take care 👀

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddInstalledWebhookIdsToCompany < ActiveRecord::Migration[8.0]
def change
add_column :companies, :installed_webhook_ids, :jsonb, default: []
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions lib/tasks/settings.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
module Tasks
class Settings
class << self

Check failure on line 4 in lib/tasks/settings.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning.
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
Expand Down Expand Up @@ -192,6 +218,10 @@
webhook_uninstallation_id: {
type: "string",
},
events: {
type: "array",
items: { enum: AVAILABLE_WEBHOOK_EVENTS },
},
},
}
setting.values = {
Expand Down
Loading