Files
docuseal/app/jobs/send_template_created_webhook_request_job.rb
T

38 lines
1.3 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-05-12 11:37:33 +03:00
MAX_ATTEMPTS = 10
2024-06-22 10:36:32 +03:00
def perform(params = {})
2025-12-03 13:20:52 +02:00
template = Template.find_by(id: params['template_id'])
return unless template
webhook_url = WebhookUrl.find_by(id: params['webhook_url_id'])
return unless webhook_url
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 = SendWebhookRequest.call(webhook_url, event_type: 'template.created',
2025-07-04 21:11:08 +03:00
event_uuid: params['event_uuid'],
record: template,
attempt:,
data: Templates::SerializeForApi.call(template))
2024-05-12 11:37:33 +03:00
2026-05-06 19:09:35 +03:00
return if attempt > MAX_ATTEMPTS || (resp && resp.status.to_i < 400)
SendTemplateCreatedWebhookRequestJob.perform_in((2**attempt).minutes, {
**params,
'attempt' => attempt + 1,
'last_status' => resp&.status.to_i
})
2024-05-12 11:37:33 +03:00
end
end