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 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