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