~singpolyma/sgx-jmp

ref: 9827b8b48336d249637f88d6921d613ad12b16f1 sgx-jmp/lib/trust_level_repo.rb -rw-r--r-- 816 bytes
9827b8b4Stephen Paul Weber Allow finishing adming command 11 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
# frozen_string_literal: true

require "value_semantics/monkey_patched"

require_relative "trust_level"

class TrustLevelRepo
	value_semantics do
		db    Anything(), default: LazyObject.new { DB }
		redis Anything(), default: LazyObject.new { REDIS }
	end

	def find(customer)
		EMPromise.all([
			redis.get("jmp_customer_trust_level-#{customer.customer_id}"),
			fetch_settled_amount(customer.customer_id)
		]).then do |(manual, rows)|
			TrustLevel.for(
				manual: manual,
				plan_name: customer.plan_name,
				**(rows.first&.transform_keys(&:to_sym) || {})
			)
		end
	end

protected

	def fetch_settled_amount(customer_id)
		db.query_defer(<<~SQL, [customer_id])
			SELECT SUM(amount) AS settled_amount FROM transactions
			WHERE customer_id=$1 AND settled_after < LOCALTIMESTAMP AND amount > 0
		SQL
	end
end