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