add submission.completed
This commit is contained in:
@@ -0,0 +1,52 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Api
|
||||||
|
class SubmissionEventsController < ApiBaseController
|
||||||
|
load_and_authorize_resource :submission, parent: false
|
||||||
|
|
||||||
|
def index
|
||||||
|
submissions = build_completed_query(@submissions)
|
||||||
|
|
||||||
|
params[:after] = Time.zone.at(params[:after].to_i) if params[:after].present?
|
||||||
|
params[:before] = Time.zone.at(params[:before].to_i) if params[:before].present?
|
||||||
|
|
||||||
|
submissions = paginate(submissions.preload(
|
||||||
|
:created_by_user, :submission_events,
|
||||||
|
template: :folder,
|
||||||
|
submitters: { documents_attachments: :blob, attachments_attachments: :blob },
|
||||||
|
audit_trail_attachment: :blob
|
||||||
|
),
|
||||||
|
field: :completed_at)
|
||||||
|
|
||||||
|
render json: {
|
||||||
|
data: submissions.map do |s|
|
||||||
|
{
|
||||||
|
event_type: 'submission.completed',
|
||||||
|
timestamp: s.completed_at,
|
||||||
|
data: Submissions::SerializeForApi.call(s, s.submitters)
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
pagination: {
|
||||||
|
count: submissions.size,
|
||||||
|
next: submissions.last&.completed_at&.to_i,
|
||||||
|
prev: submissions.first&.completed_at&.to_i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def build_completed_query(submissions)
|
||||||
|
submissions = submissions.where(
|
||||||
|
Submitter.where(completed_at: nil).where(
|
||||||
|
Submitter.arel_table[:submission_id].eq(Submission.arel_table[:id])
|
||||||
|
).select(1).arel.exists.not
|
||||||
|
)
|
||||||
|
|
||||||
|
submissions.joins(:submitters)
|
||||||
|
.group(:id)
|
||||||
|
.select(Submission.arel_table[Arel.star],
|
||||||
|
Submitter.arel_table[:completed_at].maximum.as('completed_at'))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -20,10 +20,10 @@ class ProcessSubmitterCompletionJob
|
|||||||
enqueue_completed_emails(submitter)
|
enqueue_completed_emails(submitter)
|
||||||
end
|
end
|
||||||
|
|
||||||
enqueue_completed_webhooks(submitter)
|
enqueue_completed_webhooks(submitter, is_all_completed:)
|
||||||
end
|
end
|
||||||
|
|
||||||
def enqueue_completed_webhooks(submitter)
|
def enqueue_completed_webhooks(submitter, is_all_completed: false)
|
||||||
webhook_config = Accounts.load_webhook_config(submitter.account)
|
webhook_config = Accounts.load_webhook_config(submitter.account)
|
||||||
|
|
||||||
if webhook_config
|
if webhook_config
|
||||||
@@ -31,13 +31,26 @@ class ProcessSubmitterCompletionJob
|
|||||||
'encrypted_config_id' => webhook_config.id })
|
'encrypted_config_id' => webhook_config.id })
|
||||||
end
|
end
|
||||||
|
|
||||||
webhook_ids = submitter.account.webhook_urls.where(
|
webhook_urls = submitter.account.webhook_urls
|
||||||
Arel::Table.new(:webhook_urls)[:events].matches('%"form.completed"%')
|
|
||||||
).pluck(:id)
|
|
||||||
|
|
||||||
webhook_ids.each do |webhook_id|
|
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|
|
||||||
|
if webhook.events.include?('form.completed')
|
||||||
SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
SendFormCompletedWebhookRequestJob.perform_async({ 'submitter_id' => submitter.id,
|
||||||
'webhook_url_id' => webhook_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 })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class SendSubmissionCompletedWebhookRequestJob
|
||||||
|
include Sidekiq::Job
|
||||||
|
|
||||||
|
sidekiq_options queue: :webhooks
|
||||||
|
|
||||||
|
USER_AGENT = 'DocuSeal.co Webhook'
|
||||||
|
|
||||||
|
MAX_ATTEMPTS = 10
|
||||||
|
|
||||||
|
def perform(params = {})
|
||||||
|
submission = Submission.find(params['submission_id'])
|
||||||
|
|
||||||
|
attempt = params['attempt'].to_i
|
||||||
|
|
||||||
|
webhook_url = submission.account.webhook_urls.find(params['webhook_url_id'])
|
||||||
|
|
||||||
|
url = webhook_url.url if webhook_url.events.include?('submission.completed')
|
||||||
|
|
||||||
|
return if url.blank?
|
||||||
|
|
||||||
|
resp = begin
|
||||||
|
Faraday.post(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,
|
||||||
|
'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? || submission.account.account_configs.exists?(key: :plan))
|
||||||
|
SendSubmissionCompletedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||||
|
**params,
|
||||||
|
'attempt' => attempt + 1,
|
||||||
|
'last_status' => resp&.status.to_i
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -49,6 +49,7 @@ Rails.application.routes.draw do
|
|||||||
end
|
end
|
||||||
scope 'events' do
|
scope 'events' do
|
||||||
resources :form_events, only: %i[index], path: 'form/:type'
|
resources :form_events, only: %i[index], path: 'form/:type'
|
||||||
|
resources :submission_events, only: %i[index], path: 'submission/:type'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user