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