refactor: reduce codebase and centralize classes

pull/648/head
moltenhub-bot 1 day ago
parent 744d45d2c5
commit 237a0c0ea7

@ -1,42 +1,16 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendFormCompletedWebhookRequestJob class SendFormCompletedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 12 MAX_ATTEMPTS = 12
def perform(params = {}) def perform(params = {})
submitter = Submitter.find_by(id: params['submitter_id']) perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
event_type: 'form.completed') do |submitter|
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) Submissions::EnsureResultGenerated.call(submitter)
ActiveStorage::Current.url_options = Docuseal.default_url_options ActiveStorage::Current.url_options = Docuseal.default_url_options
Submitters::SerializeForWebhook.call(submitter)
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
})
end end
end end
end end

@ -1,40 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendFormDeclinedWebhookRequestJob class SendFormDeclinedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submitter = Submitter.find_by(id: params['submitter_id']) perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
event_type: 'form.declined') do |submitter|
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 ActiveStorage::Current.url_options = Docuseal.default_url_options
Submitters::SerializeForWebhook.call(submitter)
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
})
end end
end end
end end

@ -1,40 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendFormStartedWebhookRequestJob class SendFormStartedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submitter = Submitter.find_by(id: params['submitter_id']) perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
event_type: 'form.started') do |submitter|
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 ActiveStorage::Current.url_options = Docuseal.default_url_options
Submitters::SerializeForWebhook.call(submitter)
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
})
end end
end end
end end

@ -1,40 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendFormViewedWebhookRequestJob class SendFormViewedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submitter = Submitter.find_by(id: params['submitter_id']) perform_webhook_request(params, record_key: 'submitter_id', record_class: Submitter,
event_type: 'form.viewed') do |submitter|
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 ActiveStorage::Current.url_options = Docuseal.default_url_options
Submitters::SerializeForWebhook.call(submitter)
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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendSubmissionArchivedWebhookRequestJob class SendSubmissionArchivedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submission = Submission.find_by(id: params['submission_id']) perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
event_type: 'submission.archived') do |submission|
return unless submission submission.as_json(only: %i[id archived_at])
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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendSubmissionCompletedWebhookRequestJob class SendSubmissionCompletedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submission = Submission.find_by(id: params['submission_id']) perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
event_type: 'submission.completed') do |submission|
return unless submission Submissions::SerializeForApi.call(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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendSubmissionCreatedWebhookRequestJob class SendSubmissionCreatedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submission = Submission.find_by(id: params['submission_id']) perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
event_type: 'submission.created') do |submission|
return unless submission Submissions::SerializeForApi.call(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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendSubmissionExpiredWebhookRequestJob class SendSubmissionExpiredWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
submission = Submission.find_by(id: params['submission_id']) perform_webhook_request(params, record_key: 'submission_id', record_class: Submission,
event_type: 'submission.expired') do |submission|
return unless submission Submissions::SerializeForApi.call(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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendTemplateArchivedWebhookRequestJob class SendTemplateArchivedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
template = Template.find_by(id: params['template_id']) perform_webhook_request(params, record_key: 'template_id', record_class: Template,
event_type: 'template.archived') do |template|
return unless template template.as_json(only: %i[id archived_at])
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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendTemplateCreatedWebhookRequestJob class SendTemplateCreatedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
template = Template.find_by(id: params['template_id']) perform_webhook_request(params, record_key: 'template_id', record_class: Template,
event_type: 'template.created') do |template|
return unless template Templates::SerializeForApi.call(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
})
end end
end end
end end

@ -1,38 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class SendTemplateUpdatedWebhookRequestJob class SendTemplateUpdatedWebhookRequestJob
include Sidekiq::Job include WebhookRequestJob
sidekiq_options queue: :webhooks
MAX_ATTEMPTS = 10
def perform(params = {}) def perform(params = {})
template = Template.find_by(id: params['template_id']) perform_webhook_request(params, record_key: 'template_id', record_class: Template,
event_type: 'template.updated') do |template|
return unless template Templates::SerializeForApi.call(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
})
end end
end 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…
Cancel
Save