Change summary
lib/low_balance.rb | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)
Detailed changes
@@ -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)