From 87fce7097fcea55a7abde718fc66120ccc96e48f Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 15 Jan 2024 14:10:01 -0500 Subject: [PATCH] Allow auto top up / low balance if there is a SIM Even without phone number. --- lib/low_balance.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/low_balance.rb b/lib/low_balance.rb index bee120e524a335d884abc269d11c746d15ea0c6c..f946aad26206cdec298b49bdbd07870684b16330 100644 --- a/lib/low_balance.rb +++ b/lib/low_balance.rb @@ -6,18 +6,31 @@ require_relative "credit_card_sale" class LowBalance def self.for(customer, transaction_amount=0) - return Locked.new unless customer.registered? - - ExpiringLock.new( - "jmp_customer_low_balance-#{customer.billing_customer_id}", - expiry: 60 * 60 * 24 * 7 - ).with(-> { Locked.new }) do - customer.billing_customer.then do |billing_customer| - for_no_lock(billing_customer, transaction_amount) + locked_if_no_services.then do |locked| + locked || ExpiringLock.new( + "jmp_customer_low_balance-#{customer.billing_customer_id}", + expiry: 60 * 60 * 24 * 7 + ).with(-> { Locked.new }) do + customer.billing_customer.then do |billing_customer| + for_no_lock(billing_customer, transaction_amount) + end end end end + def self.locked_if_no_services + return if customer.registered? + + DB.query_defer( + "SELECT COUNT(*) AS c FROM sims WHERE customer_id=$1", + [customer.customer_id] + ).then do |result| + next if result.first["c"].to_i.positive? + + Locked.new + end + end + def self.for_no_lock(customer, transaction_amount, auto: true) if auto && customer.auto_top_up_amount.positive? AutoTopUp.for(customer, transaction_amount)