After SIP reset, offer to change inbound fwd

Stephen Paul Weber created

Change summary

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(-)

Detailed changes

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

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(

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}")

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(

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))

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"]
 				)