auto_top_up_repo.rb

 1# frozen_string_literal: true
 2
 3class AutoTopUpRepo
 4	def initialize(redis: REDIS, db: DB)
 5		@redis = redis
 6		@db = db
 7	end
 8
 9	def find(customer_id)
10		redis(:get, customer_id)
11	end
12
13	def put(customer_id, amount)
14		if amount >= 15
15			redis(:set, customer_id, amount)
16			reset_low_balance_lock(customer_id)
17			@db.exec_params(
18				"SELECT check_and_notify_low_balance($1)",
19				[customer_id]
20			)
21		elsif amount.zero?
22			redis(:del, customer_id)
23		end
24	end
25
26protected
27
28	def redis(action, customer_id, *args)
29		@redis.public_send(
30			action,
31			"jmp_customer_auto_top_up_amount-#{customer_id}",
32			*args
33		)
34	end
35
36	def reset_low_balance_lock(customer_id)
37		@redis.del("jmp_customer_low_balance-#{customer_id}")
38	end
39end