customer_repo.rb

 1# frozen_string_literal: true
 2
 3require_relative "customer"
 4require_relative "legacy_customer"
 5require_relative "polyfill"
 6
 7class CustomerRepo
 8	def initialize(redis: REDIS, db: DB, braintree: BRAINTREE)
 9		@redis = redis
10		@db = db
11		@braintree = braintree
12	end
13
14	def find(customer_id)
15		@redis.get("jmp_customer_jid-#{customer_id}").then do |jid|
16			raise "No jid" unless jid
17			find_inner(customer_id, jid)
18		end
19	end
20
21	def find_by_jid(jid)
22		if jid.to_s =~ /\Acustomer_(.+)@jmp.chat\Z/
23			find($1)
24		else
25			@redis.get("jmp_customer_id-#{jid}").then { |customer_id|
26				raise "No customer id" unless customer_id
27				find_inner(customer_id, jid)
28			}.catch do
29				find_legacy_customer(jid)
30			end
31		end
32	end
33
34	def find_by_tel(tel)
35		@redis.get("catapult_jid-#{tel}").then do |jid|
36			raise "No jid" unless jid
37			find_by_jid(jid)
38		end
39	end
40
41	def create(jid)
42		@braintree.customer.create.then do |result|
43			raise "Braintree customer create failed" unless result.success?
44			cid = result.customer.id
45			@redis.msetnx(
46				"jmp_customer_id-#{jid}", cid, "jmp_customer_jid-#{cid}", jid
47			).then do |redis_result|
48				raise "Saving new customer to redis failed" unless redis_result == 1
49				Customer.new(cid, Blather::JID.new(jid))
50			end
51		end
52	end
53
54protected
55
56	def find_legacy_customer(jid)
57		@redis.lindex("catapult_cred-#{jid}", 3).then do |tel|
58			raise "No customer" unless tel
59			LegacyCustomer.new(Blather::JID.new(jid), tel)
60		end
61	end
62
63	def hydrate_plan(customer_id, raw_customer)
64		raw_customer.dup.tap do |data|
65			data[:plan] = CustomerPlan.new(
66				customer_id,
67				plan: data.delete(:plan_name)&.then(&Plan.method(:for)),
68				expires_at: data.delete(:expires_at)
69			)
70		end
71	end
72
73	def find_inner(customer_id, jid)
74		result = @db.query_defer(<<~SQL, [customer_id])
75			SELECT COALESCE(balance,0) AS balance, plan_name, expires_at
76			FROM customer_plans LEFT JOIN balances USING (customer_id)
77			WHERE customer_id=$1 LIMIT 1
78		SQL
79		result.then do |rows|
80			data = hydrate_plan(customer_id, rows.first&.transform_keys(&:to_sym) || {})
81			Customer.new(customer_id, Blather::JID.new(jid), **data)
82		end
83	end
84end