2023-08-05 15:25:16 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class WebhookSettingsController < ApplicationController
|
2024-11-09 01:32:56 +02:00
|
|
|
load_and_authorize_resource :webhook_url, parent: false, only: %i[index show new create update destroy]
|
|
|
|
|
load_and_authorize_resource :webhook_url, only: %i[resend], id_param: :webhook_id
|
2023-08-05 15:25:16 +03:00
|
|
|
|
2024-11-08 20:14:43 +02:00
|
|
|
def index
|
2024-11-09 01:32:56 +02:00
|
|
|
@webhook_urls = @webhook_urls.order(id: :desc)
|
2024-11-08 20:14:43 +02:00
|
|
|
@webhook_url = @webhook_urls.first_or_initialize
|
|
|
|
|
|
2025-07-04 21:11:08 +03:00
|
|
|
if @webhook_urls.size > 1
|
|
|
|
|
render :index
|
|
|
|
|
else
|
|
|
|
|
@webhook_events = @webhook_url.webhook_events
|
|
|
|
|
|
|
|
|
|
@webhook_events = @webhook_events.where(status: params[:status]) if %w[success error].include?(params[:status])
|
|
|
|
|
|
2026-01-26 18:18:07 +02:00
|
|
|
@pagy, @webhook_events = pagy(:countless, @webhook_events.order(id: :desc))
|
2025-07-04 21:11:08 +03:00
|
|
|
|
|
|
|
|
render :show
|
|
|
|
|
end
|
2024-11-08 20:14:43 +02:00
|
|
|
end
|
|
|
|
|
|
2025-07-04 21:11:08 +03:00
|
|
|
def show
|
|
|
|
|
@webhook_events = @webhook_url.webhook_events
|
|
|
|
|
|
|
|
|
|
@webhook_events = @webhook_events.where(status: params[:status]) if %w[success error].include?(params[:status])
|
|
|
|
|
|
2026-01-26 18:18:07 +02:00
|
|
|
@pagy, @webhook_events = pagy(:countless, @webhook_events.order(id: :desc))
|
2025-07-04 21:11:08 +03:00
|
|
|
end
|
2023-08-05 15:25:16 +03:00
|
|
|
|
2024-11-09 01:32:56 +02:00
|
|
|
def new; end
|
2024-11-08 20:14:43 +02:00
|
|
|
|
2023-09-17 00:45:57 +03:00
|
|
|
def create
|
2026-04-16 09:00:43 +03:00
|
|
|
if @webhook_url.url.present?
|
|
|
|
|
@webhook_url.save!
|
2023-08-05 15:25:16 +03:00
|
|
|
|
2026-04-16 09:00:43 +03:00
|
|
|
redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_saved')
|
|
|
|
|
else
|
|
|
|
|
redirect_back fallback_location: settings_webhooks_path
|
|
|
|
|
end
|
2023-08-05 15:25:16 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2024-11-09 01:32:56 +02:00
|
|
|
@webhook_url.update!(update_params)
|
2024-11-08 20:14:43 +02:00
|
|
|
|
2024-11-09 01:32:56 +02:00
|
|
|
redirect_back(fallback_location: settings_webhook_path(@webhook_url),
|
|
|
|
|
notice: I18n.t('webhook_url_has_been_updated'))
|
2024-11-08 20:14:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
2024-11-09 01:32:56 +02:00
|
|
|
@webhook_url.destroy!
|
2024-11-08 20:14:43 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2025-07-05 10:27:35 +03:00
|
|
|
authorize!(:read, submitter)
|
|
|
|
|
|
2024-11-09 01:32:56 +02:00
|
|
|
if submitter.blank? || @webhook_url.blank?
|
2024-11-08 20:14:43 +02:00
|
|
|
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
|
|
|
|
2025-07-04 21:11:08 +03:00
|
|
|
SendTestWebhookRequestJob.perform_async(
|
|
|
|
|
'submitter_id' => submitter.id,
|
|
|
|
|
'event_uuid' => SecureRandom.uuid,
|
|
|
|
|
'webhook_url_id' => @webhook_url.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
|
|
|
|
|
|
2024-11-09 01:32:56 +02:00
|
|
|
def create_params
|
2024-11-08 20:14:43 +02:00
|
|
|
params.require(:webhook_url).permit(:url, events: []).reverse_merge(events: [])
|
2023-09-17 00:45:57 +03:00
|
|
|
end
|
|
|
|
|
|
2024-11-09 01:32:56 +02:00
|
|
|
def update_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
|