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