Move Bandwidth Tn remote operations to BandwidthTnRepo

Stephen Paul Weber created

Change summary

lib/bandwidth_tn_repo.rb   | 21 +++++++++++++++++++++
lib/customer.rb            | 11 +++--------
lib/customer_repo.rb       | 24 +++++++++++++-----------
test/test_customer_info.rb |  4 ----
test/test_customer_repo.rb |  1 -
5 files changed, 37 insertions(+), 24 deletions(-)

Detailed changes

lib/bandwidth_tn_repo.rb 🔗

@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+require "ruby-bandwidth-iris"
+
+class BandwidthTnRepo
+	def find(tel)
+		BandwidthIris::Tn.new(telephone_number: tel).get_details
+	end
+
+	def put_lidb_name(tel, lidb_name)
+		BandwidthIris::Lidb.create(
+			lidb_tn_groups: { lidb_tn_group: {
+				telephone_numbers: [tel.sub(/\A\+1/, "")],
+				subscriber_information: lidb_name,
+				use_type: "RESIDENTIAL", visibility: "PUBLIC"
+			} }
+		)
+	rescue BandwidthIris::Errors::GenericError
+		raise "Could not set CNAM, please contact support"
+	end
+end

lib/customer.rb 🔗

@@ -19,7 +19,7 @@ require_relative "./trivial_backend_sgx_repo"
 class Customer
 	extend Forwardable
 
-	attr_reader :customer_id, :balance, :jid
+	attr_reader :customer_id, :balance, :jid, :tndetails
 
 	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
 	               :currency, :merchant_account, :plan_name, :auto_top_up_amount
@@ -32,6 +32,7 @@ class Customer
 		jid,
 		plan: CustomerPlan.new(customer_id),
 		balance: BigDecimal(0),
+		tndetails: {},
 		sgx: TrivialBackendSgxRepo.new.get(customer_id)
 	)
 		@plan = plan
@@ -39,6 +40,7 @@ class Customer
 		@customer_id = customer_id
 		@jid = jid
 		@balance = balance
+		@tndetails = tndetails
 		@sgx = sgx
 	end
 
@@ -84,13 +86,6 @@ class Customer
 		stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
 	end
 
-	def tndetails
-		return unless registered?
-
-		@tndetails ||=
-			BandwidthIris::Tn.new(telephone_number: registered?.phone).get_details
-	end
-
 	def ogm(from_tel=nil)
 		CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(from_tel) })
 	end

lib/customer_repo.rb 🔗

@@ -3,6 +3,7 @@
 require "lazy_object"
 require "value_semantics/monkey_patched"
 
+require_relative "bandwidth_tn_repo"
 require_relative "customer"
 require_relative "polyfill"
 
@@ -14,6 +15,7 @@ class CustomerRepo
 		db                Anything(), default: LazyObject.new { DB }
 		braintree         Anything(), default: LazyObject.new { BRAINTREE }
 		sgx_repo          Anything(), default: TrivialBackendSgxRepo.new
+		bandwidth_tn_repo Anything(), default: BandwidthTnRepo.new
 	end
 
 	def find(customer_id)
@@ -60,16 +62,7 @@ class CustomerRepo
 	end
 
 	def put_lidb_name(customer, lidb_name)
-		BandwidthIris::Lidb.create(
-			customer_order_id: customer.customer_id,
-			lidb_tn_groups: { lidb_tn_group: {
-				telephone_numbers: [customer.registered?.phone.sub(/\A\+1/, "")],
-				subscriber_information: lidb_name,
-				use_type: "RESIDENTIAL", visibility: "PUBLIC"
-			} }
-		)
-	rescue BandwidthIris::Errors::GenericError
-		raise "Could not set CNAM, please contact support"
+		@bandwidth_tn_repo.put_lidb_name(customer.registered?.phone, lidb_name)
 	end
 
 	def put_transcription_enabled(customer, transcription_enabled)
@@ -105,13 +98,22 @@ protected
 		WHERE customer_id=$1 LIMIT 1
 	SQL
 
+	def tndetails(sgx)
+		return unless sgx.registered?
+
+		LazyObject.new { @bandwidth_tn_repo.find(sgx.registered?.phone) }
+	end
+
 	def find_inner(customer_id, jid)
 		result = @db.query_defer(SQL, [customer_id])
 		EMPromise.all([@sgx_repo.get(customer_id), result]).then do |(sgx, rows)|
 			data = hydrate_plan(
 				customer_id, rows.first&.transform_keys(&:to_sym) || {}
 			)
-			Customer.new(customer_id, Blather::JID.new(jid), sgx: sgx, **data)
+			Customer.new(
+				customer_id, Blather::JID.new(jid),
+				sgx: sgx, tndetails: tndetails(sgx), **data
+			)
 		end
 	end
 end

test/test_customer_info.rb 🔗

@@ -18,7 +18,6 @@ class CustomerInfoTest < Minitest::Test
 	def test_info_does_not_crash
 		sgx = Minitest::Mock.new
 		sgx.expect(:registered?, false)
-		sgx.expect(:registered?, false)
 
 		CustomerPlan::REDIS.expect(
 			:get,
@@ -42,7 +41,6 @@ class CustomerInfoTest < Minitest::Test
 	def test_admin_info_does_not_crash
 		sgx = Minitest::Mock.new
 		sgx.expect(:registered?, false)
-		sgx.expect(:registered?, false)
 		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
 		sgx.expect(:fwd, fwd)
 
@@ -67,7 +65,6 @@ class CustomerInfoTest < Minitest::Test
 	def test_inactive_info_does_not_crash
 		sgx = Minitest::Mock.new
 		sgx.expect(:registered?, false)
-		sgx.expect(:registered?, false)
 
 		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
 		cust = Customer.new(
@@ -84,7 +81,6 @@ class CustomerInfoTest < Minitest::Test
 	def test_inactive_admin_info_does_not_crash
 		sgx = Minitest::Mock.new
 		sgx.expect(:registered?, false)
-		sgx.expect(:registered?, false)
 		sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
 
 		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)

test/test_customer_repo.rb 🔗

@@ -153,7 +153,6 @@ class CustomerRepoTest < Minitest::Test
 			:post,
 			"https://dashboard.bandwidth.com/v1.0/accounts//lidbs"
 		).with(body: {
-			CustomerOrderId: "test",
 			LidbTnGroups: {
 				LidbTnGroup: {
 					TelephoneNumbers: "5556667777",