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_financials"
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, :feature_flags
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!, :auto_top_up_amount
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, :feature_flags, *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 feature_flags: [],
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 @feature_flags = feature_flags
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 unused_invites
91 InvitesRepo.new(DB).unused_invites(customer_id)
92 end
93
94 def stanza_to(stanza)
95 stanza = stanza.dup
96 stanza.to = jid.with(resource: stanza.to&.resource)
97 stanza.from ||= Blather::JID.new("")
98 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
99 block_given? ? yield(stanza) : (BLATHER << stanza)
100 end
101
102 def stanza_from(stanza)
103 BLATHER << @sgx.stanza(stanza)
104 end
105
106 def fetch_pep(node, from_tel=nil)
107 iq = Blather::Stanza::PubSub::Items.new(:get)
108 iq.node = node
109 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
110 stanza_to(iq, &IQ_MANAGER.method(:write))
111 end
112
113 def ogm(from_tel=nil)
114 fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) }
115 CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard)
116 end
117
118 def sip_account
119 SipAccount.find(customer_id)
120 end
121
122 def reset_sip_account
123 sip_account.with_random_password.put
124 end
125
126 def admin?
127 CONFIG[:admins].include?(jid.to_s)
128 end
129
130 class ChildCustomer < Customer
131 def initialize(*args, parent_customer_id:, **kwargs)
132 super(*args, **kwargs)
133 @parent_customer_id = parent_customer_id
134 end
135
136 def billing_customer_id
137 @parent_customer_id
138 end
139
140 def billing_customer(repo=CustomerRepo.new)
141 repo.find(billing_customer_id)
142 end
143 end
144end