Files
docuseal/app/controllers/webhook_settings_controller.rb
T

73 lines
2.1 KiB
Ruby
Raw Normal View History

2023-08-05 15:25:16 +03:00
# frozen_string_literal: true
class WebhookSettingsController < ApplicationController
2024-11-08 20:14:43 +02:00
before_action :load_webhook_url, only: %i[show update destroy]
authorize_resource :webhook_url, parent: false
2023-08-05 15:25:16 +03:00
2024-11-08 20:14:43 +02:00
def index
@webhook_urls = current_account.webhook_urls.order(id: :desc)
@webhook_url = @webhook_urls.first_or_initialize
render @webhook_urls.size > 1 ? 'index' : 'show'
end
2023-09-17 00:45:57 +03:00
def show; end
2023-08-05 15:25:16 +03:00
2024-11-08 20:14:43 +02:00
def new
@webhook_url = current_account.webhook_urls.build
end
2023-09-17 00:45:57 +03:00
def create
2024-11-08 20:14:43 +02:00
@webhook_url = current_account.webhook_urls.build(create_webhook_params)
2023-12-02 22:19:24 +02:00
2024-11-08 20:14:43 +02:00
@webhook_url.save!
2023-08-05 15:25:16 +03:00
2024-11-08 20:14:43 +02:00
redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_saved')
2023-08-05 15:25:16 +03:00
end
def update
2024-11-08 20:14:43 +02:00
@webhook_url.update!(update_webhook_params)
redirect_to settings_webhook_path(@webhook_url), notice: I18n.t('webhook_url_has_been_updated')
end
def destroy
@webhook_url.delete
redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_deleted')
end
def resend
2023-08-05 15:25:16 +03:00
submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last
2024-11-08 20:14:43 +02:00
webhook = current_account.webhook_urls
.where(Arel::Table.new(:webhook_urls)[:events].matches('%"form.completed"%'))
.find_by(id: params[:webhook_id])
if submitter.blank? || webhook.blank?
return redirect_back(fallback_location: settings_webhooks_path,
alert: I18n.t('unable_to_resend_webhook_request'))
end
2023-08-05 15:25:16 +03:00
2024-11-01 15:35:02 +02:00
SendFormCompletedWebhookRequestJob.perform_async('submitter_id' => submitter.id,
2024-11-08 20:14:43 +02:00
'webhook_url_id' => webhook.id)
2023-08-05 15:25:16 +03:00
2024-09-18 19:47:47 +03:00
redirect_back(fallback_location: settings_webhooks_path, notice: I18n.t('webhook_request_has_been_sent'))
2023-08-05 15:25:16 +03:00
end
private
def load_webhook_url
2024-11-08 20:14:43 +02:00
@webhook_url = current_account.webhook_urls.find_by(id: params[:id])
redirect_to settings_webhooks_path unless @webhook_url
end
def create_webhook_params
params.require(:webhook_url).permit(:url, events: []).reverse_merge(events: [])
2023-09-17 00:45:57 +03:00
end
2024-11-08 20:14:43 +02:00
def update_webhook_params
2024-11-01 15:35:02 +02:00
params.require(:webhook_url).permit(:url)
2023-08-05 15:25:16 +03:00
end
end