~singpolyma/sgx-jmp

sgx-jmp/lib/trust_level.rb -rw-r--r-- 2.2 KiB
4ace3417Stephen Paul Weber Log errors from rev.ai 8 days 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require "delegate"

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 manual ? Manual.new(tl) : tl if tl
		end

		raise "No TrustLevel matched"
	end

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

	class Manual < SimpleDelegator
		def to_s
			"Manual(#{super})"
		end
	end

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

		def support_call?(*)
			false
		end

		def send_message?(*)
			false
		end

		def credit_card_transaction?(*)
			false
		end

		def to_s
			"Tomb"
		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

		def send_message?(messages_today)
			messages_today < 200
		end

		def credit_card_transaction?(amount, declines)
			amount <= 35 && declines <= 2
		end

		def to_s
			"Basement"
		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

		def send_message?(messages_today)
			messages_today < 700
		end

		def credit_card_transaction?(amount, declines)
			amount <= 500 && declines <= 3
		end

		def to_s
			"Paragon"
		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

		def send_message?(messages_today)
			messages_today < 500
		end

		def credit_card_transaction?(amount, declines)
			amount <= 100 && declines <= 2
		end

		def to_s
			"Customer"
		end
	end
end