use webhook urls instead of encrypted configs
This commit is contained in:
committed by
Pete Matsyburka
parent
f669045362
commit
84edb6dbda
@@ -67,8 +67,11 @@ module Api
|
||||
|
||||
submissions = create_submissions(@template, params)
|
||||
|
||||
submissions.each do |submission|
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => submission.id })
|
||||
@template.account.webhook_urls.with_event('submission.created').each do |webhook_url|
|
||||
submissions.each do |submission|
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => submission.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
|
||||
Submissions.send_signature_requests(submissions)
|
||||
@@ -93,7 +96,10 @@ module Api
|
||||
else
|
||||
@submission.update!(archived_at: Time.current)
|
||||
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_async('submission_id' => @submission.id)
|
||||
@submission.account.webhook_urls.with_event('submission.archived').each do |webhook_url|
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_async({ 'submission_id' => @submission.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
|
||||
render json: @submission.as_json(only: %i[id archived_at])
|
||||
|
||||
@@ -13,7 +13,10 @@ module Api
|
||||
|
||||
SubmissionEvents.create_with_tracking_data(submitter, 'view_form', request)
|
||||
|
||||
SendFormViewedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id })
|
||||
submitter.account.webhook_urls.with_event('form.viewed').each do |webhook_url|
|
||||
SendFormViewedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
|
||||
render json: {}
|
||||
end
|
||||
|
||||
@@ -25,7 +25,10 @@ module Api
|
||||
|
||||
schema_documents = Templates::CloneAttachments.call(template: cloned_template, original_template: @template)
|
||||
|
||||
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => cloned_template.id)
|
||||
cloned_template.account.webhook_urls.with_event('template.created').each do |webhook_url|
|
||||
SendTemplateCreatedWebhookRequestJob.perform_async({ 'template_id' => cloned_template.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
|
||||
render json: Templates::SerializeForApi.call(cloned_template, schema_documents)
|
||||
end
|
||||
|
||||
@@ -65,7 +65,10 @@ module Api
|
||||
|
||||
@template.update!(template_params)
|
||||
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_async('template_id' => @template.id)
|
||||
@template.account.webhook_urls.with_event('template.updated').each do |webhook_url|
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_async({ 'template_id' => @template.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
|
||||
render json: @template.as_json(only: %i[id updated_at])
|
||||
end
|
||||
|
||||
@@ -38,7 +38,10 @@ class StartFormController < ApplicationController
|
||||
|
||||
if @submitter.save
|
||||
if is_new_record
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => @submitter.submission.id })
|
||||
@submitter.account.webhook_urls.with_event('submission.created').each do |webhook_url|
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => @submitter.submission_id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
|
||||
redirect_to submit_form_path(@submitter.slug)
|
||||
|
||||
@@ -50,9 +50,7 @@ class SubmissionsController < ApplicationController
|
||||
params: params.merge('send_completed_email' => true))
|
||||
end
|
||||
|
||||
submissions.each do |submission|
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => submission.id })
|
||||
end
|
||||
enqueue_submission_created_webhooks(@template, submissions)
|
||||
|
||||
Submissions.send_signature_requests(submissions)
|
||||
|
||||
@@ -68,7 +66,10 @@ class SubmissionsController < ApplicationController
|
||||
else
|
||||
@submission.update!(archived_at: Time.current)
|
||||
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_async('submission_id' => @submission.id)
|
||||
@submission.account.webhook_urls.with_event('submission.archived').each do |webhook_url|
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_async({ 'submission_id' => @submission.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
|
||||
I18n.t('submission_has_been_archived')
|
||||
end
|
||||
@@ -85,6 +86,15 @@ class SubmissionsController < ApplicationController
|
||||
template.save!
|
||||
end
|
||||
|
||||
def enqueue_submission_created_webhooks(template, submissions)
|
||||
template.account.webhook_urls.with_event('submission.created').each do |webhook_url|
|
||||
submissions.each do |submission|
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => submission.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def submissions_params
|
||||
params.permit(submission: { submitters: [%i[uuid email phone name]] })
|
||||
end
|
||||
|
||||
@@ -25,7 +25,10 @@ class SubmitFormDeclineController < ApplicationController
|
||||
SubmitterMailer.declined_email(submitter, user).deliver_later!
|
||||
end
|
||||
|
||||
SendFormDeclinedWebhookRequestJob.perform_async('submitter_id' => submitter.id)
|
||||
submitter.account.webhook_urls.with_event('form.declined').each do |webhook_url|
|
||||
SendFormDeclinedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
|
||||
redirect_to submit_form_path(submitter.slug)
|
||||
end
|
||||
|
||||
@@ -66,7 +66,7 @@ class TemplatesController < ApplicationController
|
||||
if @template.save
|
||||
Templates::CloneAttachments.call(template: @template, original_template: @base_template) if @base_template
|
||||
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_async('template_id' => @template.id)
|
||||
enqueue_template_created_webhooks(@template)
|
||||
|
||||
maybe_redirect_to_template(@template)
|
||||
else
|
||||
@@ -77,7 +77,7 @@ class TemplatesController < ApplicationController
|
||||
def update
|
||||
@template.update!(template_params)
|
||||
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_async('template_id' => @template.id)
|
||||
enqueue_template_updated_webhooks(@template)
|
||||
|
||||
head :ok
|
||||
end
|
||||
@@ -128,6 +128,20 @@ class TemplatesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def enqueue_template_created_webhooks(template)
|
||||
template.account.webhook_urls.with_event('template.updated').each do |webhook_url|
|
||||
SendTemplateCreatedWebhookRequestJob.perform_async({ 'template_id' => template.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
|
||||
def enqueue_template_updated_webhooks(template)
|
||||
template.account.webhook_urls.with_event('template.updated').each do |webhook_url|
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_async({ 'template_id' => template.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
|
||||
def load_base_template
|
||||
return if params[:base_template_id].blank?
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class TemplatesUploadsController < ApplicationController
|
||||
|
||||
@template.update!(schema:)
|
||||
|
||||
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => @template.id)
|
||||
enqueue_template_created_webhooks(@template)
|
||||
|
||||
redirect_to edit_template_path(@template)
|
||||
rescue Templates::CreateAttachments::PdfEncrypted
|
||||
@@ -65,4 +65,11 @@ class TemplatesUploadsController < ApplicationController
|
||||
|
||||
{ files: [file] }
|
||||
end
|
||||
|
||||
def enqueue_template_created_webhooks(template)
|
||||
template.account.webhook_urls.with_event('template.created').each do |webhook_url|
|
||||
SendTemplateCreatedWebhookRequestJob.perform_async({ 'template_id' => template.id,
|
||||
'webhook_url_id' => webhook_url.id })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,9 +4,8 @@ class TestingApiSettingsController < ApplicationController
|
||||
def index
|
||||
authorize!(:manage, current_user.access_token)
|
||||
|
||||
@webhook_config =
|
||||
current_account.encrypted_configs.find_or_initialize_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
||||
@webhook_url = current_account.webhook_urls.first_or_initialize
|
||||
|
||||
authorize!(:manage, @webhook_config)
|
||||
authorize!(:manage, @webhook_url)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WebhookPreferencesController < ApplicationController
|
||||
EVENTS = %w[
|
||||
form.viewed
|
||||
form.started
|
||||
form.completed
|
||||
form.declined
|
||||
template.created
|
||||
template.updated
|
||||
submission.created
|
||||
submission.archived
|
||||
].freeze
|
||||
|
||||
before_action :load_account_config
|
||||
authorize_resource :account_config, parent: false
|
||||
|
||||
def create
|
||||
@account_config.value[account_config_params[:event]] = account_config_params[:value] == '1'
|
||||
|
||||
@account_config.save!
|
||||
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_account_config
|
||||
@account_config =
|
||||
current_account.account_configs.find_or_initialize_by(key: AccountConfig::WEBHOOK_PREFERENCES_KEY)
|
||||
@account_config.value ||= {}
|
||||
end
|
||||
|
||||
def account_config_params
|
||||
params.permit(:event, :value)
|
||||
end
|
||||
end
|
||||
@@ -1,29 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WebhookSecretController < ApplicationController
|
||||
before_action :load_encrypted_config
|
||||
authorize_resource :encrypted_config, parent: false
|
||||
load_and_authorize_resource :webhook_url, parent: false
|
||||
|
||||
def index; end
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@encrypted_config.assign_attributes(value: {
|
||||
encrypted_config_params[:key] => encrypted_config_params[:value]
|
||||
def update
|
||||
@webhook_url.update!(secret: {
|
||||
webhook_secret_params[:key] => webhook_secret_params[:value]
|
||||
}.compact_blank)
|
||||
|
||||
@encrypted_config.value.present? ? @encrypted_config.save! : @encrypted_config.delete
|
||||
|
||||
redirect_back(fallback_location: settings_webhooks_path, notice: I18n.t('webhook_secret_has_been_saved'))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_encrypted_config
|
||||
@encrypted_config =
|
||||
current_account.encrypted_configs.find_or_initialize_by(key: EncryptedConfig::WEBHOOK_SECRET_KEY)
|
||||
end
|
||||
|
||||
def encrypted_config_params
|
||||
params.require(:encrypted_config).permit(value: %i[key value]).fetch(:value, {})
|
||||
def webhook_secret_params
|
||||
params.require(:webhook_url).permit(secret: %i[key value]).fetch(:secret, {})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WebhookSettingsController < ApplicationController
|
||||
before_action :load_encrypted_config
|
||||
authorize_resource :encrypted_config, parent: false
|
||||
before_action :load_webhook_url
|
||||
authorize_resource :webhook_url, parent: false
|
||||
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@encrypted_config.assign_attributes(encrypted_config_params)
|
||||
@webhook_url.assign_attributes(webhook_params)
|
||||
|
||||
@encrypted_config.value.present? ? @encrypted_config.save! : @encrypted_config.delete
|
||||
@webhook_url.url.present? ? @webhook_url.save! : @webhook_url.delete
|
||||
|
||||
redirect_back(fallback_location: settings_webhooks_path, notice: I18n.t('webhook_url_has_been_saved'))
|
||||
end
|
||||
@@ -18,19 +18,18 @@ class WebhookSettingsController < ApplicationController
|
||||
submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last
|
||||
|
||||
SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
||||
'encrypted_config_id' => @encrypted_config.id })
|
||||
'webhook_url_id' => @webhook_url.id })
|
||||
|
||||
redirect_back(fallback_location: settings_webhooks_path, notice: I18n.t('webhook_request_has_been_sent'))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_encrypted_config
|
||||
@encrypted_config =
|
||||
current_account.encrypted_configs.find_or_initialize_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
||||
def load_webhook_url
|
||||
@webhook_url = current_account.webhook_urls.first_or_initialize
|
||||
end
|
||||
|
||||
def encrypted_config_params
|
||||
params.require(:encrypted_config).permit(:value)
|
||||
def webhook_params
|
||||
params.require(:webhook_url).permit(:url, events: [])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -63,24 +63,7 @@ class ProcessSubmitterCompletionJob
|
||||
end
|
||||
|
||||
def enqueue_completed_webhooks(submitter, is_all_completed: false)
|
||||
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_urls = submitter.account.webhook_urls
|
||||
|
||||
webhook_urls = webhook_urls.where(
|
||||
Arel::Table.new(:webhook_urls)[:events].matches('%"form.completed"%')
|
||||
).or(
|
||||
webhook_urls.where(
|
||||
Arel::Table.new(:webhook_urls)[:events].matches('%"submission.completed"%')
|
||||
)
|
||||
)
|
||||
|
||||
webhook_urls.each do |webhook|
|
||||
submitter.account.webhook_urls.with_events(%w[form.completed submission.completed]).each do |webhook|
|
||||
if webhook.events.include?('form.completed')
|
||||
SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
||||
'webhook_url_id' => webhook.id })
|
||||
|
||||
@@ -14,22 +14,23 @@ class SendFormCompletedWebhookRequestJob
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
url, secret = load_url_and_secret(submitter, params)
|
||||
webhook_url = submitter.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return if url.blank?
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.completed')
|
||||
|
||||
Submissions::EnsureResultGenerated.call(submitter)
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'form.completed',
|
||||
timestamp: Time.current,
|
||||
data: Submitters::SerializeForWebhook.call(submitter)
|
||||
}.to_json,
|
||||
**secret.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -45,27 +46,4 @@ class SendFormCompletedWebhookRequestJob
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def load_url_and_secret(submitter, params)
|
||||
if params['encrypted_config_id']
|
||||
config = EncryptedConfig.find(params['encrypted_config_id'])
|
||||
|
||||
url = config.value
|
||||
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.completed'] == false
|
||||
|
||||
secret = EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h
|
||||
|
||||
[url, secret]
|
||||
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
|
||||
|
||||
@@ -13,26 +13,22 @@ class SendFormDeclinedWebhookRequestJob
|
||||
submitter = Submitter.find(params['submitter_id'])
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
config = Accounts.load_webhook_config(submitter.submission.account)
|
||||
url = config&.value.presence
|
||||
|
||||
return if url.blank?
|
||||
webhook_url = submitter.submission.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.declined'] == false
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.declined')
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'form.declined',
|
||||
timestamp: Time.current,
|
||||
data: Submitters::SerializeForWebhook.call(submitter)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -43,6 +39,7 @@ class SendFormDeclinedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormDeclinedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'submitter_id' => submitter.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -13,26 +13,22 @@ class SendFormStartedWebhookRequestJob
|
||||
submitter = Submitter.find(params['submitter_id'])
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
config = Accounts.load_webhook_config(submitter.submission.account)
|
||||
url = config&.value.presence
|
||||
|
||||
return if url.blank?
|
||||
webhook_url = submitter.submission.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.started'] == false
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.started')
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'form.started',
|
||||
timestamp: Time.current,
|
||||
data: Submitters::SerializeForWebhook.call(submitter)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -43,6 +39,7 @@ class SendFormStartedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormStartedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'submitter_id' => submitter.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -13,26 +13,22 @@ class SendFormViewedWebhookRequestJob
|
||||
submitter = Submitter.find(params['submitter_id'])
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
config = Accounts.load_webhook_config(submitter.submission.account)
|
||||
url = config&.value.presence
|
||||
|
||||
return if url.blank?
|
||||
webhook_url = submitter.submission.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.viewed'] == false
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.viewed')
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'form.viewed',
|
||||
timestamp: Time.current,
|
||||
data: Submitters::SerializeForWebhook.call(submitter)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -43,6 +39,7 @@ class SendFormViewedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormViewedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'submitter_id' => submitter.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -14,24 +14,19 @@ class SendSubmissionArchivedWebhookRequestJob
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
config = Accounts.load_webhook_config(submission.account)
|
||||
url = config&.value.presence
|
||||
webhook_url = submission.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submission.account)
|
||||
|
||||
return if preferences['submission.archived'].blank?
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.archived')
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'submission.archived',
|
||||
timestamp: Time.current,
|
||||
data: submission.as_json(only: %i[id archived_at])
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -42,6 +37,7 @@ class SendSubmissionArchivedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || submission.account.account_configs.exists?(key: :plan))
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'submission_id' => submission.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -14,21 +14,19 @@ class SendSubmissionCompletedWebhookRequestJob
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
webhook_url = submission.account.webhook_urls.find(params['webhook_url_id'])
|
||||
webhook_url = submission.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
url = webhook_url.url if webhook_url.events.include?('submission.completed')
|
||||
|
||||
return if url.blank?
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.completed')
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'submission.completed',
|
||||
timestamp: Time.current,
|
||||
data: Submissions::SerializeForApi.call(submission)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: submission.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
|
||||
@@ -14,24 +14,19 @@ class SendSubmissionCreatedWebhookRequestJob
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
config = Accounts.load_webhook_config(submission.account)
|
||||
url = config&.value.presence
|
||||
webhook_url = submission.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submission.account)
|
||||
|
||||
return if preferences['submission.created'].blank?
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.created')
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'submission.created',
|
||||
timestamp: Time.current,
|
||||
data: Submissions::SerializeForApi.call(submission)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -42,6 +37,7 @@ class SendSubmissionCreatedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || submission.account.account_configs.exists?(key: :plan))
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'submission_id' => submission.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -14,24 +14,19 @@ class SendTemplateCreatedWebhookRequestJob
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
config = Accounts.load_webhook_config(template.account)
|
||||
url = config&.value.presence
|
||||
webhook_url = template.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(template.account)
|
||||
|
||||
return if preferences['template.created'].blank?
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('template.created')
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'template.created',
|
||||
timestamp: Time.current,
|
||||
data: Templates::SerializeForApi.call(template)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -42,6 +37,7 @@ class SendTemplateCreatedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || template.account.account_configs.exists?(key: :plan))
|
||||
SendTemplateCreatedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'template_id' => template.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -14,24 +14,19 @@ class SendTemplateUpdatedWebhookRequestJob
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
config = Accounts.load_webhook_config(template.account)
|
||||
url = config&.value.presence
|
||||
webhook_url = template.account.webhook_urls.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(template.account)
|
||||
|
||||
return if preferences['template.updated'].blank?
|
||||
return unless webhook_url
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('template.updated')
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
Faraday.post(webhook_url.url,
|
||||
{
|
||||
event_type: 'template.updated',
|
||||
timestamp: Time.current,
|
||||
data: Templates::SerializeForApi.call(template)
|
||||
}.to_json,
|
||||
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
|
||||
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
|
||||
**webhook_url.secret.to_h,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
@@ -42,6 +37,7 @@ class SendTemplateUpdatedWebhookRequestJob
|
||||
(!Docuseal.multitenant? || template.account.account_configs.exists?(key: :plan))
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
'template_id' => template.id,
|
||||
'webhook_url_id' => webhook_url.id,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
|
||||
@@ -35,7 +35,6 @@ class AccountConfig < ApplicationRecord
|
||||
FORM_WITH_CONFETTI_KEY = 'form_with_confetti'
|
||||
FORM_PREFILL_SIGNATURE_KEY = 'form_prefill_signature'
|
||||
ESIGNING_PREFERENCE_KEY = 'esigning_preference'
|
||||
WEBHOOK_PREFERENCES_KEY = 'webhook_preferences'
|
||||
DOWNLOAD_LINKS_AUTH_KEY = 'download_links_auth'
|
||||
FORCE_SSO_AUTH_KEY = 'force_sso_auth'
|
||||
FLATTEN_RESULT_PDF_KEY = 'flatten_result_pdf'
|
||||
|
||||
@@ -26,9 +26,7 @@ class EncryptedConfig < ApplicationRecord
|
||||
EMAIL_SMTP_KEY = 'action_mailer_smtp',
|
||||
ESIGN_CERTS_KEY = 'esign_certs',
|
||||
TIMESTAMP_SERVER_URL_KEY = 'timestamp_server_url',
|
||||
APP_URL_KEY = 'app_url',
|
||||
WEBHOOK_URL_KEY = 'webhook_url',
|
||||
WEBHOOK_SECRET_KEY = 'webhook_secret'
|
||||
APP_URL_KEY = 'app_url'
|
||||
].freeze
|
||||
|
||||
belongs_to :account
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# events :text not null
|
||||
# secret :text
|
||||
# sha1 :string not null
|
||||
# url :text not null
|
||||
# created_at :datetime not null
|
||||
@@ -22,15 +23,34 @@
|
||||
# fk_rails_... (account_id => accounts.id)
|
||||
#
|
||||
class WebhookUrl < ApplicationRecord
|
||||
EVENTS = %w[
|
||||
form.viewed
|
||||
form.started
|
||||
form.completed
|
||||
form.declined
|
||||
template.created
|
||||
template.updated
|
||||
submission.created
|
||||
submission.archived
|
||||
].freeze
|
||||
|
||||
belongs_to :account
|
||||
|
||||
attribute :events, :string, default: -> { [] }
|
||||
attribute :events, :string, default: -> { %w[form.viewed form.started form.completed form.declined] }
|
||||
|
||||
serialize :events, coder: JSON
|
||||
serialize :secret, coder: JSON
|
||||
|
||||
scope :with_event, ->(event) { with_events([event]) }
|
||||
scope :with_events, lambda { |events|
|
||||
where(events.map do |event|
|
||||
Arel::Table.new(:webhook_urls)[:events].matches("%\"#{event}\"%")
|
||||
end.reduce(:or))
|
||||
}
|
||||
|
||||
before_validation :set_sha1
|
||||
|
||||
encrypts :url
|
||||
encrypts :url, :secret
|
||||
|
||||
def set_sha1
|
||||
self.sha1 = Digest::SHA1.hexdigest(url)
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
<%= link_to 'API', settings_api_index_path, class: 'text-base hover:bg-base-300' %>
|
||||
</li>
|
||||
<% end %>
|
||||
<% if can?(:read, EncryptedConfig.new(key: EncryptedConfig::WEBHOOK_URL_KEY, account: current_account)) %>
|
||||
<% if can?(:read, WebhookUrl) %>
|
||||
<li>
|
||||
<%= link_to 'Webhooks', settings_webhooks_path, class: 'text-base hover:bg-base-300' %>
|
||||
</li>
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
<%= render 'shared/clipboard_copy', icon: 'copy', text: current_user.access_token.token, class: 'base-button', icon_class: 'w-6 h-6 text-white', copy_title: t('copy'), copied_title: t('copied') %>
|
||||
</div>
|
||||
</div>
|
||||
<%= form_for @webhook_config, url: settings_webhooks_path, method: :post, html: { autocomplete: 'off' }, data: { turbo_frame: :_top } do |f| %>
|
||||
<%= f.label :value, 'Webhook URL', class: 'text-sm font-semibold' %>
|
||||
<%= form_for @webhook_url, url: settings_webhooks_path, method: :post, html: { autocomplete: 'off' }, data: { turbo_frame: :_top } do |f| %>
|
||||
<%= f.label :url, 'Webhook URL', class: 'text-sm font-semibold' %>
|
||||
<div class="space-y-2 md:flex-nowrap mt-2">
|
||||
<%= f.url_field :value, class: 'base-input w-full', placeholder: 'https://example.com/hook' %>
|
||||
<%= f.url_field :url, class: 'base-input w-full', placeholder: 'https://example.com/hook' %>
|
||||
<%= f.button button_title(title: t('save'), disabled_with: t('saving')), class: 'base-button w-full' %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<%= render 'shared/turbo_modal', title: 'Webhook Secret' do %>
|
||||
<%= form_for @encrypted_config, url: webhook_secret_index_path, method: :post, html: { class: 'space-y-4' }, data: { turbo_frame: :_top } do |f| %>
|
||||
<%= render 'shared/turbo_modal', title: t('webhook_secret') do %>
|
||||
<%= form_for @webhook_url, url: webhook_secret_path, method: :patch, html: { class: 'space-y-4' }, data: { turbo_frame: :_top } do |f| %>
|
||||
<div class="space-y-2">
|
||||
<%= f.fields_for :value, Struct.new(:key, :value).new(*@encrypted_config.value.to_a.first) do |ff| %>
|
||||
<%= f.fields_for :secret, Struct.new(:key, :value).new(*@webhook_url.secret.to_a.first) do |ff| %>
|
||||
<div class="form-control">
|
||||
<%= ff.label :key, class: 'label' %>
|
||||
<%= ff.label :key, t('key'), class: 'label' %>
|
||||
<%= ff.text_field :key, class: 'base-input', placeholder: 'X-Example-Header' %>
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<%= ff.label :value, class: 'label' %>
|
||||
<%= ff.label :value, t('value'), class: 'label' %>
|
||||
<%= ff.text_field :value, class: 'base-input' %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -7,34 +7,31 @@
|
||||
</div>
|
||||
<div class="card bg-base-200">
|
||||
<div class="card-body p-6">
|
||||
<%= form_for @encrypted_config, url: settings_webhooks_path, method: :post, html: { autocomplete: 'off' } do |f| %>
|
||||
<%= f.label :value, 'Webhook URL', class: 'text-sm font-semibold' %>
|
||||
<%= form_for @webhook_url, url: settings_webhooks_path, method: :post, html: { autocomplete: 'off' } do |f| %>
|
||||
<%= f.label :url, 'Webhook URL', class: 'text-sm font-semibold' %>
|
||||
<div class="flex flex-row flex-wrap space-y-2 md:space-y-0 md:flex-nowrap md:space-x-2 mt-2">
|
||||
<%= f.url_field :value, class: 'input font-mono input-bordered w-full', placeholder: 'https://example.com/hook' %>
|
||||
<%= f.url_field :url, class: 'input font-mono input-bordered w-full', placeholder: 'https://example.com/hook' %>
|
||||
<%= f.button button_title(title: t('save'), disabled_with: t('saving')), class: 'base-button w-full md:w-32' %>
|
||||
<a href="<%= webhook_secret_index_path %>" data-turbo-frame="modal" class="white-button w-full md:w-auto">
|
||||
<%= t('add_secret') %>
|
||||
</a>
|
||||
<% if @webhook_url.persisted? %>
|
||||
<a href="<%= webhook_secret_path(@webhook_url) %>" data-turbo-frame="modal" class="white-button w-full md:w-auto">
|
||||
<%= @webhook_url.secret.present? ? t('edit_secret') : t('add_secret') %>
|
||||
</a>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% preference = current_account.account_configs.find_by(key: AccountConfig::WEBHOOK_PREFERENCES_KEY)&.value || {} %>
|
||||
<% WebhookPreferencesController::EVENTS.group_by { |e| e.include?('form') }.each do |_, events| %>
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 mt-2 gap-y-2">
|
||||
<% events.each do |event| %>
|
||||
<%= form_for '', url: webhook_preferences_path, method: :post do |f| %>
|
||||
<%= f.hidden_field :event, value: event %>
|
||||
<% uuid = SecureRandom.uuid %>
|
||||
<% WebhookUrl::EVENTS.group_by { |e| e.include?('form') }.each do |_, events| %>
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 mt-4 gap-y-2">
|
||||
<%= f.collection_check_boxes(:events, events, :to_s, :to_s, include_hidden: false) do |b| %>
|
||||
<div class="flex">
|
||||
<label for="<%= uuid %>" class="flex items-center space-x-2">
|
||||
<%= f.check_box :value, class: 'base-checkbox', checked: preference[event] || (!preference.key?(event) && event.starts_with?('form.')), onchange: 'this.form.requestSubmit()', id: uuid %>
|
||||
<label class="flex items-center space-x-2">
|
||||
<%= b.check_box class: 'base-checkbox', checked: @webhook_url.events.include?(b.value), onchange: 'this.form.requestSubmit()', disabled: @webhook_url.url.blank? %>
|
||||
<span>
|
||||
<%= event %>
|
||||
<%= b.label %>
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
@@ -47,7 +44,7 @@
|
||||
<span>
|
||||
<%= t('submission_example_payload') %>
|
||||
</span>
|
||||
<% if @encrypted_config.value.present? %>
|
||||
<% if @webhook_url.url.present? && @webhook_url.events.include?('form.completed') %>
|
||||
<%= button_to button_title(title: 'Test Webhook', disabled_with: t('sending'), icon_disabled: svg_icon('loader', class: 'w-4 h-4 animate-spin')), settings_webhooks_path, class: 'btn btn-neutral btn-outline btn-sm', method: :put %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user