From 1e82f5297473bc2f1d08fbd133d1ccac77720219 Mon Sep 17 00:00:00 2001 From: Amolith Date: Mon, 13 Nov 2023 16:35:06 -0500 Subject: [PATCH] Reserve number in appropriate backend - At the start of registration, numbers are now reserved in Bandwidth or "reserved" in the local inventory, whichever source the number is from - Tests mock successful reservation rather than half of a reservation Signed-off-by: Amolith --- lib/registration.rb | 17 +++++++++++++++-- lib/tel_selections.rb | 8 ++++++++ test/test_helper.rb | 5 +++++ test/test_registration.rb | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/registration.rb b/lib/registration.rb index e12b55e8d148f64501c02c9ae738e0ec06afdc3f..d1a8cad0d3e57803ba513c9d248c2da89f7cec90 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -22,12 +22,25 @@ class Registration Registered.for(customer, reg.phone) else tel_selections[customer.jid].then(&:choose_tel).then do |tel| - BandwidthTnReservationRepo.new.ensure(customer, tel) - FinishOrStartActivation.for(customer, google_play_userid, tel) + reserve_and_continue(tel_selections, customer, tel).then do + FinishOrStartActivation.for(customer, google_play_userid, tel) + end end end end + def self.reserve_and_continue(tel_selections, customer, tel) + tel.reserve(customer).catch do + tel_selections.delete(customer.jid).then { + tel_selections[customer.jid] + }.then { |choose| + choose.choose_tel( + error: "The JMP number #{tel} is no longer available." + ) + }.then { |n_tel| reserve_and_continue(tel_selections, customer, n_tel) } + end + end + class Registered def self.for(customer, tel) jid = ProxiedJID.new(customer.jid).unproxied diff --git a/lib/tel_selections.rb b/lib/tel_selections.rb index f2127244ad62adf9d3e31c36c202825a250241b2..9fff7ec23a68c0ce6fad906b5860a66d3b61c651 100644 --- a/lib/tel_selections.rb +++ b/lib/tel_selections.rb @@ -258,12 +258,20 @@ class TelSelections def pending_value tel end + + def reserve(customer) + BandwidthTnReservationRepo.new.ensure(customer, tel) + end end class LocalInventory < SimpleDelegator def pending_value "LocalInventory/#{tel}" end + + def reserve(*) + EMPromise.resolve(nil) + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index c923eb8bfa3389af9a0c8c15e804ae31a045aa63..daedb15c953fae1839d3bc3fb4779534c9e7f79d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -242,6 +242,11 @@ class FakeRedis set(key, value) end + def del(key) + @values.delete(key) + EMPromise.resolve("OK") + end + def mget(*keys) EMPromise.all(keys.map(&method(:get))) end diff --git a/test/test_registration.rb b/test/test_registration.rb index 04865508e28fa71ac24936cce58728a32a3bce64..a519007fbee6023c66c0b4d62b819afd11a0e97e 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -28,7 +28,24 @@ class RegistrationTest < Minitest::Test reservation_req = stub_request( :post, "https://dashboard.bandwidth.com/v1.0/accounts//tnreservation" + ).to_return( + status: 201, + headers: { + location: "https://dashboard.bandwidth.com/api/accounts//TnReservation/8da0f39c-043c-4806-9f0f-497b2d197bc5" + } ) + stub_request( + :get, "https://dashboard.bandwidth.com/v1.0/accounts//tnreservation/8da0f39c-043c-4806-9f0f-497b2d197bc5" + ).to_return(status: 200, body: <<~RESPONSE) + + + f342904f-b03a-4499-bac0-e8f43a2664a1 + 12346099 + 1492 + 4354776010 + + + RESPONSE web_manager = TelSelections.new( redis: FakeRedis.new, db: FakeDB.new, memcache: FakeMemcache.new )