Files
docuseal/app/jobs/send_form_viewed_webhook_request_job.rb
T

35 lines
1.3 KiB
Ruby
Raw Normal View History

2023-08-05 15:25:16 +03:00
# frozen_string_literal: true
2023-09-14 20:31:07 +03:00
class SendFormViewedWebhookRequestJob < ApplicationJob
2023-08-05 15:25:16 +03:00
USER_AGENT = 'DocuSeal.co Webhook'
2024-02-16 18:41:58 +02:00
MAX_ATTEMPTS = 10
2024-02-09 23:04:42 +02:00
2024-02-16 18:41:58 +02:00
def perform(submitter, params = {})
attempt = params[:attempt].to_i
2023-10-08 12:04:32 +03:00
config = Accounts.load_webhook_configs(submitter.submission.account)
2023-08-05 15:25:16 +03:00
return if config.blank? || config.value.blank?
ActiveStorage::Current.url_options = Docuseal.default_url_options
2024-02-09 23:04:42 +02:00
resp = Faraday.post(config.value,
{
event_type: 'form.viewed',
timestamp: Time.current,
data: Submitters::SerializeForWebhook.call(submitter)
}.to_json,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
2024-02-16 18:41:58 +02:00
if resp.status.to_i >= 400 && attempt <= MAX_ATTEMPTS &&
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
SendFormViewedWebhookRequestJob.set(wait: (2**attempt).minutes)
.perform_later(submitter, {
attempt: attempt + 1,
last_status: resp.status.to_i
})
2024-02-09 23:04:42 +02:00
end
2023-08-05 15:25:16 +03:00
end
end