Check for low balance when setting auto top up

Stephen Paul Weber created

People expect to be able to set their auto top up setting in response to a low
balance alert, and have this solve the problem instantly.  This change resets
the lock that prevents sgx-jmp from acting on a low_balance notification, and
then asks the database to check if the balance is low and issue a notify if
relevant, which would then result in sgx-jmp acting and charging their card at
the newly-configured level.

Change summary

lib/auto_top_up_repo.rb       |  9 +++++++++
test/test_auto_top_up_repo.rb | 11 +++++++++++
2 files changed, 20 insertions(+)

Detailed changes

lib/auto_top_up_repo.rb 🔗

@@ -13,6 +13,11 @@ class AutoTopUpRepo
 	def put(customer_id, amount)
 		if amount >= 15
 			redis(:set, customer_id, amount)
+			reset_low_balance_lock(customer_id)
+			@db.exec_params(
+				"SELECT check_and_notify_low_balance($1)",
+				[customer_id]
+			)
 		elsif amount.zero?
 			redis(:del, customer_id)
 		end
@@ -27,4 +32,8 @@ protected
 			*args
 		)
 	end
+
+	def reset_low_balance_lock(customer_id)
+		@redis.del("jmp_customer_low_balance-#{customer_id}")
+	end
 end

test/test_auto_top_up_repo.rb 🔗

@@ -28,8 +28,19 @@ class AutoTopUpRepoTest < Minitest::Test
 			nil,
 			["jmp_customer_auto_top_up_amount-somecustomer", amount]
 		)
+		@redis.expect(
+			:del,
+			nil,
+			["jmp_customer_low_balance-somecustomer"]
+		)
+		@db.expect(
+			:exec_params,
+			nil,
+			["SELECT check_and_notify_low_balance($1)", ["somecustomer"]]
+		)
 		@repo.put("somecustomer", amount)
 		assert_mock @redis
+		assert_mock @db
 	end
 
 	property(:put_invalid_amount) { branch [:range, 1, 14], [:range, -999, -1] }