~singpolyma/sgx-jmp

15100634c1fddfa296090193e452d5fdce45f660 — Stephen Paul Weber 10 months ago 7b6c0a0
After SIP reset, offer to change inbound fwd
6 files changed, 51 insertions(+), 17 deletions(-)

M lib/backend_sgx.rb
M lib/customer.rb
M lib/registration.rb
M lib/sip_account.rb
M sgx_jmp.rb
M test/test_registration.rb
M lib/backend_sgx.rb => lib/backend_sgx.rb +7 -1
@@ 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

M lib/customer.rb => lib/customer.rb +1 -1
@@ 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(

M lib/registration.rb => lib/registration.rb +3 -3
@@ 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}")

M lib/sip_account.rb => lib/sip_account.rb +7 -3
@@ 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(

M sgx_jmp.rb => sgx_jmp.rb +25 -3
@@ 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))

M test/test_registration.rb => test/test_registration.rb +8 -6
@@ 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"]
				)