adjust webhooks controller

pull/382/head
Pete Matsyburka 12 months ago
parent fd3530ac62
commit ca7ab5aab7

@ -1,11 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
class WebhookSettingsController < ApplicationController class WebhookSettingsController < ApplicationController
before_action :load_webhook_url, only: %i[show update destroy] load_and_authorize_resource :webhook_url, parent: false, only: %i[index show new create update destroy]
authorize_resource :webhook_url, parent: false load_and_authorize_resource :webhook_url, only: %i[resend], id_param: :webhook_id
def index def index
@webhook_urls = current_account.webhook_urls.order(id: :desc) @webhook_urls = @webhook_urls.order(id: :desc)
@webhook_url = @webhook_urls.first_or_initialize @webhook_url = @webhook_urls.first_or_initialize
render @webhook_urls.size > 1 ? 'index' : 'show' render @webhook_urls.size > 1 ? 'index' : 'show'
@ -13,60 +13,48 @@ class WebhookSettingsController < ApplicationController
def show; end def show; end
def new def new; end
@webhook_url = current_account.webhook_urls.build
end
def create def create
@webhook_url = current_account.webhook_urls.build(create_webhook_params)
@webhook_url.save! @webhook_url.save!
redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_saved') redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_saved')
end end
def update def update
@webhook_url.update!(update_webhook_params) @webhook_url.update!(update_params)
redirect_to settings_webhook_path(@webhook_url), notice: I18n.t('webhook_url_has_been_updated') redirect_back(fallback_location: settings_webhook_path(@webhook_url),
notice: I18n.t('webhook_url_has_been_updated'))
end end
def destroy def destroy
@webhook_url.delete @webhook_url.destroy!
redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_deleted') redirect_to settings_webhooks_path, notice: I18n.t('webhook_url_has_been_deleted')
end end
def resend def resend
submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last submitter = current_account.submitters.where.not(completed_at: nil).order(:id).last
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? if submitter.blank? || @webhook_url.blank?
return redirect_back(fallback_location: settings_webhooks_path, return redirect_back(fallback_location: settings_webhooks_path,
alert: I18n.t('unable_to_resend_webhook_request')) alert: I18n.t('unable_to_resend_webhook_request'))
end end
SendFormCompletedWebhookRequestJob.perform_async('submitter_id' => submitter.id, SendFormCompletedWebhookRequestJob.perform_async('submitter_id' => submitter.id,
'webhook_url_id' => webhook.id) 'webhook_url_id' => @webhook_url.id)
redirect_back(fallback_location: settings_webhooks_path, notice: I18n.t('webhook_request_has_been_sent')) redirect_back(fallback_location: settings_webhooks_path, notice: I18n.t('webhook_request_has_been_sent'))
end end
private private
def load_webhook_url def create_params
@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: []) params.require(:webhook_url).permit(:url, events: []).reverse_merge(events: [])
end end
def update_webhook_params def update_params
params.require(:webhook_url).permit(:url) params.require(:webhook_url).permit(:url)
end end
end end

@ -33,7 +33,7 @@
</div> </div>
<% end %> <% end %>
</div> </div>
<%= form_for @webhook_url, url: @webhook_url.persisted? ? settings_webhook_path(@webhook_url) : settings_webhooks_path, html: { autocomplete: 'off' }, data: { turbo: false } do |f| %> <%= form_for @webhook_url, url: @webhook_url.persisted? ? settings_webhook_path(@webhook_url) : settings_webhooks_path, html: { autocomplete: 'off' } do |f| %>
<div class="flex flex-row flex-wrap space-y-2 md:space-y-0 md:flex-nowrap md:space-x-2"> <div class="flex flex-row flex-wrap space-y-2 md:space-y-0 md:flex-nowrap md:space-x-2">
<%= f.url_field :url, class: 'input font-mono input-bordered w-full', placeholder: 'https://example.com/hook', required: true %> <%= f.url_field :url, class: 'input font-mono input-bordered w-full', placeholder: 'https://example.com/hook', required: true %>
<%= f.button button_title(title: t('save'), disabled_with: t('saving')), class: 'base-button w-full md:w-32' %> <%= f.button button_title(title: t('save'), disabled_with: t('saving')), class: 'base-button w-full md:w-32' %>

@ -168,7 +168,7 @@ Rails.application.routes.draw do
defaults: { status: :integration } defaults: { status: :integration }
resource :personalization, only: %i[show create], controller: 'personalization_settings' resource :personalization, only: %i[show create], controller: 'personalization_settings'
resources :api, only: %i[index create], controller: 'api_settings' resources :api, only: %i[index create], controller: 'api_settings'
resources :webhooks, except: %i[edit], controller: 'webhook_settings' do resources :webhooks, only: %i[index show new create update destroy], controller: 'webhook_settings' do
post :resend post :resend
end end
resource :account, only: %i[show update destroy] resource :account, only: %i[show update destroy]

@ -86,7 +86,7 @@ RSpec.describe 'Webhook Settings' do
expect(webhook_url.url).to eq('https://example.org/webhook') expect(webhook_url.url).to eq('https://example.org/webhook')
expect(page).to have_content('Webhook URL has been updated.') expect(page).to have_content('Webhook URL has been updated.')
expect(page.current_path).to eq(settings_webhook_path(webhook_url)) expect(page.current_path).to eq(settings_webhooks_path)
end end
it 'deletes the webhook' do it 'deletes the webhook' do
@ -185,17 +185,5 @@ RSpec.describe 'Webhook Settings' do
expect(args['submitter_id']).to eq(submitter.id) expect(args['submitter_id']).to eq(submitter.id)
expect(page).to have_content('Webhook request has been sent.') expect(page).to have_content('Webhook request has been sent.')
end end
it "doesn't resend the webhook request when the webhook is doesn't exist" do
visit settings_webhooks_path
webhook_url.destroy
expect do
click_button 'Test Webhook'
end.not_to change(SendFormCompletedWebhookRequestJob.jobs, :size)
expect(page).to have_content('Unable to resend webhook request.')
end
end end
end end

Loading…
Cancel
Save