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
|
|
|
|
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-06-22 10:36:32 +03:00
|
|
|
def perform(params = {})
|
|
|
|
|
submitter = Submitter.find(params['submitter_id'])
|
|
|
|
|
|
|
|
|
|
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.completed'] == false
|
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
|
2024-04-09 18:11:58 +03:00
|
|
|
Faraday.post(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',
|
|
|
|
|
'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
|
|
|
|
|
|
|
|
|
|
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, {
|
|
|
|
|
'submitter_id' => submitter.id,
|
|
|
|
|
'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
|