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