add webhook events
This commit is contained in:
committed by
Pete Matsyburka
parent
1b60b42428
commit
988a5361a6
@@ -1,6 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module WebhookUrls
|
||||
EVENT_TYPE_TO_JOB_CLASS = {
|
||||
'form.started' => SendFormStartedWebhookRequestJob,
|
||||
'form.completed' => SendFormCompletedWebhookRequestJob,
|
||||
'form.declined' => SendFormDeclinedWebhookRequestJob,
|
||||
'form.viewed' => SendFormViewedWebhookRequestJob,
|
||||
'submission.created' => SendSubmissionCreatedWebhookRequestJob,
|
||||
'submission.completed' => SendSubmissionCompletedWebhookRequestJob,
|
||||
'submission.expired' => SendSubmissionExpiredWebhookRequestJob,
|
||||
'submission.archived' => SendSubmissionArchivedWebhookRequestJob,
|
||||
'template.created' => SendTemplateCreatedWebhookRequestJob,
|
||||
'template.updated' => SendTemplateUpdatedWebhookRequestJob
|
||||
}.freeze
|
||||
|
||||
EVENT_TYPE_ID_KEYS = {
|
||||
'form' => 'submitter_id',
|
||||
'submission' => 'submission_id',
|
||||
'template' => 'template_id'
|
||||
}.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def for_account_id(account_id, events)
|
||||
@@ -24,4 +43,26 @@ module WebhookUrls
|
||||
(account_urls.present? ? WebhookUrl.none : linked_urls)
|
||||
end
|
||||
end
|
||||
|
||||
def enqueue_events(records, event_type)
|
||||
args = []
|
||||
|
||||
id_key = EVENT_TYPE_ID_KEYS.fetch(event_type.split('.').first)
|
||||
|
||||
Array.wrap(records).group_by(&:account_id).each do |account_id, account_records|
|
||||
webhook_urls = for_account_id(account_id, event_type)
|
||||
|
||||
account_records.each do |record|
|
||||
event_uuid = SecureRandom.uuid
|
||||
|
||||
webhook_urls.each do |webhook_url|
|
||||
next unless webhook_url.events.include?(event_type)
|
||||
|
||||
args << [{ id_key => record.id, 'webhook_url_id' => webhook_url.id, 'event_uuid' => event_uuid }]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Sidekiq::Client.push_bulk('class' => EVENT_TYPE_TO_JOB_CLASS[event_type], 'args' => args)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user