insert number into tel_inventory on cancel

Phillip Davis created

instead of Bandwidth.move'ing it

Change summary

lib/admin_actions/cancel.rb |  8 ++++
lib/bandwidth_tn_repo.rb    | 31 ++++++++++++++++++---
test/test_admin_command.rb  | 55 ++++++++++++++++++++++++++++----------
3 files changed, 73 insertions(+), 21 deletions(-)

Detailed changes

lib/admin_actions/cancel.rb 🔗

@@ -2,11 +2,17 @@
 
 class AdminAction
 	class CancelCustomer
-		def self.call(customer, customer_repo:, **)
+		def self.cancel_stanza
 			m = Blather::Stanza::Message.new
 			m.from = CONFIG[:notify_from]
 			m.body = "Your JMP account has been cancelled."
+			m
+		end
+
+		def self.call(customer, customer_repo:, **)
 			raise "Customer not registered" unless customer.registered?
+
+			m = cancel_stanza
 			customer.stanza_to(m).then {
 				EMPromise.all([
 					Churnbuster.new.cancellation(customer),

lib/bandwidth_tn_repo.rb 🔗

@@ -3,6 +3,22 @@
 require "ruby-bandwidth-iris"
 
 class BandwidthTnRepo
+	STASH_QUERY = <<~SQL
+		INSERT INTO tel_inventory (
+			tel,
+			region,
+			locality,
+			bandwidth_account_id,
+			available_after
+		) VALUES (
+			$1,
+			$2,
+			$3,
+			$4,
+			LOCALTIMESTAMP + '1 MONTH'
+		)
+	SQL
+
 	def initialize
 		@move_client =
 			BandwidthIris::Client.new(
@@ -30,14 +46,19 @@ class BandwidthTnRepo
 		raise "Could not set CNAM, please contact support"
 	end
 
+	def stash_for_later(tel, btn)
+		details = btn.get_rate_center
+		region = details[:state]
+		locality = details[:rate_center]
+		params = [tel, region, locality, CONFIG[:creds][:account]]
+		DB.exec(STASH_QUERY, params)
+	end
+
 	def disconnect(tel, order_name)
 		tn = tel.sub(/\A\+1/, "")
+		btn = BandwidthIris::Tn.new({ telephone_number: tn }, @move_client)
 		if CONFIG[:keep_area_codes].find { |area| tn.start_with?(area) }
-			BandwidthIris::Tn.new({ telephone_number: tn }, @move_client).move(
-				customer_order_id: order_name,
-				source_account_id: CONFIG[:creds][:account],
-				**CONFIG[:keep_area_codes_in].slice(:site_id, :sip_peer_id)
-			)
+			stash_for_later(tel, btn)
 		else
 			BandwidthIris::Disconnect.create(order_name, tn)
 		end

test/test_admin_command.rb 🔗

@@ -8,6 +8,7 @@ AdminAction::LaunchSnikket::IQ_MANAGER = Minitest::Mock.new
 Customer::BLATHER = Minitest::Mock.new
 AdminActionRepo::REDIS = Minitest::Mock.new
 TrivialBackendSgxRepo::REDIS = Minitest::Mock.new
+BandwidthTnRepo::DB = Minitest::Mock.new
 
 class AdminCommandTest < Minitest::Test
 	def admin_command(tel="+15556667777")
@@ -248,7 +249,33 @@ class AdminCommandTest < Minitest::Test
 	em :test_action_cancel_account
 
 	def test_action_cancel_account_keep_number
-		req = stub_request(:post, "https://api.churnbuster.io/v1/cancellations")
+		rate_center_response = {
+			"TelephoneNumberDetails": {
+				City: "MANHATTAN",
+				State: "NY",
+				RateCenter: "NWYRCYZN01"
+			}
+		}.to_xml(indent: 0, root: "TelephoneNumberResponse")
+
+		bandwidth_req = stub_request(
+			:get,
+			"https://dashboard.bandwidth.com/v1.0/tns/5566667777/ratecenter"
+		)
+			.with(
+				headers: {
+					"Accept" => "application/xml",
+					"Accept-Encoding" => "gzip, compressed",
+					"Authorization" => "Basic dGVzdF9id191c2VyOnRlc3RfYndfcGFzc3dvcmQ=",
+					"User-Agent" => "Ruby-Bandwidth-Iris"
+				},
+				body: ""
+			)
+			.to_return(status: 200, body: rate_center_response, headers: {})
+
+		churnbuster_req = stub_request(
+			:post,
+			"https://api.churnbuster.io/v1/cancellations"
+		)
 			.with(
 				body: {
 					customer: {
@@ -267,6 +294,14 @@ class AdminCommandTest < Minitest::Test
 
 		sgx, admin = admin_command("+15566667777")
 
+		sql_params = ["+15566667777", "NY", "NWYRCYZN01", "test_bw_account"]
+
+		BandwidthTnRepo::DB.expect(
+			:exec,
+			EMPromise.resolve(nil),
+			[String, sql_params]
+		)
+
 		Customer::BLATHER.expect(
 			:<<,
 			EMPromise.resolve(nil),
@@ -293,25 +328,15 @@ class AdminCommandTest < Minitest::Test
 
 		sgx.expect(:deregister!, EMPromise.resolve(nil))
 
-		stub_request(
-			:post,
-			"https://dashboard.bandwidth.com/v1.0/accounts/moveto/moveTns"
-		).with(
-			body: {
-				CustomerOrderId: "test",
-				SourceAccountId: "test_bw_account",
-				SiteId: "movetosite",
-				SipPeerId: "movetopeer",
-				TelephoneNumbers: { TelephoneNumber: "5566667777" }
-			}.to_xml(indent: 0, root: "MoveTnsOrder")
-		).to_return(status: 200, body: "")
-
 		admin.action_cancel_account.sync
 
 		assert_mock sgx
 		assert_mock BackendSgx::IQ_MANAGER
 		assert_mock Customer::BLATHER
-		assert_requested req
+		assert_mock BandwidthTnRepo::DB
+
+		assert_requested churnbuster_req
+		assert_requested bandwidth_req
 	end
 	em :test_action_cancel_account_keep_number
 end