Reserve TN with bandwith after selection and pass reservation id when ordering

root21 created

Change summary

lib/bandwidth_tn_reservation_repo.rb | 30 ++++++++++++++++++++++++++++++
lib/registration.rb                  | 20 ++++++++++++--------
test/test_registration.rb            |  7 +++++++
3 files changed, 49 insertions(+), 8 deletions(-)

Detailed changes

lib/bandwidth_tn_reservation_repo.rb 🔗

@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+require "forwardable"
+require "ruby-bandwidth-iris"
+Faraday.default_adapter = :em_synchrony
+
+class BandwidthTnReservationRepo
+	FOUR_HOURS = 60 * 60 * 4
+
+	def initialize(redis: REDIS)
+		@redis = redis
+	end
+
+	def ensure(customer, tel)
+		REDIS.exists(key(customer, tel)).then do |exists|
+			unless exists == 1
+				reservation_id = BandwidthIris::TnReservation.create(tel).reservation_id
+				REDIS.setex(key(customer, tel), FOUR_HOURS, reservation_id)
+			end
+		end
+	end
+
+	def get(customer, tel)
+		REDIS.get(key(customer, tel))
+	end
+
+	def key(customer, tel)
+		"jmp_customer_tn_reservation_#{tel}-#{customer.customer_id}"
+	end
+end

lib/registration.rb 🔗

@@ -6,6 +6,7 @@ require "securerandom"
 
 require_relative "./alt_top_up_form"
 require_relative "./bandwidth_tn_order"
+require_relative "./bandwidth_tn_reservation_repo"
 require_relative "./command"
 require_relative "./em"
 require_relative "./invites_repo"
@@ -19,6 +20,7 @@ class Registration
 			Registered.new(reg.phone)
 		else
 			tel_selections[customer.jid].then(&:choose_tel).then do |tel|
+				BandwidthTnReservationRepo.new.ensure(customer, tel)
 				FinishOrStartActivation.for(customer, tel)
 			end
 		end
@@ -446,14 +448,16 @@ class Registration
 		end
 
 		def write
-			BandwidthTNOrder.create(
-				@tel,
-				customer_order_id: @customer.customer_id,
-				reservation_id: rid
-			).then(&:poll).then(
-				->(_) { customer_active_tel_purchased },
-				method(:number_purchase_error)
-			)
+			BandwidthTnReservationRepo.new.get(@customer, @tel).then do |rid|
+				BandwidthTNOrder.create(
+					@tel,
+					customer_order_id: @customer.customer_id,
+					reservation_id: rid
+				).then(&:poll).then(
+					->(_) { customer_active_tel_purchased },
+					method(:number_purchase_error)
+				)
+			end
 		end
 
 	protected

test/test_registration.rb 🔗

@@ -4,6 +4,8 @@ require "test_helper"
 require "customer"
 require "registration"
 
+BandwidthTnReservationRepo::REDIS = FakeRedis.new
+
 class RegistrationTest < Minitest::Test
 	def test_for_registered
 		sgx = OpenStruct.new(
@@ -22,6 +24,10 @@ class RegistrationTest < Minitest::Test
 	em :test_for_registered
 
 	def test_for_activated
+		reservation_req = stub_request(
+			:post,
+			"https://dashboard.bandwidth.com/v1.0/accounts//tnreservation"
+		)
 		web_manager = TelSelections.new(redis: FakeRedis.new)
 		web_manager.set("test@example.net", "+15555550000")
 		result = execute_command do
@@ -36,6 +42,7 @@ class RegistrationTest < Minitest::Test
 			)
 		end
 		assert_kind_of Registration::Finish, result
+		assert_requested reservation_req
 	end
 	em :test_for_activated