Merge branch '380-conditionally-show-command' of https://git.secluded.site/sgx-bwmsgsv2

Stephen Paul Weber created

* '380-conditionally-show-command' of https://git.secluded.site/sgx-bwmsgsv2:
  refactor: only show port-out cmd if tn eligible

Change summary

lib/bandwidth_tn_options.rb | 47 +++++++++++++++++++++++++++++---------
sgx-bwmsgsv2.rb             | 47 +++++++++++++++++++-------------------
2 files changed, 58 insertions(+), 36 deletions(-)

Detailed changes

lib/bandwidth_tn_options.rb 🔗

@@ -5,27 +5,50 @@ require "ruby-bandwidth-iris"
 Faraday.default_adapter = :em_synchrony
 
 class BandwidthTNOptions
-	def self.set_port_out_pin(user_id, token, secret, pin, tel)
+	def self.tn_eligible_for_port_out_pin?(creds)
+		user_id, token, secret, tel = creds
+		tel_local = tel.sub(/^\+1/, '')
 		client = BandwidthIris::Client.new(
 			account_id: user_id,
 			username: token,
 			password: secret
 		)
+		EMPromise.resolve(nil).then do
+			tn = BandwidthIris::Tn.get(client, tel_local)
+			details = tn.get_details()
+			details[:tier] == 0.0 && details[:on_net_vendor] == true
+		end
+	end
+
+	def self.set_port_out_pin(creds, pin)
+		tn_eligible_for_port_out_pin?(creds).then do |eligible|
+			unless eligible
+				raise "TN not eligible for port-out PIN"
+			end
 
-		data = {
-			tn_option_groups: {
-				tn_option_group: [
-					{
-						port_out_passcode: pin,
-						telephone_numbers: {
-							telephone_number: [tel]
+			user_id, token, secret, tel = creds
+			tel_local = tel.sub(/^\+1/, '')
+
+			client = BandwidthIris::Client.new(
+				account_id: user_id,
+				username: token,
+				password: secret
+			)
+
+
+			data = {
+				tn_option_groups: {
+					tn_option_group: [
+						{
+							port_out_passcode: pin,
+							telephone_numbers: {
+								telephone_number: [tel]
+							}
 						}
-					}
-				]
+					]
+				}
 			}
-		}
 
-		EMPromise.resolve(nil).then do
 			order = BandwidthIris::TnOptions.create_tn_option_order(client, data)
 			order_id = order[:order_id]
 

sgx-bwmsgsv2.rb 🔗

@@ -541,22 +541,26 @@ module SGXbwmsgsv2
 		to: Blather::JID.new(ARGV[0]),
 		node: "http://jabber.org/protocol/commands"
 	) do |i|
-		# Ensure user is registered, but discard their credentials because we don't
-		# need them yet
-		fetch_catapult_cred_for(i.from).then { |_creds|
-			reply = i.reply
-			reply.node = 'http://jabber.org/protocol/commands'
-
-			reply.items = [
-				Blather::Stanza::DiscoItems::Item.new(
-					i.to,
-					'set-port-out-pin',
-					'Set Port-Out PIN'
-				)
-			]
+		fetch_catapult_cred_for(i.from).then { |creds|
+			BandwidthTNOptions.tn_eligible_for_port_out_pin?(creds).then { |eligible|
+				reply = i.reply
+				reply.node = 'http://jabber.org/protocol/commands'
+
+				if eligible
+					reply.items = [
+						Blather::Stanza::DiscoItems::Item.new(
+							i.to,
+							'set-port-out-pin',
+							'Set Port-Out PIN'
+						)
+					]
+				else
+					reply.items = []
+				end
 
-			puts 'RESPONSE_CMD_DISCO: ' + reply.inspect
-			write_to_stream reply
+				puts 'RESPONSE_CMD_DISCO: ' + reply.inspect
+				write_to_stream reply
+			}
 		}.catch { |e|
 			if e.is_a?(Array) && [2, 3].include?(e.length)
 				write_to_stream i.as_error(e[1], e[0], e[2])
@@ -753,8 +757,8 @@ module SGXbwmsgsv2
 	end
 
 	command :execute?, node: "set-port-out-pin", sessionid: nil do |iq|
-		# Ensure user is registered, but discard their credentials because we don't
-		# need them yet
+		# Ensure user is registered, but discard their credentials
+		# because we're just showing them a form.
 		fetch_catapult_cred_for(iq.from).then { |_creds|
 			reply = iq.reply
 			reply.node = 'set-port-out-pin'
@@ -787,7 +791,7 @@ module SGXbwmsgsv2
 			if e.is_a?(Array) && [2, 3].include?(e.length)
 				write_to_stream iq.as_error(e[1], e[0], e[2])
 			else
-			EMPromise.reject(e)
+				EMPromise.reject(e)
 			end
 		}.catch(&method(:panic))
 	end
@@ -824,12 +828,7 @@ module SGXbwmsgsv2
 		end
 
 		fetch_catapult_cred_for(iq.from).then { |creds|
-			user_id, token, secret, phone_num = creds
-
-			# Stripping +1 like this feels janky, but Bandwidth only deals in +1
-			# numbers and this is a Bandwidth SGX, so it's fine.
-			phone_num_local = phone_num.sub(/^\+1/, '')
-			BandwidthTNOptions.set_port_out_pin(user_id, token, secret, pin, phone_num_local).then {
+			BandwidthTNOptions.set_port_out_pin(creds, pin).then {
 				reply = iq.reply
 				reply.node = 'set-port-out-pin'
 				reply.sessionid = iq.sessionid