From de52f2f5e54dd8053afd44fc15ac37641069752c Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sun, 14 Jul 2024 21:38:24 +0300 Subject: [PATCH] add webhook urls record --- .../webhook_settings_controller.rb | 3 +- app/jobs/process_submitter_completion_job.rb | 20 +++++++++- ...send_form_completed_webhook_request_job.rb | 27 ++++++++++--- app/models/account.rb | 1 + app/models/webhook_url.rb | 38 +++++++++++++++++++ .../20240714172222_create_webhook_urls.rb | 14 +++++++ db/schema.rb | 14 ++++++- lib/accounts.rb | 6 ++- 8 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 app/models/webhook_url.rb create mode 100644 db/migrate/20240714172222_create_webhook_urls.rb diff --git a/app/controllers/webhook_settings_controller.rb b/app/controllers/webhook_settings_controller.rb index d61aa30d..4c0f7101 100644 --- a/app/controllers/webhook_settings_controller.rb +++ b/app/controllers/webhook_settings_controller.rb @@ -17,7 +17,8 @@ class WebhookSettingsController < ApplicationController def update submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last - SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id }) + SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id, + 'encrypted_config_id' => @encrypted_config.id }) redirect_back(fallback_location: settings_webhooks_path, notice: 'Webhook request has been sent.') end diff --git a/app/jobs/process_submitter_completion_job.rb b/app/jobs/process_submitter_completion_job.rb index 64bec6e9..ae23a20a 100644 --- a/app/jobs/process_submitter_completion_job.rb +++ b/app/jobs/process_submitter_completion_job.rb @@ -20,9 +20,25 @@ class ProcessSubmitterCompletionJob enqueue_completed_emails(submitter) end - return if Accounts.load_webhook_url(submitter.account).blank? + enqueue_completed_webhooks(submitter) + end + + def enqueue_completed_webhooks(submitter) + webhook_config = Accounts.load_webhook_config(submitter.account) + + if webhook_config + SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id, + 'encrypted_config_id' => webhook_config.id }) + end + + webhook_ids = submitter.account.webhook_urls.where( + Arel::Table.new(:webhook_urls)[:events].matches('%"form.completed"%') + ).pluck(:id) - SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id }) + webhook_ids.each do |webhook_id| + SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id, + 'webhook_url_id' => webhook_id }) + end end def enqueue_completed_emails(submitter) diff --git a/app/jobs/send_form_completed_webhook_request_job.rb b/app/jobs/send_form_completed_webhook_request_job.rb index 8f417278..1012c60a 100644 --- a/app/jobs/send_form_completed_webhook_request_job.rb +++ b/app/jobs/send_form_completed_webhook_request_job.rb @@ -13,13 +13,10 @@ class SendFormCompletedWebhookRequestJob submitter = Submitter.find(params['submitter_id']) attempt = params['attempt'].to_i - url = Accounts.load_webhook_url(submitter.submission.account) - return if url.blank? - - preferences = Accounts.load_webhook_preferences(submitter.submission.account) + url = load_url(submitter, params) - return if preferences['form.completed'] == false + return if url.blank? Submissions::EnsureResultGenerated.call(submitter) @@ -41,10 +38,28 @@ class SendFormCompletedWebhookRequestJob if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS && (!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan)) SendFormCompletedWebhookRequestJob.perform_in((2**attempt).minutes, { - 'submitter_id' => submitter.id, + **params, 'attempt' => attempt + 1, 'last_status' => resp&.status.to_i }) end end + + def load_url(submitter, params) + if params['encrypted_config_id'] + url = EncryptedConfig.find(params['encrypted_config_id']).value + + return if url.blank? + + preferences = Accounts.load_webhook_preferences(submitter.submission.account) + + return if preferences['form.completed'] == false + + url + elsif params['webhook_url_id'] + webhook_url = submitter.account.webhook_urls.find(params['webhook_url_id']) + + webhook_url.url if webhook_url.events.include?('form.completed') + end + end end diff --git a/app/models/account.rb b/app/models/account.rb index 6e8d1718..1cc9f6b0 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -32,6 +32,7 @@ class Account < ApplicationRecord has_many :submitters, through: :submissions has_many :account_linked_accounts, dependent: :destroy has_many :email_events, dependent: :destroy + has_many :webhook_urls, dependent: :destroy has_many :account_testing_accounts, -> { testing }, dependent: :destroy, class_name: 'AccountLinkedAccount', inverse_of: :account diff --git a/app/models/webhook_url.rb b/app/models/webhook_url.rb new file mode 100644 index 00000000..74d70bb4 --- /dev/null +++ b/app/models/webhook_url.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: webhook_urls +# +# id :bigint not null, primary key +# events :text not null +# sha1 :string not null +# url :text not null +# created_at :datetime not null +# updated_at :datetime not null +# account_id :bigint not null +# +# Indexes +# +# index_webhook_urls_on_account_id (account_id) +# index_webhook_urls_on_sha1 (sha1) +# +# Foreign Keys +# +# fk_rails_... (account_id => accounts.id) +# +class WebhookUrl < ApplicationRecord + belongs_to :account + + attribute :events, :string, default: -> { [] } + + serialize :events, coder: JSON + + before_validation :set_sha1 + + encrypts :url + + def set_sha1 + self.sha1 = Digest::SHA1.hexdigest(url) + end +end diff --git a/db/migrate/20240714172222_create_webhook_urls.rb b/db/migrate/20240714172222_create_webhook_urls.rb new file mode 100644 index 00000000..ecdfc43f --- /dev/null +++ b/db/migrate/20240714172222_create_webhook_urls.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class CreateWebhookUrls < ActiveRecord::Migration[7.1] + def change + create_table :webhook_urls do |t| + t.references :account, null: false, foreign_key: true, index: true + t.text :url, null: false + t.text :events, null: false + t.string :sha1, null: false, index: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index bfebcf29..b3455979 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[7.1].define(version: 2024_06_24_102526) do +ActiveRecord::Schema[7.1].define(version: 2024_07_14_172222) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -290,6 +290,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_24_102526) do t.index ["uuid"], name: "index_users_on_uuid", unique: true end + create_table "webhook_urls", force: :cascade do |t| + t.bigint "account_id", null: false + t.text "url", null: false + t.text "events", null: false + t.string "sha1", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["account_id"], name: "index_webhook_urls_on_account_id" + t.index ["sha1"], name: "index_webhook_urls_on_sha1" + end + add_foreign_key "access_tokens", "users" add_foreign_key "account_configs", "accounts" add_foreign_key "account_linked_accounts", "accounts" @@ -315,4 +326,5 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_24_102526) do add_foreign_key "templates", "users", column: "author_id" add_foreign_key "user_configs", "users" add_foreign_key "users", "accounts" + add_foreign_key "webhook_urls", "accounts" end diff --git a/lib/accounts.rb b/lib/accounts.rb index 01158e98..4103f76c 100644 --- a/lib/accounts.rb +++ b/lib/accounts.rb @@ -79,13 +79,17 @@ module Accounts end def load_webhook_url(account) + load_webhook_config(account)&.value.presence + end + + def load_webhook_config(account) configs = account.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY) if !configs && !Docuseal.multitenant? && !account.testing? configs = Account.order(:id).first.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY) end - configs&.value.presence + configs end def load_webhook_preferences(account)