Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improvement] Improve create api key flow #2058

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@
%>

<% end %>

2 changes: 1 addition & 1 deletion app/components/whats_app_setup/whats_app_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(organization_id:)
private

def permissions_url
"https://hub.360dialog.com/dashboard/app/#{ENV.fetch('THREE_SIXTY_DIALOG_PARTNER_ID', '')}/permissions?redirect_url=#{CGI.escape(organization_whats_app_onboarding_successful_url(organization_id))}"
"https://hub.360dialog.com/dashboard/app/#{ENV.fetch('THREE_SIXTY_DIALOG_PARTNER_ID', '')}/permissions?redirect_url=#{CGI.escape(organization_whats_app_setup_successful_url(organization_id))}"
end
end
end
26 changes: 26 additions & 0 deletions app/controllers/whats_app/three_sixty_dialog/setup_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module WhatsApp
module ThreeSixtyDialog
class SetupController < ApplicationController
skip_before_action :require_login, :verify_authenticity_token, :user_permitted?
layout 'minimal'

def create_api_key
channel_ids = YAML.safe_load(create_api_key_params[:channels])
client_id = create_api_key_params[:client]
@organization.update!(three_sixty_dialog_client_id: client_id)
channel_ids.each do |channel_id|
WhatsAppAdapter::CreateApiKey.perform_later(organization_id: @organization.id, channel_id: channel_id)
end
render 'whats_app/setup/success'
end

private

def create_api_key_params
params.permit(:organization_id, :client, :channels, :revoked)
end
end
end
end
14 changes: 0 additions & 14 deletions app/controllers/whats_app/three_sixty_dialog_webhook_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ def message
WhatsAppAdapter::ThreeSixtyDialog::ProcessWebhookJob.perform_later(organization_id: @organization.id, components: @components)
end

def create_api_key
channel_ids = create_api_key_params[:channels].split('[').last.split(']').last.split(',')
client_id = create_api_key_params[:client]
organization.update!(three_sixty_dialog_client_id: client_id)
channel_ids.each do |channel_id|
WhatsAppAdapter::CreateApiKey.perform_later(organization_id: @organization.id, channel_id: channel_id)
end
render 'onboarding/success'
end

private

def extract_components
Expand Down Expand Up @@ -55,10 +45,6 @@ def message_params
}])
end

def create_api_key_params
params.permit(:client, :channels, :revoked)
end

def handle_error(error)
exception = WhatsAppAdapter::ThreeSixtyDialogError.new(error_code: error[:code], message: error[:title])
ErrorNotifier.report(exception, context: { details: error[:error_data][:details] })
Expand Down
5 changes: 0 additions & 5 deletions app/views/whats_app/onboarding/success.html.erb

This file was deleted.

5 changes: 5 additions & 0 deletions app/views/whats_app/setup/success.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= c 'onboarding_response',
style: :success,
heading: t('organization.whats_app_setup.success_heading'),
text: t('organization.whats_app_setup.success_text')
%>
3 changes: 3 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ de:
threema_unknown_content_message:
label: Fehler-Nachricht bei unbekannten Inhalten
help: Wenn ein Mitglied via Threema Nachrichten versendet, die von 100eyes noch nicht unterstützt werden, erhält es diese Nachricht.
whats_app_setup:
success_heading: WhatsApp wurde erfolgreich konfiguriert
success_text: Du kannst jetzt Mitglieder über WhatsApp einladen.

settings:
header: Einstellungen
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# TODO: Move each unscoped controller here if it has to be scoped by its organization

namespace :whats_app do
get '/onboarding-successful', to: 'three_sixty_dialog_webhook#create_api_key'
get '/setup-successful', to: 'three_sixty_dialog/setup#create_api_key'
post '/three-sixty-dialog-webhook', to: 'three_sixty_dialog_webhook#message'
end

Expand Down
31 changes: 31 additions & 0 deletions spec/requests/whats_app/three_sixty_dialog/setup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'WhatsApp 360dialog Setup' do
let(:organization) { create(:organization) }

describe 'GET /:organization_id/whats_app/setup-successful' do
subject { -> { get organization_whats_app_setup_successful_url(organization), params: params } }

let(:params) { { channels: '[valid_channel_id]', client: 'valid_client_id' } }

it 'updates the organization with the client id' do
expect { subject.call }.to (change { organization.reload.three_sixty_dialog_client_id }).from(nil).to('valid_client_id')
end

it 'schedules a job to create an api key for the client' do
expect { subject.call }.to have_enqueued_job(WhatsAppAdapter::CreateApiKey).with(
organization_id: organization.id,
channel_id: 'valid_channel_id'
)
end

it 'renders the setup success page' do
subject.call
expect(response).to be_successful
expect(page).to have_content('WhatsApp wurde erfolgreich konfiguriert')
expect(page).to have_content('Du kannst jetzt Mitglieder über WhatsApp einladen.')
end
end
end
Loading