~singpolyma/sgx-jmp

ref: 4b5310cb01ad20bfebb9156979d549e8a406c748 sgx-jmp/lib/trust_level.rb -rw-r--r-- 1.4 KiB
4b5310cbStephen Paul Weber CallAttempt needs customer 3 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

module TrustLevel
	def self.for(plan_name:, settled_amount: 0, manual: nil)
		@levels.each do |level|
			tl = level.call(
				plan_name: plan_name,
				settled_amount: settled_amount,
				manual: manual
			)
			return tl if tl
		end

		raise "No TrustLevel matched"
	end

	def self.register(&maybe_mk)
		@levels ||= []
		@levels << maybe_mk
	end

	class Tomb
		TrustLevel.register do |manual:, **|
			new if manual == "Tomb"
		end

		def support_call?(*)
			false
		end
	end

	class Basement
		TrustLevel.register do |manual:, settled_amount:, **|
			new if manual == "Basement" || (!manual && settled_amount < 10)
		end

		def support_call?(rate, concurrency)
			rate <= 0.02 && concurrency < 1
		end
	end

	class Paragon
		TrustLevel.register do |manual:, settled_amount:, **|
			new if manual == "Paragon" || (!manual && settled_amount > 60)
		end

		def support_call?(_, concurrency)
			concurrency < 10
		end
	end

	class Customer
		TrustLevel.register do |manual:, plan_name:, **|
			if manual && manual != "Customer"
				Sentry.capture_message("Unknown TrustLevel: #{manual}")
			end

			new(plan_name)
		end

		EXPENSIVE_ROUTE = {
			"usd_beta_unlimited-v20210223" => 0.9,
			"cad_beta_unlimited-v20210223" => 1.1
		}.freeze

		def initialize(plan_name)
			@max_rate = EXPENSIVE_ROUTE.fetch(plan_name, 0.1)
		end

		def support_call?(rate, concurrency)
			rate <= @max_rate && concurrency < 4
		end
	end
end