More generic with helper for customer

Stephen Paul Weber created

Change summary

bin/sim_job               |  2 +-
lib/bill_plan_command.rb  |  2 +-
lib/call_attempt.rb       |  2 +-
lib/customer.rb           |  4 ++--
lib/registration.rb       |  2 +-
lib/tel_selections.rb     | 10 ++++++++++
test/test_registration.rb | 16 ++++++++--------
7 files changed, 24 insertions(+), 14 deletions(-)

Detailed changes

bin/sim_job 🔗

@@ -147,7 +147,7 @@ class SimTopUp
 
 	def low_balance
 		LowBalance.for(customer, refill_price).then(&:notify!).then do |result|
-			@customer = customer.with_balance(customer.balance + result)
+			@customer = customer.with(balance: customer.balance + result)
 			next call if result.positive?
 
 			LOG.info "Low balance #{customer.customer_id} #{iccid}"

lib/bill_plan_command.rb 🔗

@@ -77,7 +77,7 @@ protected
 
 		def command_for(amount)
 			BillPlanCommand.for(
-				@customer.with_balance(@customer.balance + amount)
+				@customer.with(balance: @customer.balance + amount)
 			)
 		end
 	end

lib/call_attempt.rb 🔗

@@ -149,7 +149,7 @@ class CallAttempt
 			low_balance.for(customer).then(&:notify!).then do |amount|
 				if amount&.positive?
 					CallAttempt.for(
-						customer: customer.with_balance(customer.balance + amount),
+						customer: customer.with(balance: customer.balance + amount),
 						**kwargs.merge(direction: direction)
 					)
 				else

lib/customer.rb 🔗

@@ -71,11 +71,11 @@ class Customer
 		@sgx = sgx
 	end
 
-	def with_balance(balance)
+	def with(balance: @balance, sgx: @sgx)
 		self.class.new(
 			@customer_id, @jid,
 			plan: @plan, balance: balance,
-			tndetails: @tndetails, sgx: @sgx
+			tndetails: @tndetails, sgx: sgx, feature_flags: @feature_flags
 		)
 	end
 

lib/registration.rb 🔗

@@ -679,7 +679,7 @@ class Registration
 		def write
 			@customer.bill_plan(note: "Bill #{@tel} for first month").then do
 				updated_customer =
-					@customer.with_balance(@customer.balance - @customer.monthly_price)
+					@customer.with(balance: @customer.balance - @customer.monthly_price)
 				@finish.new(updated_customer, @tel).write
 			end
 		end

lib/tel_selections.rb 🔗

@@ -398,6 +398,16 @@ class TelSelections
 					# )
 					db.exec_defer("DELETE FROM tel_inventory WHERE tel = $1", [tel])
 						.then { |r| raise unless r.cmd_tuples.positive? }
+						.then {
+							next unless @source.start_with?("xmpp:")
+
+							TrivialBackendSgxRepo.new
+								.put(customer.customer_id, @source.sub(/\Axmpp:/, ""))
+						}.then { |sgx|
+							next customer unless sgx
+
+							customer.with(sgx: sgx)
+						}
 				end
 			end
 		end

test/test_registration.rb 🔗

@@ -997,7 +997,7 @@ class RegistrationTest < Minitest::Test
 					)
 					Command.execution.customer_repo.expect(
 						:find,
-						customer.with_balance(10000),
+						customer.with(balance: 10000),
 						["test"]
 					)
 					Command::COMMAND_MANAGER.expect(
@@ -1668,11 +1668,11 @@ class RegistrationTest < Minitest::Test
 			low_cust = customer(
 				sgx: @sgx,
 				jid: Blather::JID.new("test\\40onboarding.example.com@proxy")
-			).with_balance(5.0)
+			).with(balance: 5.0)
 			high_cust = customer(
 				sgx: @sgx,
 				jid: Blather::JID.new("test\\40onboarding.example.com@proxy")
-			).with_balance(100.0)
+			).with(balance: 100.0)
 
 			stub_request(
 				:post,
@@ -2252,14 +2252,14 @@ class RegistrationTest < Minitest::Test
 
 	class RegistrationTypeTest < Minitest::Test
 		def test_for_with_sim_kind
-			cust = customer(plan_name: "test_usd").with_balance(1000)
+			cust = customer(plan_name: "test_usd").with(balance: 1000)
 			sim_kind = SIMKind.new("sim")
 			result = Registration::RegistrationType.for(cust, nil, sim_kind)
 			assert_kind_of Registration::DataOnly, result
 		end
 
 		def test_for_with_esim_kind
-			cust = customer(plan_name: "test_usd").with_balance(1000)
+			cust = customer(plan_name: "test_usd").with(balance: 1000)
 			sim_kind = SIMKind.new("esim")
 			result = Registration::RegistrationType.for(cust, nil, sim_kind)
 			assert_kind_of Registration::DataOnly, result
@@ -2279,12 +2279,12 @@ class RegistrationTest < Minitest::Test
 
 	class DataOnlyTest < Minitest::Test
 		def setup
-			@customer = customer(plan_name: "test_usd").with_balance(5)
+			@customer = customer(plan_name: "test_usd").with(balance: 5)
 			@sim_kind = SIMKind.new("sim")
 		end
 
 		def test_for_returns_pay_for_sim_when_balance_insufficient
-			low_balance_customer = @customer.with_balance(0.50)
+			low_balance_customer = @customer.with(balance: 0.50)
 			result = Registration::DataOnly.for(low_balance_customer, @sim_kind)
 			assert_kind_of Registration::PayForSim, result
 		end
@@ -2430,7 +2430,7 @@ class RegistrationTest < Minitest::Test
 				{ var: "activation_method", value: "credit_card" }
 			]
 
-			reloaded_customer = @customer.with_balance(4).with_plan("test_usd")
+			reloaded_customer = @customer.with(balance: 4).with_plan("test_usd")
 
 			result = execute_command do |exe|
 				payment = Minitest::Mock.new