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
24 def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
25 :currency, :merchant_account, :plan_name, :auto_top_up_amount
26 def_delegators :@sgx, :register!, :registered?, :set_ogm_url,
27 :fwd, :transcription_enabled
28 def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage
29
30 def initialize(
31 customer_id,
32 jid,
33 plan: CustomerPlan.new(customer_id),
34 balance: BigDecimal(0),
35 sgx: TrivialBackendSgxRepo.new.get(customer_id)
36 )
37 @plan = plan
38 @usage = CustomerUsage.new(customer_id)
39 @customer_id = customer_id
40 @jid = jid
41 @balance = balance
42 @sgx = sgx
43 end
44
45 def with_plan(plan_name)
46 self.class.new(
47 @customer_id,
48 @jid,
49 plan: @plan.with_plan_name(plan_name),
50 balance: @balance,
51 sgx: @sgx
52 )
53 end
54
55 def payment_methods
56 BRAINTREE
57 .customer
58 .find(@customer_id)
59 .catch { OpenStruct.new(payment_methods: []) }
60 .then(PaymentMethods.method(:for_braintree_customer))
61 end
62
63 def unused_invites
64 promise = DB.query_defer(<<~SQL, [customer_id])
65 SELECT code FROM unused_invites WHERE creator_id=$1
66 SQL
67 promise.then { |result| result.map { |row| row["code"] } }
68 end
69
70 def stanza_to(stanza)
71 stanza = stanza.dup
72 stanza.to = jid.with(resource: stanza.to&.resource)
73 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
74 block_given? ? yield(stanza) : (BLATHER << stanza)
75 end
76
77 def stanza_from(stanza)
78 BLATHER << @sgx.stanza(stanza)
79 end
80
81 def fetch_vcard_temp(from_tel=nil)
82 iq = Blather::Stanza::Iq::Vcard.new(:get)
83 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
84 stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
85 end
86
87 def tndetails
88 return unless registered?
89
90 @tndetails ||=
91 BandwidthIris::Tn.new(telephone_number: registered?.phone).get_details
92 end
93
94 def ogm(from_tel=nil)
95 CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(from_tel) })
96 end
97
98 def sip_account
99 SipAccount.find(customer_id)
100 end
101
102 def reset_sip_account
103 sip_account.with_random_password.put
104 end
105
106 def btc_addresses
107 REDIS.smembers("jmp_customer_btc_addresses-#{customer_id}")
108 end
109
110 def add_btc_address
111 REDIS.spopsadd([
112 "jmp_available_btc_addresses",
113 "jmp_customer_btc_addresses-#{customer_id}"
114 ]).then do |addr|
115 ELECTRUM.notify(
116 addr,
117 CONFIG[:electrum_notify_url].call(addr, customer_id)
118 )
119 addr
120 end
121 end
122
123 def admin?
124 CONFIG[:admins].include?(jid.to_s)
125 end
126
127 def api
128 API.for(self)
129 end
130
131 def admin_info
132 AdminInfo.for(self, @plan, expires_at)
133 end
134
135 def info
136 CustomerInfo.for(self, @plan, expires_at)
137 end
138
139 protected def_delegator :@plan, :expires_at
140end