Skip to content

Commit

Permalink
Merge from docusealco/wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBTurchyn authored Nov 4, 2024
2 parents 88776e7 + cb6d24d commit 50acff2
Show file tree
Hide file tree
Showing 54 changed files with 1,294 additions and 288 deletions.
14 changes: 10 additions & 4 deletions app/controllers/api/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ def create

submissions = create_submissions(@template, params)

submissions.each do |submission|
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => submission.id })
WebhookUrls.for_account_id(@template.account_id, '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)

submissions.each do |submission|
submission.submitters.each do |submitter|
ProcessSubmitterCompletionJob.perform_async({ 'submitter_id' => submitter.id }) if submitter.completed_at?
ProcessSubmitterCompletionJob.perform_async('submitter_id' => submitter.id) if submitter.completed_at?
end
end

Expand All @@ -93,7 +96,10 @@ def destroy
else
@submission.update!(archived_at: Time.current)

SendSubmissionArchivedWebhookRequestJob.perform_async('submission_id' => @submission.id)
WebhookUrls.for_account_id(@submission.account_id, '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])
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/api/submitter_form_views_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def create

SubmissionEvents.create_with_tracking_data(submitter, 'view_form', request)

SendFormViewedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id })
WebhookUrls.for_account_id(submitter.account_id, 'form.viewed').each do |webhook_url|
SendFormViewedWebhookRequestJob.perform_async('submitter_id' => submitter.id,
'webhook_url_id' => webhook_url.id)
end

render json: {}
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/submitters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def update
end

if @submitter.completed_at?
ProcessSubmitterCompletionJob.perform_async({ 'submitter_id' => @submitter.id })
ProcessSubmitterCompletionJob.perform_async('submitter_id' => @submitter.id)
elsif normalized_params[:send_email] || normalized_params[:send_sms]
Submitters.send_signature_requests([@submitter])
end
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/api/templates_clone_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def create

schema_documents = Templates::CloneAttachments.call(template: cloned_template, original_template: @template)

SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => cloned_template.id)
WebhookUrls.for_account_id(cloned_template.account_id, '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
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/api/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def update

@template.update!(template_params)

SendTemplateUpdatedWebhookRequestJob.perform_async('template_id' => @template.id)
WebhookUrls.for_account_id(@template.account_id, '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
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/start_form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def update

if @submitter.save
if is_new_record
SendSubmissionCreatedWebhookRequestJob.perform_async({ 'submission_id' => @submitter.submission.id })
WebhookUrls.for_account_id(@submitter.account_id, '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)
Expand Down
18 changes: 14 additions & 4 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def create
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)

Expand All @@ -68,7 +66,10 @@ def destroy
else
@submission.update!(archived_at: Time.current)

SendSubmissionArchivedWebhookRequestJob.perform_async('submission_id' => @submission.id)
WebhookUrls.for_account_id(@submission.account_id, '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
Expand All @@ -85,6 +86,15 @@ def save_template_message(template, params)
template.save!
end

def enqueue_submission_created_webhooks(template, submissions)
WebhookUrls.for_account_id(template.account_id, '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
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/submit_form_decline_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def create
SubmitterMailer.declined_email(submitter, user).deliver_later!
end

SendFormDeclinedWebhookRequestJob.perform_async('submitter_id' => submitter.id)
WebhookUrls.for_account_id(submitter.account_id, '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
Expand Down
18 changes: 16 additions & 2 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def create
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
Expand All @@ -77,7 +77,7 @@ def create
def update
@template.update!(template_params)

SendTemplateUpdatedWebhookRequestJob.perform_async('template_id' => @template.id)
enqueue_template_updated_webhooks(@template)

head :ok
end
Expand Down Expand Up @@ -128,6 +128,20 @@ def maybe_redirect_to_template(template)
end
end

def enqueue_template_created_webhooks(template)
WebhookUrls.for_account_id(template.account_id, 'template.created').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)
WebhookUrls.for_account_id(template.account_id, '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?

Expand Down
9 changes: 8 additions & 1 deletion app/controllers/templates_uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create

@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
Expand Down Expand Up @@ -65,4 +65,11 @@ def create_file_params_from_url

{ files: [file] }
end

def enqueue_template_created_webhooks(template)
WebhookUrls.for_account_id(template.account_id, 'template.created').each do |webhook_url|
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => template.id,
'webhook_url_id' => webhook_url.id)
end
end
end
5 changes: 2 additions & 3 deletions app/controllers/testing_api_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 9 additions & 24 deletions app/controllers/webhook_preferences_controller.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
# 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
load_and_authorize_resource :webhook_url, parent: false

before_action :load_account_config
authorize_resource :account_config, parent: false
def update
webhook_preferences_params[:events].each do |event, val|
@webhook_url.events.delete(event) if val == '0'
@webhook_url.events.push(event) if val == '1' && @webhook_url.events.exclude?(event)
end

def create
@account_config.value[account_config_params[:event]] = account_config_params[:value] == '1'

@account_config.save!
@webhook_url.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)
def webhook_preferences_params
params.require(:webhook_url).permit(events: {})
end
end
22 changes: 7 additions & 15 deletions app/controllers/webhook_secret_controller.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 10 additions & 11 deletions app/controllers/webhook_settings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
# 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

def update
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 })
SendFormCompletedWebhookRequestJob.perform_async('submitter_id' => submitter.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)
end
end
27 changes: 5 additions & 22 deletions app/jobs/process_submitter_completion_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,15 @@ def create_completed_documents!(submitter)
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|
WebhookUrls.for_account_id(submitter.account_id, %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 })
SendFormCompletedWebhookRequestJob.perform_async('submitter_id' => submitter.id,
'webhook_url_id' => webhook.id)
end

if webhook.events.include?('submission.completed') && is_all_completed
SendSubmissionCompletedWebhookRequestJob.perform_async({ 'submission_id' => submitter.submission_id,
'webhook_url_id' => webhook.id })
SendSubmissionCompletedWebhookRequestJob.perform_async('submission_id' => submitter.submission_id,
'webhook_url_id' => webhook.id)
end
end
end
Expand Down
Loading

0 comments on commit 50acff2

Please sign in to comment.