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 :extend_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 :calling_charges_this_month
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, :feature_flags, *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 feature_flags: [],
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 @feature_flags = feature_flags
68 @sgx = sgx
69 end
70
71 def with_balance(balance)
72 self.class.new(
73 @customer_id, @jid,
74 plan: @plan, balance: balance,
75 tndetails: @tndetails, sgx: @sgx
76 )
77 end
78
79 def with_plan(plan_name)
80 self.class.new(
81 @customer_id, @jid,
82 plan: @plan.with_plan_name(plan_name),
83 balance: @balance, tndetails: @tndetails, sgx: @sgx
84 )
85 end
86
87 def billing_customer(*)
88 EMPromise.resolve(self)
89 end
90
91 def unused_invites
92 InvitesRepo.new(DB).unused_invites(customer_id)
93 end
94
95 def stanza_to(stanza)
96 stanza = stanza.dup
97 stanza.to = jid.with(resource: stanza.to&.resource)
98 stanza.from ||= Blather::JID.new("")
99 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
100 block_given? ? yield(stanza) : (BLATHER << stanza)
101 end
102
103 def stanza_from(stanza)
104 BLATHER << @sgx.stanza(stanza)
105 end
106
107 def fetch_pep(node, from_tel=nil)
108 iq = Blather::Stanza::PubSub::Items.new(:get)
109 iq.node = node
110 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
111 stanza_to(iq, &IQ_MANAGER.method(:write))
112 end
113
114 def ogm(from_tel=nil)
115 fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) }
116 CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard)
117 end
118
119 def sip_account
120 SipAccount.find(customer_id)
121 end
122
123 def reset_sip_account
124 sip_account.with_random_password.put
125 end
126
127 def admin?
128 CONFIG[:admins].include?(jid.to_s)
129 end
130
131 class ChildCustomer < Customer
132 def initialize(*args, parent_customer_id:, **kwargs)
133 super(*args, **kwargs)
134 @parent_customer_id = parent_customer_id
135 end
136
137 def billing_customer_id
138 @parent_customer_id
139 end
140
141 def billing_customer(repo=CustomerRepo.new)
142 repo.find(billing_customer_id)
143 end
144 end
145end