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 "./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 :monthly_price, :save_plan!
28 def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
29 :fwd, :transcription_enabled, :alternate_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 self.extract(customer_id, jid, **kwargs)
36 Customer.new(
37 customer_id, jid,
38 plan: CustomerPlan.extract(customer_id, kwargs),
39 **kwargs.slice(:balance, :sgx, :tndetails)
40 )
41 end
42
43 def initialize(
44 customer_id,
45 jid,
46 plan: CustomerPlan.new(customer_id),
47 balance: BigDecimal(0),
48 tndetails: {},
49 sgx: TrivialBackendSgxRepo.new.get(customer_id)
50 )
51 @plan = plan
52 @usage = CustomerUsage.new(customer_id)
53 @financials = CustomerFinancials.new(customer_id)
54 @customer_id = customer_id
55 @jid = jid
56 @balance = balance
57 @tndetails = tndetails
58 @sgx = sgx
59 end
60
61 def with_balance(balance)
62 self.class.new(
63 @customer_id, @jid,
64 plan: @plan, balance: balance,
65 tndetails: @tndetails, sgx: @sgx
66 )
67 end
68
69 def with_plan(plan_name)
70 self.class.new(
71 @customer_id, @jid,
72 plan: @plan.with_plan_name(plan_name),
73 balance: @balance, tndetails: @tndetails, sgx: @sgx
74 )
75 end
76
77 def unused_invites
78 promise = DB.query_defer(<<~SQL, [customer_id])
79 SELECT code FROM unused_invites WHERE creator_id=$1
80 SQL
81 promise.then { |result| result.map { |row| row["code"] } }
82 end
83
84 def stanza_to(stanza)
85 stanza = stanza.dup
86 stanza.to = jid.with(resource: stanza.to&.resource)
87 stanza.from ||= Blather::JID.new("")
88 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
89 block_given? ? yield(stanza) : (BLATHER << stanza)
90 end
91
92 def stanza_from(stanza)
93 BLATHER << @sgx.stanza(stanza)
94 end
95
96 def fetch_vcard_temp(from_tel=nil)
97 iq = Blather::Stanza::Iq::Vcard.new(:get)
98 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
99 stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
100 end
101
102 def ogm(from_tel=nil)
103 CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(from_tel) })
104 end
105
106 def sip_account
107 SipAccount.find(customer_id)
108 end
109
110 def reset_sip_account
111 sip_account.with_random_password.put
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 # kwargs are passed through for dependency injection from tests
123 def admin_info(**kwargs)
124 AdminInfo.for(self, @plan, **kwargs)
125 end
126
127 def info
128 CustomerInfo.for(self, @plan)
129 end
130
131 protected def_delegator :@plan, :expires_at
132end