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 "./port_out_pin"
16require_relative "./sip_account"
17require_relative "./trivial_backend_sgx_repo"
18
19class Customer
20 extend Forwardable
21
22 attr_reader :customer_id, :balance, :jid, :tndetails, :feature_flags
23 alias billing_customer_id customer_id
24
25 def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
26 :currency, :merchant_account, :plan_name, :minute_limit,
27 :message_limit, :monthly_overage_limit, :activation_date,
28 :expires_at, :monthly_price, :save_plan!, :auto_top_up_amount,
29 :extend_plan, :status
30 def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
31 :fwd, :transcription_enabled, :commands
32 def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage,
33 :calling_charges_this_month
34 def_delegators :@financials, :payment_methods, :declines, :mark_decline,
35 :btc_addresses, :add_btc_address, :transactions,
36 :bch_addresses, :add_bch_address
37
38 def self.extract(customer_id, jid, **kwargs)
39 (kwargs[:parent_customer_id] ? ChildCustomer : Customer).new(
40 customer_id, jid,
41 plan: CustomerPlan.extract(customer_id: customer_id, **kwargs),
42 **kwargs.slice(:balance, :sgx, :tndetails, :feature_flags)
43 )
44 end
45
46 def self.created(customer_id, jid, repo:, **kwargs)
47 jid = Blather::JID.new(jid)
48 plan = CustomerPlan.default(customer_id, jid)
49 if plan.parent_customer_id
50 # Parent may have a balance, so look it up
51 plan.save_plan!.then { repo.find(customer_id) }
52 else
53 new(customer_id, jid, plan: plan, **kwargs)
54 end
55 end
56
57 def initialize(
58 customer_id, jid,
59 sgx:,
60 plan: CustomerPlan.new(customer_id),
61 balance: BigDecimal(0), tndetails: {}, feature_flags: []
62 )
63 @plan = plan
64 @usage = CustomerUsage.new(customer_id)
65 @financials = CustomerFinancials.new(customer_id)
66 @customer_id = customer_id
67 @jid = jid
68 @balance = balance
69 @tndetails = tndetails
70 @feature_flags = feature_flags
71 @sgx = sgx
72 end
73
74 def with_balance(balance)
75 self.class.new(
76 @customer_id, @jid,
77 plan: @plan, balance: balance,
78 tndetails: @tndetails, sgx: @sgx
79 )
80 end
81
82 def with_plan(plan_name, **kwargs)
83 self.class.new(
84 @customer_id, @jid,
85 plan: @plan.with(plan_name: plan_name, **kwargs),
86 balance: @balance, tndetails: @tndetails, sgx: @sgx
87 )
88 end
89
90 def billing_customer(*)
91 EMPromise.resolve(self)
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 from_tel = nil unless from_tel.to_s.start_with?("+")
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 sgx
124 @sgx.jid
125 end
126
127 def port_out_pin
128 PortOutPin.new(@sgx)
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 billing_customer_id
141 @plan.parent_customer_id
142 end
143
144 def billing_customer(repo=CustomerRepo.new)
145 repo.find(billing_customer_id)
146 end
147 end
148end