~singpolyma/jmp-pay

ref: 68ed9c34b0d4a2eaa0e49def372deabe4eb55daf jmp-pay/lib/auto_top_up_repo.rb -rw-r--r-- 714 bytes
68ed9c34Stephen Paul Weber Run billing 3 at a time 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

class AutoTopUpRepo
	def initialize(redis: REDIS, db: DB)
		@redis = redis
		@db = db
	end

	def find(customer_id)
		redis(:get, customer_id)
	end

	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
	end

protected

	def redis(action, customer_id, *args)
		@redis.public_send(
			action,
			"jmp_customer_auto_top_up_amount-#{customer_id}",
			*args
		)
	end

	def reset_low_balance_lock(customer_id)
		@redis.del("jmp_customer_low_balance-#{customer_id}")
	end
end