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 "./invites_repo"
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 :monthly_price, :save_plan!
29 def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
30 :fwd, :transcription_enabled
31 def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage
32 def_delegators :@financials, :payment_methods, :btc_addresses,
33 :add_btc_address, :declines, :mark_decline,
34 :transactions
35
36 def self.extract(customer_id, jid, **kwargs)
37 Customer.new(
38 customer_id, jid,
39 plan: CustomerPlan.extract(customer_id, kwargs),
40 **kwargs.slice(:balance, :sgx, :tndetails)
41 )
42 end
43
44 def initialize(
45 customer_id,
46 jid,
47 plan: CustomerPlan.new(customer_id),
48 balance: BigDecimal(0),
49 tndetails: {},
50 sgx: TrivialBackendSgxRepo.new.get(customer_id)
51 )
52 @plan = plan
53 @usage = CustomerUsage.new(customer_id)
54 @financials = CustomerFinancials.new(customer_id)
55 @customer_id = customer_id
56 @jid = jid
57 @balance = balance
58 @tndetails = tndetails
59 @sgx = sgx
60 end
61
62 def with_balance(balance)
63 self.class.new(
64 @customer_id, @jid,
65 plan: @plan, balance: balance,
66 tndetails: @tndetails, sgx: @sgx
67 )
68 end
69
70 def with_plan(plan_name)
71 self.class.new(
72 @customer_id, @jid,
73 plan: @plan.with_plan_name(plan_name),
74 balance: @balance, tndetails: @tndetails, sgx: @sgx
75 )
76 end
77
78 def unused_invites
79 InvitesRepo.new(DB).unused_invites(customer_id)
80 end
81
82 def stanza_to(stanza)
83 stanza = stanza.dup
84 stanza.to = jid.with(resource: stanza.to&.resource)
85 stanza.from ||= Blather::JID.new("")
86 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
87 block_given? ? yield(stanza) : (BLATHER << stanza)
88 end
89
90 def stanza_from(stanza)
91 BLATHER << @sgx.stanza(stanza)
92 end
93
94 def fetch_pep(node, from_tel=nil)
95 iq = Blather::Stanza::PubSub::Items.new(:get)
96 iq.node = node
97 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
98 stanza_to(iq, &IQ_MANAGER.method(:write))
99 end
100
101 def ogm(from_tel=nil)
102 CustomerOGM.for(@sgx.ogm_url, -> { fetch_pep("urn:xmpp:vcard4", from_tel) })
103 end
104
105 def sip_account
106 SipAccount.find(customer_id)
107 end
108
109 def reset_sip_account
110 sip_account.with_random_password.put
111 end
112
113 def admin?
114 CONFIG[:admins].include?(jid.to_s)
115 end
116
117 def api
118 API.for(self)
119 end
120
121 # kwargs are passed through for dependency injection from tests
122 def admin_info(**kwargs)
123 AdminInfo.for(self, @plan, **kwargs)
124 end
125
126 def info
127 CustomerInfo.for(self, @plan)
128 end
129
130 protected def_delegator :@plan, :expires_at
131end