Files
docuseal/app/jobs/send_form_started_webhook_request_job.rb
T

45 lines
1.5 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 SendFormStartedWebhookRequestJob < ApplicationJob
2024-05-27 14:35:19 +03:00
queue_as :webhooks
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
2024-04-09 18:11:58 +03:00
url = Accounts.load_webhook_url(submitter.submission.account)
2023-08-05 15:25:16 +03:00
2024-04-09 18:11:58 +03:00
return if url.blank?
preferences = Accounts.load_webhook_preferences(submitter.submission.account)
return if preferences['form.started'] == false
2023-08-05 15:25:16 +03:00
ActiveStorage::Current.url_options = Docuseal.default_url_options
2024-02-24 20:24:43 +02:00
resp = begin
2024-04-09 18:11:58 +03:00
Faraday.post(url,
2024-02-24 20:24:43 +02:00
{
event_type: 'form.started',
timestamp: Time.current,
data: Submitters::SerializeForWebhook.call(submitter)
}.to_json,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
2024-03-01 18:51:11 +02:00
rescue Faraday::Error
2024-02-24 20:24:43 +02:00
nil
end
2024-02-09 23:04:42 +02:00
2024-02-24 20:24:43 +02:00
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
2024-02-16 18:41:58 +02:00
(!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan))
SendFormStartedWebhookRequestJob.set(wait: (2**attempt).minutes)
.perform_later(submitter, {
attempt: attempt + 1,
2024-02-24 20:24:43 +02:00
last_status: resp&.status.to_i
2024-02-16 18:41:58 +02:00
})
2024-02-09 23:04:42 +02:00
end
2023-08-05 15:25:16 +03:00
end
end