add webhook event preferences
This commit is contained in:
@@ -102,6 +102,8 @@ module Api
|
||||
def destroy
|
||||
@submission.update!(archived_at: Time.current)
|
||||
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_later(@submission)
|
||||
|
||||
render json: @submission.as_json(only: %i[id], methods: %i[archived_at])
|
||||
end
|
||||
|
||||
|
||||
@@ -50,6 +50,8 @@ class SubmissionsController < ApplicationController
|
||||
def destroy
|
||||
@submission.update!(archived_at: Time.current)
|
||||
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_later(@submission)
|
||||
|
||||
redirect_back(fallback_location: template_path(@submission.template), notice: 'Submission has been archived')
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WebhookPreferencesController < ApplicationController
|
||||
EVENTS = %w[
|
||||
form.viewed
|
||||
form.started
|
||||
form.completed
|
||||
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
|
||||
@@ -16,7 +16,7 @@ class ProcessSubmitterCompletionJob < ApplicationJob
|
||||
enqueue_completed_emails(submitter)
|
||||
end
|
||||
|
||||
return if Accounts.load_webhook_configs(submitter.account).blank?
|
||||
return if Accounts.load_webhook_url(submitter.account).blank?
|
||||
|
||||
SendFormCompletedWebhookRequestJob.perform_later(submitter)
|
||||
end
|
||||
|
||||
@@ -7,16 +7,20 @@ class SendFormCompletedWebhookRequestJob < ApplicationJob
|
||||
|
||||
def perform(submitter, params = {})
|
||||
attempt = params[:attempt].to_i
|
||||
config = Accounts.load_webhook_configs(submitter.submission.account)
|
||||
url = Accounts.load_webhook_url(submitter.submission.account)
|
||||
|
||||
return if config.blank? || config.value.blank?
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.completed'] == false
|
||||
|
||||
Submissions::EnsureResultGenerated.call(submitter)
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(config.value,
|
||||
Faraday.post(url,
|
||||
{
|
||||
event_type: 'form.completed',
|
||||
timestamp: Time.current,
|
||||
|
||||
@@ -7,14 +7,18 @@ class SendFormStartedWebhookRequestJob < ApplicationJob
|
||||
|
||||
def perform(submitter, params = {})
|
||||
attempt = params[:attempt].to_i
|
||||
config = Accounts.load_webhook_configs(submitter.submission.account)
|
||||
url = Accounts.load_webhook_url(submitter.submission.account)
|
||||
|
||||
return if config.blank? || config.value.blank?
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.started'] == false
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(config.value,
|
||||
Faraday.post(url,
|
||||
{
|
||||
event_type: 'form.started',
|
||||
timestamp: Time.current,
|
||||
|
||||
@@ -7,14 +7,18 @@ class SendFormViewedWebhookRequestJob < ApplicationJob
|
||||
|
||||
def perform(submitter, params = {})
|
||||
attempt = params[:attempt].to_i
|
||||
config = Accounts.load_webhook_configs(submitter.submission.account)
|
||||
url = Accounts.load_webhook_url(submitter.submission.account)
|
||||
|
||||
return if config.blank? || config.value.blank?
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
|
||||
|
||||
return if preferences['form.viewed'] == false
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = begin
|
||||
Faraday.post(config.value,
|
||||
Faraday.post(url,
|
||||
{
|
||||
event_type: 'form.viewed',
|
||||
timestamp: Time.current,
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendSubmissionArchivedWebhookRequestJob < ApplicationJob
|
||||
USER_AGENT = 'DocuSeal.co Webhook'
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
|
||||
def perform(submission, params = {})
|
||||
attempt = params[:attempt].to_i
|
||||
url = Accounts.load_webhook_url(submission.account)
|
||||
|
||||
return if url.blank?
|
||||
|
||||
preferences = Accounts.load_webhook_preferences(submission.account)
|
||||
|
||||
return if preferences['submission.archived'].blank?
|
||||
|
||||
resp = begin
|
||||
Faraday.post(url,
|
||||
{
|
||||
event_type: 'submission.archived',
|
||||
timestamp: Time.current,
|
||||
data: submission.as_json(only: %i[id archived_at])
|
||||
}.to_json,
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => USER_AGENT)
|
||||
rescue Faraday::Error
|
||||
nil
|
||||
end
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendSubmissionArchivedWebhookRequestJob.set(wait: (2**attempt).minutes)
|
||||
.perform_later(submitter, {
|
||||
attempt: attempt + 1,
|
||||
last_status: resp&.status.to_i
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -33,6 +33,7 @@ class AccountConfig < ApplicationRecord
|
||||
FORM_COMPLETED_MESSAGE_KEY = 'form_completed_message'
|
||||
FORM_WITH_CONFETTI_KEY = 'form_with_confetti'
|
||||
ESIGNING_PREFERENCE_KEY = 'esigning_preference'
|
||||
WEBHOOK_PREFERENCES_KEY = 'webhook_preferences'
|
||||
|
||||
DEFAULT_VALUES = {
|
||||
SUBMITTER_INVITATION_EMAIL_KEY => {
|
||||
|
||||
@@ -14,6 +14,23 @@
|
||||
<%= f.button button_title(title: 'Save', disabled_with: 'Saving'), class: 'base-button w-full md:w-32' %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% preference = current_account.account_configs.find_by(key: AccountConfig::WEBHOOK_PREFERENCES_KEY)&.value || {} %>
|
||||
<div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 mt-1 gap-y-2">
|
||||
<% WebhookPreferencesController::EVENTS.each do |event| %>
|
||||
<%= form_for '', url: webhook_preferences_path, method: :post do |f| %>
|
||||
<%= f.hidden_field :event, value: event %>
|
||||
<% uuid = SecureRandom.uuid %>
|
||||
<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 %>
|
||||
<span>
|
||||
<%= event %>
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last %>
|
||||
|
||||
@@ -59,6 +59,7 @@ Rails.application.routes.draw do
|
||||
resources :testing_api_settings, only: %i[index]
|
||||
resources :submitters_autocomplete, only: %i[index]
|
||||
resources :template_folders_autocomplete, only: %i[index]
|
||||
resources :webhook_preferences, only: %i[create]
|
||||
resource :templates_upload, only: %i[create]
|
||||
authenticated do
|
||||
resource :templates_upload, only: %i[show], path: 'new'
|
||||
|
||||
+12
-2
@@ -67,14 +67,24 @@ module Accounts
|
||||
new_template
|
||||
end
|
||||
|
||||
def load_webhook_configs(account)
|
||||
def load_webhook_url(account)
|
||||
configs = account.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
||||
|
||||
unless Docuseal.multitenant?
|
||||
configs ||= Account.order(:id).first.encrypted_configs.find_by(key: EncryptedConfig::WEBHOOK_URL_KEY)
|
||||
end
|
||||
|
||||
configs
|
||||
configs&.value.presence
|
||||
end
|
||||
|
||||
def load_webhook_preferences(account)
|
||||
configs = account.account_configs.find_by(key: AccountConfig::WEBHOOK_PREFERENCES_KEY)
|
||||
|
||||
unless Docuseal.multitenant?
|
||||
configs ||= Account.order(:id).first.account_configs.find_by(key: AccountConfig::WEBHOOK_PREFERENCES_KEY)
|
||||
end
|
||||
|
||||
configs&.value.presence || {}
|
||||
end
|
||||
|
||||
def load_signing_pkcs(account)
|
||||
|
||||
Reference in New Issue
Block a user