From cc50a2fb8a167131e5cbda5324d7d733505b3d14 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Mon, 15 Dec 2025 17:28:23 -0500 Subject: [PATCH] fix: use same unit for DataOnly balance and price --- lib/registration.rb | 12 +++--------- lib/sim_kind.rb | 7 +++++++ lib/sim_order.rb | 1 - test/test_registration.rb | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/registration.rb b/lib/registration.rb index 13a4ad8a7cd3372f0c65a64840264ae419d82563..6a7c5ab26d50ec39cb249c2c66df3c1b34b3a150 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -81,14 +81,8 @@ class Registration class DataOnly def self.for(customer, sim_kind) - cfg = sim_kind.cfg(customer.currency) - log.debug( - "DataOnly#for", - cfg: cfg, - customer_balance: customer.balance, - customer_currency: customer.currency - ) - unless cfg && customer.balance >= cfg[:price] + price = sim_kind.price_dollars(customer.currency) + unless price && customer.balance >= price return PayForSim.new(customer, sim_kind) end @@ -827,7 +821,7 @@ class Registration end end - def initialize(customer, tel, error: nil, old: nil, db:) + def initialize(customer, tel, db:, error: nil, old: nil) @customer = customer @tel = tel @error = error diff --git a/lib/sim_kind.rb b/lib/sim_kind.rb index aa77b78123201aa787539fc7b3219d5316239b18..232656e4539aa41ee8c324c7523ec05f4dac0b04 100644 --- a/lib/sim_kind.rb +++ b/lib/sim_kind.rb @@ -21,4 +21,11 @@ class SIMKind def cfg(currency) CONFIG.dig(:sims, @variant.to_sym, currency) end + + def price_dollars(currency) + cfg = cfg(currency) + return nil unless cfg + + cfg[:price] / 100.to_d + end end diff --git a/lib/sim_order.rb b/lib/sim_order.rb index e10f2af74d230e40558aaab922efcc973b1cc72e..dc7b04082a695251d6a6e1bd318b782504097c94 100644 --- a/lib/sim_order.rb +++ b/lib/sim_order.rb @@ -38,7 +38,6 @@ class SIMOrder def self.for(customer, price:, **kwargs) price = price.to_i / 100.to_d - log.debug("SIMOrder.for", balance: customer.balance, price: price) return new(customer, price: price, **kwargs) if customer.balance >= price LowBalance::AutoTopUp.for(customer, price).then do |top_up| diff --git a/test/test_registration.rb b/test/test_registration.rb index f7c9d63cdbfa877df73d50d25dfb44e40527ab4e..c4b54d157921786648dad69f434709d61c3c1620 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -2223,7 +2223,7 @@ class RegistrationTest < Minitest::Test class DataOnlyTest < Minitest::Test def setup - @customer = customer(plan_name: "test_usd").with_balance(1000) + @customer = customer(plan_name: "test_usd").with_balance(5) @sim_kind = SIMKind.new("sim") end @@ -2374,7 +2374,7 @@ class RegistrationTest < Minitest::Test { var: "activation_method", value: "credit_card" } ] - reloaded_customer = @customer.with_balance(100).with_plan("test_usd") + reloaded_customer = @customer.with_balance(4).with_plan("test_usd") result = execute_command do |exe| payment = Minitest::Mock.new