~singpolyma/sgx-jmp

ref: 45c07210a934bd61da046903195e3ab1688ebf3b sgx-jmp/lib/customer_repo.rb -rw-r--r-- 4.4 KiB
45c07210Stephen Paul Weber Proc !== method 1 year, 2 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true

require "lazy_object"
require "value_semantics/monkey_patched"

require_relative "bandwidth_tn_repo"
require_relative "customer"
require_relative "polyfill"

class CustomerRepo
	class NotFound < RuntimeError; end

	value_semantics do
		set_user          Proc,       default: ->(**) {}, coerce: :to_proc.to_proc
		redis             Anything(), default: LazyObject.new { REDIS }
		db                Anything(), default: LazyObject.new { DB }
		braintree         Anything(), default: LazyObject.new { BRAINTREE }
		sgx_repo          Anything(), default: TrivialBackendSgxRepo.new
		bandwidth_tn_repo Anything(), default: BandwidthTnRepo.new
	end

	module QueryKey
		def self.for(s)
			case s
			when Blather::JID
				JID.for(s)
			when /\Axmpp:(.*)/
				JID.for($1)
			when /\A(?:tel:)?(\+\d+)\Z/
				Tel.new($1)
			else
				ID.new(s)
			end
		end

		ID = Struct.new(:customer_id) do
			def keys(redis)
				redis.get("jmp_customer_jid-#{customer_id}").then do |jid|
					raise NotFound, "No jid" unless jid

					[customer_id, jid]
				end
			end
		end

		JID = Struct.new(:jid) do
			def self.for(jid)
				if jid.to_s =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/
					ID.new($1)
				else
					new(jid)
				end
			end

			def keys(redis)
				redis.get("jmp_customer_id-#{jid}").then do |customer_id|
					raise NotFound, "No customer" unless customer_id

					[customer_id, jid]
				end
			end
		end

		Tel = Struct.new(:tel) do
			def keys(redis)
				redis.get("catapult_jid-#{tel}").then do |jid|
					raise NotFound, "No jid" unless jid

					JID.for(jid).keys(redis)
				end
			end
		end
	end

	def find(customer_id)
		set_user.call(customer_id: customer_id)
		QueryKey::ID.new(customer_id).keys(redis).then { |k| find_inner(*k) }
	end

	def find_by_jid(jid)
		set_user.call(jid: jid)
		QueryKey::JID.for(jid).keys(redis).then { |k| find_inner(*k) }
	end

	def find_by_tel(tel)
		set_user.call(tel: tel)
		QueryKey::Tel.new(tel).keys(redis).then { |k| find_inner(*k) }
	end

	def find_by_format(s)
		set_user.call(q: s)
		QueryKey.for(s).keys(redis).then { |k| find_inner(*k) }
	end

	def create(jid)
		@braintree.customer.create.then do |result|
			raise "Braintree customer create failed" unless result.success?

			cid = result.customer.id
			@redis.msetnx(
				"jmp_customer_id-#{jid}", cid, "jmp_customer_jid-#{cid}", jid
			).then do |redis_result|
				raise "Saving new customer to redis failed" unless redis_result == 1

				Customer.new(cid, Blather::JID.new(jid), sgx: new_sgx(cid))
			end
		end
	end

	def put_lidb_name(customer, lidb_name)
		@bandwidth_tn_repo.put_lidb_name(customer.registered?.phone, lidb_name)
	end

	def put_transcription_enabled(customer, enabled)
		@sgx_repo.put_transcription_enabled(customer.customer_id, enabled)
	end

	def put_fwd(customer, customer_fwd)
		tel = customer.registered?.phone
		@sgx_repo.put_fwd(customer.customer_id, tel, customer_fwd)
	end

	def put_monthly_overage_limit(customer, limit)
		k = "jmp_customer_monthly_overage_limit-#{customer.customer_id}"
		@redis.set(k, limit)
	end

protected

	def new_sgx(customer_id)
		TrivialBackendSgxRepo.new.get(customer_id).with(registered?: false)
	end

	def mget(*keys)
		@redis.mget(*keys).then { |values| Hash[keys.zip(values.map(&:to_i))] }
	end

	def fetch_redis(customer_id)
		mget(
			"jmp_customer_auto_top_up_amount-#{customer_id}",
			"jmp_customer_monthly_overage_limit-#{customer_id}"
		).then { |r|
			r.transform_keys { |k| k.match(/^jmp_customer_([^-]+)/)[1].to_sym }
		}
	end

	SQL = <<~SQL
		SELECT COALESCE(balance,0) AS balance, plan_name, expires_at
		FROM customer_plans LEFT JOIN balances USING (customer_id)
		WHERE customer_id=$1 LIMIT 1
	SQL

	def fetch_sql(customer_id)
		@db.query_defer(SQL, [customer_id]).then do |rows|
			rows.first&.transform_keys(&:to_sym) || {}
		end
	end

	def fetch_all(customer_id)
		EMPromise.all([
			@sgx_repo.get(customer_id),
			fetch_sql(customer_id),
			fetch_redis(customer_id)
		]).then { |sgx, sql, redis| [sgx, sql.merge(redis)] }
	end

	def tndetails(sgx)
		return unless sgx.registered?

		LazyObject.new { @bandwidth_tn_repo.find(sgx.registered?.phone) }
	end

	def find_inner(customer_id, jid)
		set_user.call(customer_id: customer_id, jid: jid)
		fetch_all(customer_id).then do |(sgx, data)|
			Customer.new(
				customer_id, Blather::JID.new(jid),
				sgx: sgx, tndetails: tndetails(sgx), plan: CustomerPlan.for(
					customer_id,
					**data.reject { |(k, _)| k == :balance }
				), **data.slice(:balance)
			)
		end
	end
end