From 54006129529211a09386454e48e3df4e17703cef Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 25 Jan 2022 16:54:25 -0500 Subject: [PATCH] Check for low balance when setting auto top up 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. --- lib/auto_top_up_repo.rb | 9 +++++++++ test/test_auto_top_up_repo.rb | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/auto_top_up_repo.rb b/lib/auto_top_up_repo.rb index dba06a76b5503ab83949f1d7cdf69a24aafcd0a9..d899aaf30547df376ffc927115b701735ff5379b 100644 --- a/lib/auto_top_up_repo.rb +++ b/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 diff --git a/test/test_auto_top_up_repo.rb b/test/test_auto_top_up_repo.rb index 00036fc14cf12ffe6480d42f4561fc122674b9cd..64c139944a1fb3ada5bf8f422f1cf24dcbf66795 100644 --- a/test/test_auto_top_up_repo.rb +++ b/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] }