~singpolyma/jmp-pay

54006129529211a09386454e48e3df4e17703cef — Stephen Paul Weber 1 year, 10 months ago 7958c1c
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.
2 files changed, 20 insertions(+), 0 deletions(-)

M lib/auto_top_up_repo.rb
M test/test_auto_top_up_repo.rb
M lib/auto_top_up_repo.rb => lib/auto_top_up_repo.rb +9 -0
@@ 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

M test/test_auto_top_up_repo.rb => test/test_auto_top_up_repo.rb +11 -0
@@ 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] }