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