From 43ac00160445859bfed198c781ecb1f0ee55ce4d Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 13 Oct 2021 14:36:24 -0500 Subject: [PATCH] Move more persistence into the repo layer BackendSGX shouldn't touch Redis, instead get one of the repos to save the data we want saved wherever that repo saves it (in this case, those same Redis keys). --- lib/backend_sgx.rb | 8 ---- lib/bwmsgsv2_repo.rb | 28 +++++++++++++ lib/customer.rb | 2 +- lib/customer_repo.rb | 30 ++++++++++++-- lib/registration.rb | 11 ++---- sgx_jmp.rb | 10 ++--- test/test_customer_repo.rb | 81 +++++++++++++++++++++++++++++++++++++- test/test_registration.rb | 10 ++--- 8 files changed, 150 insertions(+), 30 deletions(-) diff --git a/lib/backend_sgx.rb b/lib/backend_sgx.rb index 9a0ee42940c4f9184424a94651f72955572a1e5a..8b0a15a20fb018df23aacf5ef5a4aae843d9cdc2 100644 --- a/lib/backend_sgx.rb +++ b/lib/backend_sgx.rb @@ -36,14 +36,6 @@ class BackendSgx end end - def set_fwd(uri) - REDIS.set("catapult_fwd-#{registered?.phone}", uri) - end - - def set_fwd_timeout(timeout) - REDIS.set("catapult_fwd_timeout-#{from_jid}", timeout) - end - def set_ogm_url(url) REDIS.set("catapult_ogm_url-#{from_jid}", url) end diff --git a/lib/bwmsgsv2_repo.rb b/lib/bwmsgsv2_repo.rb index 2aa498e6a91dab4c7f922e9d6be77f36fdfe179a..b295ff199a08b972648ff974d9e1b79cf4d23112 100644 --- a/lib/bwmsgsv2_repo.rb +++ b/lib/bwmsgsv2_repo.rb @@ -27,8 +27,36 @@ class Bwmsgsv2Repo end end + def put_transcription_enabled(customer_id, enabled) + sgx = @trivial_repo.get(customer_id) + REDIS.setbit( + "catapult_settings_flags-#{sgx.from_jid}", + Bwmsgsv2Repo::VOICEMAIL_TRANSCRIPTION_DISABLED, + enabled ? 0 : 1 + ) + end + + def put_fwd(customer_id, tel, customer_fwd) + sgx = @trivial_repo.get(customer_id) + EMPromise.all([ + set_or_delete("catapult_fwd-#{tel}", customer_fwd.uri), + set_or_delete( + "catapult_fwd_timeout-#{sgx.from_jid}", + customer_fwd.timeout.to_i + ) + ]) + end + protected + def set_or_delete(k, v) + if v.nil? + REDIS.del(k) + else + REDIS.set(k, v) + end + end + def fetch_raw(from_jid) registration(from_jid).then do |r| EMPromise.all([from_redis(from_jid, r ? r.phone : nil), r]) diff --git a/lib/customer.rb b/lib/customer.rb index 71aebc0b97ed12416b218d7bf15e3e110e0bf7c9..e4f50a6839f5bab5723510871ae2f003e79e8219 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -23,7 +23,7 @@ class Customer def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan, :currency, :merchant_account, :plan_name, :auto_top_up_amount def_delegators :@sgx, :register!, :registered?, :set_ogm_url, - :set_fwd, :fwd, :transcription_enabled + :fwd, :transcription_enabled def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage def initialize( diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index c45c8fc50f056b67d978ee224495535244561c14..109ac28cf82da9c3b5b5cfe62754b378ca9bd593 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -59,12 +59,36 @@ class CustomerRepo end end + def put_lidb_name(customer, lidb_name) + BandwidthIris::Lidb.create( + customer_order_id: customer.customer_id, + lidb_tn_groups: { lidb_tn_group: { + telephone_numbers: [customer.registered?.phone.sub(/\A\+1/, "")], + subscriber_information: lidb_name, + use_type: "RESIDENTIAL", + visibility: "PUBLIC" + } } + ) + end + + def put_transcription_enabled(customer, transcription_enabled) + @sgx_repo.put_transcription_enabled( + customer.customer_id, transcription_enabled + ) + end + + def put_fwd(customer, customer_fwd) + @sgx_repo.put_fwd( + customer.customer_id, + customer.registered?.phone, + customer_fwd + ) + end + protected def new_sgx(customer_id) - TrivialBackendSgxRepo.new.get(customer_id).with( - registered?: false - ) + TrivialBackendSgxRepo.new.get(customer_id).with(registered?: false) end def find_legacy_customer(jid) diff --git a/lib/registration.rb b/lib/registration.rb index 7b12c51a4a35c5473ff0e594bd76e3c2704b6724..9de075707d379cfb09efa1f9f0a81b67290e94e6 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -445,10 +445,6 @@ class Registration }.then { |tel| Finish.new(@customer, tel).write } end - def cheogram_sip_addr - "sip:#{ERB::Util.url_encode(@customer.jid)}@sip.cheogram.com" - end - def raise_setup_error(e) Command.log.error "@customer.register! failed", e Command.finish( @@ -459,11 +455,12 @@ class Registration end def customer_active_tel_purchased - @customer.register!(@tel).catch(&method(:raise_setup_error)).then { |sgx| + @customer.register!(@tel).catch(&method(:raise_setup_error)).then { EMPromise.all([ REDIS.del("pending_tel_for-#{@customer.jid}"), - sgx.set_fwd(cheogram_sip_addr), - sgx.set_fwd_timeout(25) # ~5 seconds / ring, 5 rings + Bwmsgsv2Repo.new.put_fwd(@customer.customer_id, @tel, CustomerFwd.for( + uri: "xmpp:#{@customer.jid}", timeout: 25 # ~5 seconds / ring, 5 rings + )) ]) }.then do Command.finish("Your JMP account has been activated as #{@tel}") diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 15cfde0eeea3ed5d3725c6de6f4cbcc52c22f571..6c4cf34d657d88c7f9a433185f213974b3cadb46 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -617,12 +617,12 @@ Command.new( if ["1", "true"].include?(fwd.form.field("change_fwd")&.value.to_s) # Migrate location if needed BandwidthIris::SipPeer.new( - site_id: CONFIG[:bandwidth_site], - id: CONFIG[:bandwidth_peer] + site_id: CONFIG[:bandwidth_site], id: CONFIG[:bandwidth_peer] ).move_tns([customer.registered?.phone]) - customer.set_fwd(sip_account.uri).then do - Command.finish("Inbound calls will now forward to SIP.") - end + Command.execution.customer_repo.put_fwd( + customer, + customer.fwd.with(uri: sip_account.uri) + ).then { Command.finish("Inbound calls will now forward to SIP.") } else Command.finish end diff --git a/test/test_customer_repo.rb b/test/test_customer_repo.rb index 255352dc78f557c2964a63c0ff8223cc780c5d91..d996491e4d0aeb26d5c85812e3f65b19d3e3006c 100644 --- a/test/test_customer_repo.rb +++ b/test/test_customer_repo.rb @@ -3,6 +3,10 @@ require "test_helper" require "customer_repo" +class CustomerRepo + attr_reader :sgx_repo +end + class CustomerRepoTest < Minitest::Test FAKE_REDIS = FakeRedis.new( # sgx-jmp customer @@ -51,7 +55,13 @@ class CustomerRepoTest < Minitest::Test db: FAKE_DB, braintree: Minitest::Mock.new ) - CustomerRepo.new(redis: redis, db: db, braintree: braintree) + sgx_repo = Minitest::Mock.new(TrivialBackendSgxRepo.new) + CustomerRepo.new( + redis: redis, + db: db, + braintree: braintree, + sgx_repo: sgx_repo + ) end def setup @@ -153,4 +163,73 @@ class CustomerRepoTest < Minitest::Test assert_mock redis end em :test_create + + def test_put_lidb_name + post = stub_request( + :post, + "https://dashboard.bandwidth.com/v1.0/accounts//lidbs" + ).with(body: { + CustomerOrderId: "test", + LidbTnGroups: { + LidbTnGroup: { + TelephoneNumbers: "5556667777", + SubscriberInformation: "Hank", + UseType: "RESIDENTIAL", + Visibility: "PUBLIC" + } + } + }.to_xml(root: "LidbOrder", indent: 0)).to_return( + status: 201, + headers: { location: "/boop/123" } + ) + + stub_request( + :get, + "https://dashboard.bandwidth.com/v1.0/accounts//lidbs/123" + ) + + @repo.put_lidb_name( + Customer.new( + "test", + "test@exmple.com", + sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777")) + ), + "Hank" + ) + + assert_requested post + end + em :test_put_lidb_name + + def test_put_transcription_enabled + @repo.sgx_repo.expect( + :put_transcription_enabled, + EMPromise.resolve(nil), + ["test", true] + ) + @repo.put_transcription_enabled( + Customer.new("test", "test@exmple.com"), + true + ) + assert_mock @repo.sgx_repo + end + em :test_put_transcription_enabled + + def test_put_fwd + @repo.sgx_repo.expect( + :put_fwd, + EMPromise.resolve(nil), + ["test", "+15556667777", :fwd] + ) + @repo.put_fwd( + Customer.new( + "test", + "test@exmple.com", + sgx: OpenStruct.new(registered?: OpenStruct.new(phone: "+15556667777")) + ), + :fwd + ) + assert_mock @repo.sgx_repo + end + em :test_put_fwd end diff --git a/test/test_registration.rb b/test/test_registration.rb index 52b5e6475fe5592a543b4aa4b20da63892dfe731..24b2e7c926e89d230bc0d4802feed387a60934d7 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -517,7 +517,7 @@ class RegistrationTest < Minitest::Test Command::COMMAND_MANAGER = Minitest::Mock.new Registration::Finish::TEL_SELECTIONS = FakeTelSelections.new Registration::Finish::REDIS = Minitest::Mock.new - BackendSgx::REDIS = Minitest::Mock.new + Bwmsgsv2Repo::REDIS = Minitest::Mock.new def setup @sgx = Minitest::Mock.new(TrivialBackendSgxRepo.new.get("test")) @@ -568,15 +568,15 @@ class RegistrationTest < Minitest::Test nil, ["pending_tel_for-test@example.net"] ) - BackendSgx::REDIS.expect( + Bwmsgsv2Repo::REDIS.expect( :set, nil, [ "catapult_fwd-+15555550000", - "sip:test%40example.net@sip.cheogram.com" + "xmpp:test@example.net" ] ) - BackendSgx::REDIS.expect( + Bwmsgsv2Repo::REDIS.expect( :set, nil, ["catapult_fwd_timeout-customer_test@component", 25] @@ -608,7 +608,7 @@ class RegistrationTest < Minitest::Test assert_requested create_order assert_mock @sgx assert_mock Registration::Finish::REDIS - assert_mock BackendSgx::REDIS + assert_mock Bwmsgsv2Repo::REDIS assert_mock blather end em :test_write