Skip to content

Commit

Permalink
Schedule a job to set the username
Browse files Browse the repository at this point in the history
- update the organization's signal_complete_onboarding_link with the username link in response
  • Loading branch information
mattwr18 committed Oct 16, 2024
1 parent 750c1a4 commit aafb8c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/organizations/signal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def verify
Rails.logger.debug JSON.parse(response.body)
case response
when Net::HTTPSuccess
SignalAdapter::SetTrustModeJob.perform_later(signal_server_phone_number: organization.signal_server_phone_number)
SignalAdapter::SetTrustModeJob.perform_later(signal_server_phone_number: @organization.signal_server_phone_number)
SignalAdapter::SetUsernameJob.perform_later(organization_id: @organization.id)
else
handle_error_response(response)
render :verify_form, status: :unprocessable_entity
Expand Down
39 changes: 39 additions & 0 deletions app/jobs/signal_adapter/set_username_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'net/http'

module SignalAdapter
class SetUsernameJob < ApplicationJob
def perform(organization_id:)
organization = Organization.find(organization_id)
uri = URI.parse("#{ENV.fetch('SIGNAL_CLI_REST_API_ENDPOINT', 'http://localhost:8080')}/v1/accounts/#{organization.signal_server_phone_number}/username")
request = Net::HTTP::Post.new(uri, {
Accept: 'application/json',
'Content-Type': 'application/json'
})
request.body = { username: organization.project_name.gsub(/\s+/, '').camelize }.to_json
response = Net::HTTP.start(uri.host, uri.port) do |http|
http.request(request)
end
case response
when Net::HTTPSuccess
organization.update!(signal_complete_onboarding_link: JSON.parse(response.body)['username_link'])
else
handle_error(JSON.parse(response.body)['error'])
end
end

private

def handle_error(error_message)
exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message)
context = {
code: response.code,
message: response.message,
headers: response.to_hash,
body: error_message
}
ErrorNotifier.report(exception, context: context)
end
end
end

0 comments on commit aafb8c7

Please sign in to comment.