Files
docuseal/app/jobs/send_form_completed_webhook_request_job.rb
T

48 lines
1.6 KiB
Ruby
Raw Normal View History

2023-08-05 15:25:16 +03:00
# frozen_string_literal: true
2024-06-18 16:51:42 +03:00
class SendFormCompletedWebhookRequestJob
include Sidekiq::Job
sidekiq_options queue: :webhooks
2024-05-27 14:35:19 +03:00
2024-11-03 10:19:59 +02:00
USER_AGENT = 'DocuSeal.com Webhook'
2023-08-05 15:25:16 +03:00
2024-12-28 16:58:20 +02:00
MAX_ATTEMPTS = 20
2024-02-09 23:04:42 +02:00
2024-06-22 10:36:32 +03:00
def perform(params = {})
submitter = Submitter.find(params['submitter_id'])
2024-11-01 15:35:02 +02:00
webhook_url = WebhookUrl.find(params['webhook_url_id'])
2024-06-22 10:36:32 +03:00
attempt = params['attempt'].to_i
2023-08-05 15:25:16 +03:00
return if webhook_url.url.blank? || webhook_url.events.exclude?('form.completed')
2023-08-05 15:25:16 +03:00
Submissions::EnsureResultGenerated.call(submitter)
ActiveStorage::Current.url_options = Docuseal.default_url_options
2024-02-24 20:24:43 +02:00
resp = begin
Faraday.post(webhook_url.url,
2024-02-24 20:24:43 +02:00
{
event_type: 'form.completed',
timestamp: Time.current,
data: Submitters::SerializeForWebhook.call(submitter)
}.to_json,
'Content-Type' => 'application/json',
2024-11-06 13:44:55 +02:00
'User-Agent' => USER_AGENT,
**webhook_url.secret.to_h)
2024-03-01 18:51:11 +02:00
rescue Faraday::Error
2024-02-24 20:24:43 +02:00
nil
end
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))
2024-06-18 16:51:42 +03:00
SendFormCompletedWebhookRequestJob.perform_in((2**attempt).minutes, {
2024-07-14 21:38:24 +03:00
**params,
2024-06-18 16:51:42 +03:00
'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