1# frozen_string_literal: true
2
3require "forwardable"
4
5require_relative "./api"
6require_relative "./blather_ext"
7require_relative "./customer_info"
8require_relative "./customer_ogm"
9require_relative "./customer_plan"
10require_relative "./customer_usage"
11require_relative "./backend_sgx"
12require_relative "./ibr"
13require_relative "./payment_methods"
14require_relative "./plan"
15require_relative "./proxied_jid"
16require_relative "./sip_account"
17require_relative "./trivial_backend_sgx_repo"
18
19class Customer
20 extend Forwardable
21
22 attr_reader :customer_id, :balance, :jid
23 def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
24 :currency, :merchant_account, :plan_name, :auto_top_up_amount
25 def_delegators :@sgx, :register!, :registered?, :set_ogm_url,
26 :set_fwd_timeout, :fwd, :transcription_enabled
27 def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage
28
29 def initialize(
30 customer_id,
31 jid,
32 plan: CustomerPlan.new(customer_id),
33 balance: BigDecimal(0),
34 sgx: TrivialBackendSgxRepo.new.get(customer_id)
35 )
36 @plan = plan
37 @usage = CustomerUsage.new(customer_id)
38 @customer_id = customer_id
39 @jid = jid
40 @balance = balance
41 @sgx = sgx
42 end
43
44 def with_plan(plan_name)
45 self.class.new(
46 @customer_id,
47 @jid,
48 plan: @plan.with_plan_name(plan_name),
49 balance: @balance,
50 sgx: @sgx
51 )
52 end
53
54 def payment_methods
55 BRAINTREE
56 .customer
57 .find(@customer_id)
58 .catch { OpenStruct.new(payment_methods: []) }
59 .then(PaymentMethods.method(:for_braintree_customer))
60 end
61
62 def unused_invites
63 promise = DB.query_defer(<<~SQL, [customer_id])
64 SELECT code FROM unused_invites WHERE creator_id=$1
65 SQL
66 promise.then { |result| result.map { |row| row["code"] } }
67 end
68
69 def stanza_to(stanza)
70 stanza = stanza.dup
71 stanza.to = jid.with(resource: stanza.to&.resource)
72 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
73 block_given? ? yield(stanza) : (BLATHER << stanza)
74 end
75
76 def stanza_from(stanza)
77 BLATHER << @sgx.stanza(stanza)
78 end
79
80 def fetch_vcard_temp(from_tel=nil)
81 iq = Blather::Stanza::Iq::Vcard.new(:get)
82 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
83 stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
84 end
85
86 def ogm(from_tel=nil)
87 CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(from_tel) })
88 end
89
90 def sip_account
91 SipAccount.find(customer_id)
92 end
93
94 def reset_sip_account
95 sip_account.with_random_password.put
96 end
97
98 def btc_addresses
99 REDIS.smembers("jmp_customer_btc_addresses-#{customer_id}")
100 end
101
102 def add_btc_address
103 REDIS.spopsadd([
104 "jmp_available_btc_addresses",
105 "jmp_customer_btc_addresses-#{customer_id}"
106 ]).then do |addr|
107 ELECTRUM.notify(addr, CONFIG[:electrum_notify_url].call(addr, customer_id))
108 addr
109 end
110 end
111
112 def admin?
113 CONFIG[:admins].include?(jid.to_s)
114 end
115
116 def api
117 API.for(self)
118 end
119
120 def admin_info
121 AdminInfo.for(self, @plan, expires_at)
122 end
123
124 def info
125 CustomerInfo.for(self, @plan, expires_at)
126 end
127
128 protected def_delegator :@plan, :expires_at
129end