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 TOP_UP_ADJUSTEMENT_THRESHOLD = 5
37
38 def self.extract(customer_id, jid, **kwargs)
39 klass, *keys = if kwargs[:parent_customer_id]
40 [ChildCustomer, :parent_customer_id]
41 else
42 [Customer]
43 end
44
45 klass.new(
46 customer_id, jid,
47 plan: CustomerPlan.extract(customer_id, **kwargs),
48 **kwargs.slice(:balance, :sgx, :tndetails, *keys)
49 )
50 end
51
52 def initialize(
53 customer_id,
54 jid,
55 plan: CustomerPlan.new(customer_id),
56 balance: BigDecimal(0),
57 tndetails: {},
58 sgx: TrivialBackendSgxRepo.new.get(customer_id)
59 )
60 @plan = plan
61 @usage = CustomerUsage.new(customer_id)
62 @financials = CustomerFinancials.new(customer_id)
63 @customer_id = customer_id
64 @jid = jid
65 @balance = balance
66 @tndetails = tndetails
67 @sgx = sgx
68 end
69
70 def with_balance(balance)
71 self.class.new(
72 @customer_id, @jid,
73 plan: @plan, balance: balance,
74 tndetails: @tndetails, sgx: @sgx
75 )
76 end
77
78 def with_plan(plan_name)
79 self.class.new(
80 @customer_id, @jid,
81 plan: @plan.with_plan_name(plan_name),
82 balance: @balance, tndetails: @tndetails, sgx: @sgx
83 )
84 end
85
86 def billing_customer(*)
87 EMPromise.resolve(self)
88 end
89
90 def auto_top_up_amount
91 if @plan.auto_top_up_amount.positive? &&
92 balance + @plan.auto_top_up_amount < TOP_UP_ADJUSTEMENT_THRESHOLD
93 -balance + @plan.auto_top_up_amount # amount needed to set balance to @plan.auto_top_up_amount
94 else
95 @plan.auto_top_up_amount
96 end
97 end
98
99 def unused_invites
100 InvitesRepo.new(DB).unused_invites(customer_id)
101 end
102
103 def stanza_to(stanza)
104 stanza = stanza.dup
105 stanza.to = jid.with(resource: stanza.to&.resource)
106 stanza.from ||= Blather::JID.new("")
107 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
108 block_given? ? yield(stanza) : (BLATHER << stanza)
109 end
110
111 def stanza_from(stanza)
112 BLATHER << @sgx.stanza(stanza)
113 end
114
115 def fetch_pep(node, from_tel=nil)
116 iq = Blather::Stanza::PubSub::Items.new(:get)
117 iq.node = node
118 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
119 stanza_to(iq, &IQ_MANAGER.method(:write))
120 end
121
122 def ogm(from_tel=nil)
123 fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) }
124 CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard)
125 end
126
127 def sip_account
128 SipAccount.find(customer_id)
129 end
130
131 def reset_sip_account
132 sip_account.with_random_password.put
133 end
134
135 def admin?
136 CONFIG[:admins].include?(jid.to_s)
137 end
138
139 class ChildCustomer < Customer
140 def initialize(*args, parent_customer_id:, **kwargs)
141 super(*args, **kwargs)
142 @parent_customer_id = parent_customer_id
143 end
144
145 def billing_customer_id
146 @parent_customer_id
147 end
148
149 def billing_customer(repo=CustomerRepo.new)
150 repo.find(billing_customer_id)
151 end
152 end
153end