mirror of https://github.com/docusealco/docuseal
parent
744d45d2c5
commit
237a0c0ea7
@ -1,42 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendFormCompletedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
include WebhookRequestJob
|
||||
|
||||
MAX_ATTEMPTS = 12
|
||||
|
||||
def perform(params = {})
|
||||
submitter = Submitter.find_by(id: params['submitter_id'])
|
||||
|
||||
return unless submitter
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
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 = SendWebhookRequest.call(webhook_url, event_type: 'form.completed',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submitter,
|
||||
attempt:,
|
||||
data: Submitters::SerializeForWebhook.call(submitter))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormCompletedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
|
||||
event_type: 'form.completed') do |submitter|
|
||||
Submissions::EnsureResultGenerated.call(submitter)
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
Submitters::SerializeForWebhook.call(submitter)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,40 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendFormDeclinedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submitter = Submitter.find_by(id: params['submitter_id'])
|
||||
|
||||
return unless submitter
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.declined')
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'form.declined',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submitter,
|
||||
attempt:,
|
||||
data: Submitters::SerializeForWebhook.call(submitter))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormDeclinedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
|
||||
event_type: 'form.declined') do |submitter|
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
Submitters::SerializeForWebhook.call(submitter)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,40 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendFormStartedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submitter = Submitter.find_by(id: params['submitter_id'])
|
||||
|
||||
return unless submitter
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.started')
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'form.started',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submitter,
|
||||
attempt:,
|
||||
data: Submitters::SerializeForWebhook.call(submitter))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormStartedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
|
||||
event_type: 'form.started') do |submitter|
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
Submitters::SerializeForWebhook.call(submitter)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,40 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendFormViewedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submitter = Submitter.find_by(id: params['submitter_id'])
|
||||
|
||||
return unless submitter
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.viewed')
|
||||
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'form.viewed',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submitter,
|
||||
attempt:,
|
||||
data: Submitters::SerializeForWebhook.call(submitter))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
|
||||
SendFormViewedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
|
||||
event_type: 'form.viewed') do |submitter|
|
||||
ActiveStorage::Current.url_options = Docuseal.default_url_options
|
||||
Submitters::SerializeForWebhook.call(submitter)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendSubmissionArchivedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submission = Submission.find_by(id: params['submission_id'])
|
||||
|
||||
return unless submission
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.archived')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'submission.archived',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submission,
|
||||
attempt:,
|
||||
data: submission.as_json(only: %i[id archived_at]))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submission.account.account_configs.exists?(key: :plan))
|
||||
SendSubmissionArchivedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
|
||||
event_type: 'submission.archived') do |submission|
|
||||
submission.as_json(only: %i[id archived_at])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendSubmissionCompletedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submission = Submission.find_by(id: params['submission_id'])
|
||||
|
||||
return unless submission
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.completed')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'submission.completed',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submission,
|
||||
attempt:,
|
||||
data: Submissions::SerializeForApi.call(submission))
|
||||
|
||||
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
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
|
||||
event_type: 'submission.completed') do |submission|
|
||||
Submissions::SerializeForApi.call(submission)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendSubmissionCreatedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submission = Submission.find_by(id: params['submission_id'])
|
||||
|
||||
return unless submission
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.created')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'submission.created',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submission,
|
||||
attempt:,
|
||||
data: Submissions::SerializeForApi.call(submission))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submission.account.account_configs.exists?(key: :plan))
|
||||
SendSubmissionCreatedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
|
||||
event_type: 'submission.created') do |submission|
|
||||
Submissions::SerializeForApi.call(submission)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendSubmissionExpiredWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
submission = Submission.find_by(id: params['submission_id'])
|
||||
|
||||
return unless submission
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('submission.expired')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'submission.expired',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: submission,
|
||||
attempt:,
|
||||
data: Submissions::SerializeForApi.call(submission))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || submission.account.account_configs.exists?(key: :plan))
|
||||
SendSubmissionExpiredWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
|
||||
event_type: 'submission.expired') do |submission|
|
||||
Submissions::SerializeForApi.call(submission)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendTemplateArchivedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
template = Template.find_by(id: params['template_id'])
|
||||
|
||||
return unless template
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('template.archived')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'template.archived',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: template,
|
||||
attempt:,
|
||||
data: template.as_json(only: %i[id archived_at]))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || template.account.account_configs.exists?(key: :plan))
|
||||
SendTemplateArchivedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'template_id', record_class: Template,
|
||||
event_type: 'template.archived') do |template|
|
||||
template.as_json(only: %i[id archived_at])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendTemplateCreatedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
template = Template.find_by(id: params['template_id'])
|
||||
|
||||
return unless template
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('template.created')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'template.created',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: template,
|
||||
attempt:,
|
||||
data: Templates::SerializeForApi.call(template))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || template.account.account_configs.exists?(key: :plan))
|
||||
SendTemplateCreatedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'template_id', record_class: Template,
|
||||
event_type: 'template.created') do |template|
|
||||
Templates::SerializeForApi.call(template)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,38 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendTemplateUpdatedWebhookRequestJob
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
|
||||
MAX_ATTEMPTS = 10
|
||||
include WebhookRequestJob
|
||||
|
||||
def perform(params = {})
|
||||
template = Template.find_by(id: params['template_id'])
|
||||
|
||||
return unless template
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?('template.updated')
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type: 'template.updated',
|
||||
event_uuid: params['event_uuid'],
|
||||
record: template,
|
||||
attempt:,
|
||||
data: Templates::SerializeForApi.call(template))
|
||||
|
||||
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
|
||||
(!Docuseal.multitenant? || template.account.account_configs.exists?(key: :plan))
|
||||
SendTemplateUpdatedWebhookRequestJob.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
perform_webhook_request(params, record_key: 'template_id', record_class: Template,
|
||||
event_type: 'template.updated') do |template|
|
||||
Templates::SerializeForApi.call(template)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module WebhookRequestJob
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
DEFAULT_MAX_ATTEMPTS = 10
|
||||
|
||||
included do
|
||||
include Sidekiq::Job
|
||||
|
||||
sidekiq_options queue: :webhooks
|
||||
end
|
||||
|
||||
def perform_webhook_request(params, record_key:, record_class:, event_type:)
|
||||
record = record_class.find_by(id: params[record_key])
|
||||
|
||||
return unless record
|
||||
|
||||
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
|
||||
|
||||
return unless webhook_url
|
||||
|
||||
attempt = params['attempt'].to_i
|
||||
|
||||
return if webhook_url.url.blank? || webhook_url.events.exclude?(event_type)
|
||||
|
||||
resp = SendWebhookRequest.call(webhook_url, event_type:,
|
||||
event_uuid: params['event_uuid'],
|
||||
record:,
|
||||
attempt:,
|
||||
data: yield(record))
|
||||
|
||||
retry_webhook_request(resp, record, params, attempt)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def retry_webhook_request(resp, record, params, attempt)
|
||||
return unless retry_webhook_request?(resp, record, attempt)
|
||||
|
||||
self.class.perform_in((2**attempt).minutes, {
|
||||
**params,
|
||||
'attempt' => attempt + 1,
|
||||
'last_status' => resp&.status.to_i
|
||||
})
|
||||
end
|
||||
|
||||
def retry_webhook_request?(resp, record, attempt)
|
||||
(resp.nil? || resp.status.to_i >= 400) &&
|
||||
attempt <= max_attempts &&
|
||||
(!Docuseal.multitenant? || record.account.account_configs.exists?(key: :plan))
|
||||
end
|
||||
|
||||
def max_attempts
|
||||
self.class.const_defined?(:MAX_ATTEMPTS, false) ? self.class::MAX_ATTEMPTS : DEFAULT_MAX_ATTEMPTS
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue