~singpolyma/sgx-jmp

43ac00160445859bfed198c781ecb1f0ee55ce4d — Stephen Paul Weber 9 months ago 75d1042
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).
M lib/backend_sgx.rb => lib/backend_sgx.rb +0 -8
@@ 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

M lib/bwmsgsv2_repo.rb => lib/bwmsgsv2_repo.rb +28 -0
@@ 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])

M lib/customer.rb => lib/customer.rb +1 -1
@@ 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(

M lib/customer_repo.rb => lib/customer_repo.rb +27 -3
@@ 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)

M lib/registration.rb => lib/registration.rb +4 -7
@@ 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}")

M sgx_jmp.rb => sgx_jmp.rb +5 -5
@@ 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

M test/test_customer_repo.rb => test/test_customer_repo.rb +80 -1
@@ 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

M test/test_registration.rb => test/test_registration.rb +5 -5
@@ 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