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 find_by_jid(jid)
37 end
38 end
39
40 def create(jid)
41 @braintree.customer.create.then do |result|
42 raise "Braintree customer create failed" unless result.success?
43 cid = result.customer.id
44 @redis.msetnx(
45 "jmp_customer_id-#{jid}", cid, "jmp_customer_jid-#{cid}", jid
46 ).then do |redis_result|
47 raise "Saving new customer to redis failed" unless redis_result == 1
48 Customer.new(cid, Blather::JID.new(jid))
49 end
50 end
51 end
52
53protected
54
55 def find_legacy_customer(jid)
56 @redis.lindex("catapult_cred-#{jid}", 3).then do |tel|
57 raise "No customer" unless tel
58 LegacyCustomer.new(Blather::JID.new(jid), tel)
59 end
60 end
61
62 def hydrate_plan(customer_id, raw_customer)
63 raw_customer.dup.tap do |data|
64 data[:plan] = CustomerPlan.new(
65 customer_id,
66 plan: data.delete(:plan_name)&.then(&Plan.method(:for)),
67 expires_at: data.delete(:expires_at)
68 )
69 end
70 end
71
72 def find_inner(customer_id, jid)
73 result = @db.query_defer(<<~SQL, [customer_id])
74 SELECT COALESCE(balance,0) AS balance, plan_name, expires_at
75 FROM customer_plans LEFT JOIN balances USING (customer_id)
76 WHERE customer_id=$1 LIMIT 1
77 SQL
78 result.then do |rows|
79 data = hydrate_plan(customer_id, rows.first&.transform_keys(&:to_sym) || {})
80 Customer.new(customer_id, Blather::JID.new(jid), **data)
81 end
82 end
83end