~singpolyma/sgx-jmp

ref: 23018db151ab4e6683c38ff1aeb0f588720e4d68 sgx-jmp/lib/customer_repo.rb -rw-r--r-- 3.1 KiB
23018db1Stephen Paul Weber Switch to rubocop 0.89.1 1 year, 7 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
# frozen_string_literal: true

require "lazy_object"

require_relative "customer"
require_relative "legacy_customer"
require_relative "polyfill"

class CustomerRepo
	class NotFound < RuntimeError; end

	def initialize(
		redis: LazyObject.new { REDIS },
		db: LazyObject.new { DB },
		braintree: LazyObject.new { BRAINTREE },
		sgx_repo: TrivialBackendSgxRepo.new
	)
		@redis = redis
		@db = db
		@braintree = braintree
		@sgx_repo = sgx_repo
	end

	def find(customer_id)
		@redis.get("jmp_customer_jid-#{customer_id}").then do |jid|
			raise NotFound, "No jid" unless jid

			find_inner(customer_id, jid)
		end
	end

	def find_by_jid(jid)
		if jid.to_s =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/
			find($1)
		else
			@redis.get("jmp_customer_id-#{jid}").then do |customer_id|
				next find_legacy_customer(jid) unless customer_id

				find_inner(customer_id, jid)
			end
		end
	end

	def find_by_tel(tel)
		@redis.get("catapult_jid-#{tel}").then do |jid|
			raise NotFound, "No jid" unless jid

			find_by_jid(jid)
		end
	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)
		BandwidthIris::Lidb.create(
			customer_order_id: customer.customer_id,
			lidb_tn_groups: { lidb_tn_group: {
				telephone_numbers: [customer.registered?.phone.sub(/\A\+1/, "")],
				subscriber_information: lidb_name,
				use_type: "RESIDENTIAL", visibility: "PUBLIC"
			} }
		)
	end

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

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

protected

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

	def find_legacy_customer(jid)
		@redis.lindex("catapult_cred-#{jid}", 3).then do |tel|
			raise NotFound, "No customer" unless tel

			LegacyCustomer.new(Blather::JID.new(jid), tel)
		end
	end

	def hydrate_plan(customer_id, raw_customer)
		raw_customer.dup.tap do |data|
			data[:plan] = CustomerPlan.new(
				customer_id,
				plan: data.delete(:plan_name)&.then(&Plan.method(:for)),
				expires_at: data.delete(:expires_at)
			)
		end
	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 find_inner(customer_id, jid)
		result = @db.query_defer(SQL, [customer_id])
		EMPromise.all([@sgx_repo.get(customer_id), result]).then do |(sgx, rows)|
			data = hydrate_plan(
				customer_id,
				rows.first&.transform_keys(&:to_sym) || {}
			)
			Customer.new(customer_id, Blather::JID.new(jid), sgx: sgx, **data)
		end
	end
end