diff --git a/lib/bandwidth_tn_reservation_repo.rb b/lib/bandwidth_tn_reservation_repo.rb new file mode 100644 index 0000000000000000000000000000000000000000..e425f05b64147d3edc68c3e88a94529c5806e6b0 --- /dev/null +++ b/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 diff --git a/lib/registration.rb b/lib/registration.rb index 0be697dd5b8210910d24a803220958feb845917c..00bbc2fe08004929621bb0039ca6d1c0868ba1c6 100644 --- a/lib/registration.rb +++ b/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 diff --git a/test/test_registration.rb b/test/test_registration.rb index caeaeaee899af36231201e40e62ed11a28ebaae5..b2455957c8a7f299d22d2c2004a2c2c4ade0f708 100644 --- a/test/test_registration.rb +++ b/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