From 15100634c1fddfa296090193e452d5fdce45f660 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 11 Oct 2021 15:22:25 -0500 Subject: [PATCH] After SIP reset, offer to change inbound fwd --- lib/backend_sgx.rb | 8 +++++++- lib/customer.rb | 2 +- lib/registration.rb | 6 +++--- lib/sip_account.rb | 10 +++++++--- sgx_jmp.rb | 28 +++++++++++++++++++++++++--- test/test_registration.rb | 14 ++++++++------ 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/lib/backend_sgx.rb b/lib/backend_sgx.rb index 3cbbc5f..9a0ee42 100644 --- a/lib/backend_sgx.rb +++ b/lib/backend_sgx.rb @@ -24,7 +24,9 @@ class BackendSgx ibr.username = creds[:username] ibr.password = creds[:password] ibr.phone = tel - IQ_MANAGER.write(ibr) + IQ_MANAGER.write(ibr).then do + with(registered?: irb) + end end def stanza(s) @@ -34,6 +36,10 @@ class BackendSgx end end + def set_fwd(uri) + REDIS.set("catapult_fwd-#{registered?.phone}", uri) + end + def set_fwd_timeout(timeout) REDIS.set("catapult_fwd_timeout-#{from_jid}", timeout) end diff --git a/lib/customer.rb b/lib/customer.rb index f47350d..71aebc0 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -23,7 +23,7 @@ class Customer def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan, :currency, :merchant_account, :plan_name, :auto_top_up_amount def_delegators :@sgx, :register!, :registered?, :set_ogm_url, - :set_fwd_timeout, :fwd, :transcription_enabled + :set_fwd, :fwd, :transcription_enabled def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage def initialize( diff --git a/lib/registration.rb b/lib/registration.rb index 96cace7..7b12c51 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -459,11 +459,11 @@ class Registration end def customer_active_tel_purchased - @customer.register!(@tel).catch(&method(:raise_setup_error)).then { + @customer.register!(@tel).catch(&method(:raise_setup_error)).then { |sgx| EMPromise.all([ REDIS.del("pending_tel_for-#{@customer.jid}"), - REDIS.set("catapult_fwd-#{@tel}", cheogram_sip_addr), - @customer.set_fwd_timeout(25) # ~5 seconds / ring, 5 rings + sgx.set_fwd(cheogram_sip_addr), + sgx.set_fwd_timeout(25) # ~5 seconds / ring, 5 rings ]) }.then do Command.finish("Your JMP account has been activated as #{@tel}") diff --git a/lib/sip_account.rb b/lib/sip_account.rb index 4c93c67..a6ad4a0 100644 --- a/lib/sip_account.rb +++ b/lib/sip_account.rb @@ -41,9 +41,9 @@ class SipAccount form.instructions = "These are your new SIP credentials" form.fields = [ - { var: "username", value: username, label: "Username" }, - { var: "password", value: @password, label: "Password" }, - { var: "server", value: server, label: "Server" } + { var: "username", value: username, label: "Username", type: :fixed }, + { var: "password", value: @password, label: "Password", type: :fixed }, + { var: "server", value: server, label: "Server", type: :fixed } ] form @@ -71,6 +71,10 @@ class SipAccount @api_object.realm end + def uri + "sip:#{username}@#{server}" + end + class New < SipAccount def put BandwidthIris::SipCredential.create( diff --git a/sgx_jmp.rb b/sgx_jmp.rb index ec4e7c4..7b80eb8 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -597,11 +597,33 @@ Command.new( Command.new( "reset sip account", - "Create or Reset SIP Account" + "Create or Reset SIP Account", + customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new) ) { Command.customer.then do |customer| - Command.finish do |reply| - reply.command << customer.reset_sip_account.form + sip_account = customer.reset_sip_account + Command.reply { |reply| + reply.allowed_actions = [:next] + form = sip_account.form + form.type = :form + form.fields += [{ + type: :boolean, var: "change_fwd", + label: "Should inbound calls forward to this SIP account?" + }] + reply.command << form + }.then do |fwd| + if ["1", "true"].include?(fwd.form.field("change_fwd")&.value.to_s) + # Migrate location if needed + BandwidthIris::SipPeer.new( + site_id: CONFIG[:bandwidth_site], + id: CONFIG[:bandwidth_peer] + ).move_tns([customer.registered?.phone]) + customer.set_fwd(sip_account.uri).then do + Command.finish("Inbound calls will now forward to SIP.") + end + else + Command.finish + end end end }.register(self).then(&CommandList.method(:register)) diff --git a/test/test_registration.rb b/test/test_registration.rb index b176a39..52b5e64 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -564,6 +564,11 @@ class RegistrationTest < Minitest::Test } ).to_return(status: 201) Registration::Finish::REDIS.expect( + :del, + nil, + ["pending_tel_for-test@example.net"] + ) + BackendSgx::REDIS.expect( :set, nil, [ @@ -571,11 +576,6 @@ class RegistrationTest < Minitest::Test "sip:test%40example.net@sip.cheogram.com" ] ) - Registration::Finish::REDIS.expect( - :del, - nil, - ["pending_tel_for-test@example.net"] - ) BackendSgx::REDIS.expect( :set, nil, @@ -597,7 +597,9 @@ class RegistrationTest < Minitest::Test execute_command(blather: blather) do @sgx.expect( :register!, - EMPromise.resolve(OpenStruct.new(error?: false)), + EMPromise.resolve(@sgx.with(registered?: IBR.new.tap do |ibr| + ibr.phone = "+15555550000" + end)), ["+15555550000"] ) -- 2.45.2