add webhook urls record
This commit is contained in:
@@ -17,7 +17,8 @@ class WebhookSettingsController < ApplicationController
|
|||||||
def update
|
def update
|
||||||
submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last
|
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.')
|
redirect_back(fallback_location: settings_webhooks_path, notice: 'Webhook request has been sent.')
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,9 +20,25 @@ class ProcessSubmitterCompletionJob
|
|||||||
enqueue_completed_emails(submitter)
|
enqueue_completed_emails(submitter)
|
||||||
end
|
end
|
||||||
|
|
||||||
return if Accounts.load_webhook_url(submitter.account).blank?
|
enqueue_completed_webhooks(submitter)
|
||||||
|
end
|
||||||
|
|
||||||
SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id })
|
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)
|
||||||
|
|
||||||
|
webhook_ids.each do |webhook_id|
|
||||||
|
SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
||||||
|
'webhook_url_id' => webhook_id })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def enqueue_completed_emails(submitter)
|
def enqueue_completed_emails(submitter)
|
||||||
|
|||||||
@@ -13,14 +13,11 @@ class SendFormCompletedWebhookRequestJob
|
|||||||
submitter = Submitter.find(params['submitter_id'])
|
submitter = Submitter.find(params['submitter_id'])
|
||||||
|
|
||||||
attempt = params['attempt'].to_i
|
attempt = params['attempt'].to_i
|
||||||
url = Accounts.load_webhook_url(submitter.submission.account)
|
|
||||||
|
url = load_url(submitter, params)
|
||||||
|
|
||||||
return if url.blank?
|
return if url.blank?
|
||||||
|
|
||||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
|
||||||
|
|
||||||
return if preferences['form.completed'] == false
|
|
||||||
|
|
||||||
Submissions::EnsureResultGenerated.call(submitter)
|
Submissions::EnsureResultGenerated.call(submitter)
|
||||||
|
|
||||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||||
@@ -41,10 +38,28 @@ class SendFormCompletedWebhookRequestJob
|
|||||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||||
SendFormCompletedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
SendFormCompletedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||||
'submitter_id' => submitter.id,
|
**params,
|
||||||
'attempt' => attempt + 1,
|
'attempt' => attempt + 1,
|
||||||
'last_status' => resp&.status.to_i
|
'last_status' => resp&.status.to_i
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class Account < ApplicationRecord
|
|||||||
has_many :submitters, through: :submissions
|
has_many :submitters, through: :submissions
|
||||||
has_many :account_linked_accounts, dependent: :destroy
|
has_many :account_linked_accounts, dependent: :destroy
|
||||||
has_many :email_events, dependent: :destroy
|
has_many :email_events, dependent: :destroy
|
||||||
|
has_many :webhook_urls, dependent: :destroy
|
||||||
has_many :account_testing_accounts, -> { testing }, dependent: :destroy,
|
has_many :account_testing_accounts, -> { testing }, dependent: :destroy,
|
||||||
class_name: 'AccountLinkedAccount',
|
class_name: 'AccountLinkedAccount',
|
||||||
inverse_of: :account
|
inverse_of: :account
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
||||||
+13
-1
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
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
|
t.index ["uuid"], name: "index_users_on_uuid", unique: true
|
||||||
end
|
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 "access_tokens", "users"
|
||||||
add_foreign_key "account_configs", "accounts"
|
add_foreign_key "account_configs", "accounts"
|
||||||
add_foreign_key "account_linked_accounts", "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 "templates", "users", column: "author_id"
|
||||||
add_foreign_key "user_configs", "users"
|
add_foreign_key "user_configs", "users"
|
||||||
add_foreign_key "users", "accounts"
|
add_foreign_key "users", "accounts"
|
||||||
|
add_foreign_key "webhook_urls", "accounts"
|
||||||
end
|
end
|
||||||
|
|||||||
+5
-1
@@ -79,13 +79,17 @@ module Accounts
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_webhook_url(account)
|
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)
|
configs = account.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
||||||
|
|
||||||
if !configs && !Docuseal.multitenant? && !account.testing?
|
if !configs && !Docuseal.multitenant? && !account.testing?
|
||||||
configs = Account.order(:id).first.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
configs = Account.order(:id).first.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
||||||
end
|
end
|
||||||
|
|
||||||
configs&.value.presence
|
configs
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_webhook_preferences(account)
|
def load_webhook_preferences(account)
|
||||||
|
|||||||
Reference in New Issue
Block a user