1# frozen_string_literal: true
2
3require "forwardable"
4
5require_relative "./api"
6require_relative "./blather_ext"
7require_relative "./customer_usage"
8require_relative "./customer_plan"
9require_relative "./customer_ogm"
10require_relative "./customer_info"
11require_relative "./customer_finacials"
12require_relative "./backend_sgx"
13require_relative "./invites_repo"
14require_relative "./payment_methods"
15require_relative "./plan"
16require_relative "./proxied_jid"
17require_relative "./sip_account"
18require_relative "./trivial_backend_sgx_repo"
19
20class Customer
21 extend Forwardable
22
23 attr_reader :customer_id, :balance, :jid, :tndetails
24 alias billing_customer_id customer_id
25
26 def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
27 :currency, :merchant_account, :plan_name, :minute_limit,
28 :message_limit, :auto_top_up_amount, :monthly_overage_limit,
29 :monthly_price, :save_plan!
30 def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
31 :fwd, :transcription_enabled
32 def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage
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 (kwargs[:parent_customer_id] ? ChildCustomer : Customer).new(
39 customer_id, jid,
40 plan: CustomerPlan.extract(customer_id, kwargs),
41 **kwargs.slice(:balance, :sgx, :tndetails)
42 )
43 end
44
45 def initialize(
46 customer_id,
47 jid,
48 plan: CustomerPlan.new(customer_id),
49 balance: BigDecimal(0),
50 tndetails: {},
51 sgx: TrivialBackendSgxRepo.new.get(customer_id)
52 )
53 @plan = plan
54 @usage = CustomerUsage.new(customer_id)
55 @financials = CustomerFinancials.new(customer_id)
56 @customer_id = customer_id
57 @jid = jid
58 @balance = balance
59 @tndetails = tndetails
60 @sgx = sgx
61 end
62
63 def with_balance(balance)
64 self.class.new(
65 @customer_id, @jid,
66 plan: @plan, balance: balance,
67 tndetails: @tndetails, sgx: @sgx
68 )
69 end
70
71 def with_plan(plan_name)
72 self.class.new(
73 @customer_id, @jid,
74 plan: @plan.with_plan_name(plan_name),
75 balance: @balance, tndetails: @tndetails, sgx: @sgx
76 )
77 end
78
79 def billing_customer(*)
80 EMPromise.resolve(self)
81 end
82
83 def unused_invites
84 InvitesRepo.new(DB).unused_invites(customer_id)
85 end
86
87 def stanza_to(stanza)
88 stanza = stanza.dup
89 stanza.to = jid.with(resource: stanza.to&.resource)
90 stanza.from ||= Blather::JID.new("")
91 stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
92 block_given? ? yield(stanza) : (BLATHER << stanza)
93 end
94
95 def stanza_from(stanza)
96 BLATHER << @sgx.stanza(stanza)
97 end
98
99 def fetch_pep(node, from_tel=nil)
100 iq = Blather::Stanza::PubSub::Items.new(:get)
101 iq.node = node
102 iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
103 stanza_to(iq, &IQ_MANAGER.method(:write))
104 end
105
106 def ogm(from_tel=nil)
107 CustomerOGM.for(@sgx.ogm_url, -> { fetch_pep("urn:xmpp:vcard4", from_tel) })
108 end
109
110 def sip_account
111 SipAccount.find(customer_id)
112 end
113
114 def reset_sip_account
115 sip_account.with_random_password.put
116 end
117
118 def admin?
119 CONFIG[:admins].include?(jid.to_s)
120 end
121
122 def api
123 API.for(self)
124 end
125
126 # kwargs are passed through for dependency injection from tests
127 def admin_info(**kwargs)
128 AdminInfo.for(self, @plan, **kwargs)
129 end
130
131 def info
132 CustomerInfo.for(self, @plan)
133 end
134
135 protected def_delegator :@plan, :expires_at
136
137 class ChildCustomer < Customer
138 def initialize(*args, parent_customer_id:, **kwargs)
139 super(*args, **kwargs)
140 @parent_customer_id = parent_customer_id
141 end
142
143 def billing_customer_id
144 @parent_customer_id
145 end
146
147 def billing_customer(repo=CustomerRepo.new)
148 repo.find(billing_customer_id)
149 end
150 end
151end