Factor out reload customer, check balance, maybe bill

Stephen Paul Weber created

Change summary

lib/registration.rb       | 50 +++++++++++++++++++++++++++++-----------
test/test_registration.rb | 13 +++++++---
2 files changed, 45 insertions(+), 18 deletions(-)

Detailed changes

lib/registration.rb 🔗

@@ -364,15 +364,45 @@ class Registration
 			end
 		end
 
-		class CreditCard
-			Payment.kinds[:credit_card] = ->(*args, **kw) { self.for(*args, **kw) }
+		class MaybeBill
+			def initialize(customer, tel, finish: Finish)
+				@customer = customer
+				@tel = tel
+				@finish = finish
+			end
 
-			def self.for(in_customer, tel, finish: Finish, **)
-				reload_customer(in_customer).then do |customer|
+			def call
+				reload_customer.then do |customer|
 					if customer.balance >= CONFIG[:activation_amount_accept]
-						next BillPlan.new(customer, tel, finish: finish)
+						next BillPlan.new(customer, @tel, finish: @finish)
 					end
 
+					yield customer
+				end
+			end
+
+			def reload_customer
+				EMPromise.resolve(nil).then do
+					Command.execution.customer_repo.find(@customer.customer_id)
+				end
+			end
+		end
+
+		class JustCharge
+			def initialize(customer)
+				@customer = customer
+			end
+
+			def call
+				yield @customer
+			end
+		end
+
+		class CreditCard
+			Payment.kinds[:credit_card] = ->(*args, **kw) { self.for(*args, **kw) }
+
+			def self.for(in_customer, tel, finish: Finish, maybe_bill: MaybeBill, **)
+				maybe_bill.new(in_customer, tel, finish: finish).call do |customer|
 					new(customer, tel, finish: finish)
 				end
 			end
@@ -481,16 +511,8 @@ class Registration
 				}
 			end
 
-			def reload_customer
-				Command.execution.customer_repo.find(@customer.customer_id)
-			end
-
 			def check_parent_balance
-				reload_customer.then do |customer|
-					if customer.balance >= CONFIG[:activation_amount_accept]
-						next BillPlan.new(customer, @tel, finish: @finish)
-					end
-
+				MaybeBill.new(@customer, @tel, finish: @finish).call do
 					msg = "Account balance not enough to cover the activation"
 					invalid_code(RuntimeError.new(msg))
 				end

test/test_registration.rb 🔗

@@ -380,7 +380,7 @@ class RegistrationTest < Minitest::Test
 					:find,
 					EMPromise.resolve(@customer), ["test"]
 				)
-				Registration::Payment::InviteCode::BillPlan.expect(
+				Registration::Payment::MaybeBill::BillPlan.expect(
 					:new,
 					EMPromise.reject(:test_result)
 				) do |*args, **|
@@ -395,6 +395,7 @@ class RegistrationTest < Minitest::Test
 			assert_mock @customer
 			assert_mock Registration::Activation::Payment
 			assert_mock Registration::Activation::DB
+			assert_mock Registration::Payment::MaybeBill::BillPlan
 		end
 		em :test_write_with_parent_code
 
@@ -834,6 +835,10 @@ class RegistrationTest < Minitest::Test
 				cust = Minitest::Mock.new(customer)
 				cust.expect(:balance, 100)
 				cust.expect(:payment_methods, EMPromise.resolve(nil))
+				Registration::Payment::MaybeBill::BillPlan.expect(
+					:new,
+					Registration::BillPlan.new(nil, nil)
+				) { true }
 				execute_command do
 					Command.execution.customer_repo.expect(:find, cust, ["test"])
 					assert_kind_of(
@@ -913,7 +918,7 @@ class RegistrationTest < Minitest::Test
 			Command::COMMAND_MANAGER = Minitest::Mock.new
 			Registration::Payment::InviteCode::Finish =
 				Minitest::Mock.new
-			Registration::Payment::InviteCode::BillPlan =
+			Registration::Payment::MaybeBill::BillPlan =
 				Minitest::Mock.new
 
 			def test_write
@@ -965,7 +970,7 @@ class RegistrationTest < Minitest::Test
 
 			def test_write_parent_code
 				customer = customer(plan_name: "test_usd")
-				Registration::Payment::InviteCode::BillPlan.expect(
+				Registration::Payment::MaybeBill::BillPlan.expect(
 					:new,
 					OpenStruct.new(write: nil)
 				) { |*| true }
@@ -1022,7 +1027,7 @@ class RegistrationTest < Minitest::Test
 				assert_mock Command::COMMAND_MANAGER
 				assert_mock Registration::Payment::InviteCode::DB
 				assert_mock Registration::Payment::InviteCode::REDIS
-				assert_mock Registration::Payment::InviteCode::BillPlan
+				assert_mock Registration::Payment::MaybeBill::BillPlan
 			end
 			em :test_write_parent_code