Files
docuseal/app/jobs/send_template_created_webhook_request_job.rb
T

45 lines
1.6 KiB
Ruby
Raw Normal View History

2024-05-12 11:37:33 +03:00
# frozen_string_literal: true
2024-06-18 16:51:42 +03:00
class SendTemplateCreatedWebhookRequestJob
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'
2024-05-12 11:37:33 +03:00
MAX_ATTEMPTS = 10
2024-06-22 10:36:32 +03:00
def perform(params = {})
template = Template.find(params['template_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
2024-08-05 13:30:04 +03:00
return if webhook_url.url.blank? || webhook_url.events.exclude?('template.created')
2024-05-12 11:37:33 +03:00
resp = begin
Faraday.post(webhook_url.url,
2024-05-12 11:37:33 +03:00
{
event_type: 'template.created',
timestamp: Time.current,
data: Templates::SerializeForApi.call(template)
}.to_json,
**webhook_url.secret.to_h,
2024-05-12 11:37:33 +03:00
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
nil
end
if (resp.nil? || resp.status.to_i >= 400) && attempt <= MAX_ATTEMPTS &&
(!Docuseal.multitenant? || template.account.account_configs.exists?(key: :plan))
2024-06-18 16:51:42 +03:00
SendTemplateCreatedWebhookRequestJob.perform_in((2**attempt).minutes, {
'template_id' => template.id,
'webhook_url_id' => webhook_url.id,
2024-06-18 16:51:42 +03:00
'attempt' => attempt + 1,
'last_status' => resp&.status.to_i
})
2024-05-12 11:37:33 +03:00
end
end
end