1# frozen_string_literal: true
2
3require "forwardable"
4
5require_relative "./blather_ext"
6require_relative "./customer_usage"
7require_relative "./customer_plan"
8require_relative "./customer_ogm"
9require_relative "./customer_finacials"
10require_relative "./backend_sgx"
11require_relative "./invites_repo"
12require_relative "./payment_methods"
13require_relative "./plan"
14require_relative "./proxied_jid"
15require_relative "./sip_account"
16require_relative "./trivial_backend_sgx_repo"
17
18class Customer
19 extend Forwardable
20
21 attr_reader :customer_id, :balance, :jid, :tndetails
22 alias billing_customer_id customer_id
23
24 def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
25 :currency, :merchant_account, :plan_name, :minute_limit,
26 :message_limit, :monthly_overage_limit, :activation_date,
27 :expires_at, :monthly_price, :save_plan!
28 def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
29 :fwd, :transcription_enabled
30 def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage,
31 :calling_charges_this_month
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 klass, *keys = if kwargs[:parent_customer_id]
38 [ChildCustomer, :parent_customer_id]
39 else
40 [Customer]
41 end
42
43 klass.new(
44 customer_id, jid,
45 plan: CustomerPlan.extract(customer_id, **kwargs),
46 **kwargs.slice(:balance, :sgx, :tndetails, *keys)
47 )
48 end
49
50 def initialize(
51 customer_id,
52 jid,
53 plan: CustomerPlan.new(customer_id),
54 balance: BigDecimal(0),
55 tndetails: {},
56 sgx: TrivialBackendSgxRepo.new.get(customer_id)
57 )
58 @plan = plan
59 @usage = CustomerUsage.new(customer_id)
60 @financials = CustomerFinancials.new(customer_id)
61 @customer_id = customer_id
62 @jid = jid
63 @balance = balance
64 @tndetails = tndetails
65 @sgx = sgx
66 end
67
68 def with_balance(balance)
69 self.class.new(
70 @customer_id, @jid,
71 plan: @plan, balance: balance,
72 tndetails: @tndetails, sgx: @sgx
73 )
74 end
75
76 def with_plan(plan_name)
77 self.class.new(
78 @customer_id, @jid,
79 plan: @plan.with_plan_name(plan_name),
80 balance: @balance, tndetails: @tndetails, sgx: @sgx
81 )
82 end
83
84 def billing_customer(*)
85 EMPromise.resolve(self)
86 end
87
88 def auto_top_up_amount
89 if @plan.auto_top_up_amount.positive? &&
90 balance < -@plan.auto_top_up_amount + 5
91 -balance + @plan.auto_top_up_amount
92 else
93 @plan.auto_top_up_amount
94 end
95 end
96
97 def unused_invites
98 InvitesRepo.new(DB).unused_invites(customer_id)
99 end
100
101 def stanza_to(stanza)
102 stanza = stanza.dup
103 stanza.to = jid.with(resource: stanza.to&.resource)
104 stanza.from ||= Blather::JID.new("")
105 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
106 block_given? ? yield(stanza) : (BLATHER << stanza)
107 end
108
109 def stanza_from(stanza)
110 BLATHER << @sgx.stanza(stanza)
111 end
112
113 def fetch_pep(node, from_tel=nil)
114 iq = Blather::Stanza::PubSub::Items.new(:get)
115 iq.node = node
116 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
117 stanza_to(iq, &IQ_MANAGER.method(:write))
118 end
119
120 def ogm(from_tel=nil)
121 fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) }
122 CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard)
123 end
124
125 def sip_account
126 SipAccount.find(customer_id)
127 end
128
129 def reset_sip_account
130 sip_account.with_random_password.put
131 end
132
133 def admin?
134 CONFIG[:admins].include?(jid.to_s)
135 end
136
137 class ChildCustomer < Customer
138 def initialize(*args, parent_customer_id:, **kwargs)
139 super(*args, **kwargs)
140 @parent_customer_id = parent_customer_id
141 end
142
143 def billing_customer_id
144 @parent_customer_id
145 end
146
147 def billing_customer(repo=CustomerRepo.new)
148 repo.find(billing_customer_id)
149 end
150 end
151end