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