customer.rb

  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 "./payment_methods"
 14require_relative "./plan"
 15require_relative "./proxied_jid"
 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
 23
 24	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
 25	               :currency, :merchant_account, :plan_name, :minute_limit,
 26	               :message_limit, :auto_top_up_amount, :monthly_overage_limit,
 27	               :monthly_price, :save_plan!
 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	def_delegators :@financials, :payment_methods, :btc_addresses,
 32	               :add_btc_address, :declines, :mark_decline,
 33	               :transactions
 34
 35	def self.extract(customer_id, jid, **kwargs)
 36		Customer.new(
 37			customer_id, jid,
 38			plan: CustomerPlan.extract(customer_id, kwargs),
 39			**kwargs.slice(:balance, :sgx, :tndetails)
 40		)
 41	end
 42
 43	def initialize(
 44		customer_id,
 45		jid,
 46		plan: CustomerPlan.new(customer_id),
 47		balance: BigDecimal(0),
 48		tndetails: {},
 49		sgx: TrivialBackendSgxRepo.new.get(customer_id)
 50	)
 51		@plan = plan
 52		@usage = CustomerUsage.new(customer_id)
 53		@financials = CustomerFinancials.new(customer_id)
 54		@customer_id = customer_id
 55		@jid = jid
 56		@balance = balance
 57		@tndetails = tndetails
 58		@sgx = sgx
 59	end
 60
 61	def with_balance(balance)
 62		self.class.new(
 63			@customer_id, @jid,
 64			plan: @plan, balance: balance,
 65			tndetails: @tndetails, sgx: @sgx
 66		)
 67	end
 68
 69	def with_plan(plan_name)
 70		self.class.new(
 71			@customer_id, @jid,
 72			plan: @plan.with_plan_name(plan_name),
 73			balance: @balance, tndetails: @tndetails, sgx: @sgx
 74		)
 75	end
 76
 77	def unused_invites
 78		promise = DB.query_defer(<<~SQL, [customer_id])
 79			SELECT code FROM unused_invites WHERE creator_id=$1
 80		SQL
 81		promise.then { |result| result.map { |row| row["code"] } }
 82	end
 83
 84	def stanza_to(stanza)
 85		stanza = stanza.dup
 86		stanza.to = jid.with(resource: stanza.to&.resource)
 87		stanza.from ||= Blather::JID.new("")
 88		stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
 89		block_given? ? yield(stanza) : (BLATHER << stanza)
 90	end
 91
 92	def stanza_from(stanza)
 93		BLATHER << @sgx.stanza(stanza)
 94	end
 95
 96	def fetch_pep(node, from_tel=nil)
 97		iq = Blather::Stanza::PubSub::Items.new(:get)
 98		iq.node = node
 99		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
100		stanza_to(iq, &IQ_MANAGER.method(:write))
101	end
102
103	def ogm(from_tel=nil)
104		CustomerOGM.for(@sgx.ogm_url, -> { fetch_pep("urn:xmpp:vcard4", from_tel) })
105	end
106
107	def sip_account
108		SipAccount.find(customer_id)
109	end
110
111	def reset_sip_account
112		sip_account.with_random_password.put
113	end
114
115	def admin?
116		CONFIG[:admins].include?(jid.to_s)
117	end
118
119	def api
120		API.for(self)
121	end
122
123	# kwargs are passed through for dependency injection from tests
124	def admin_info(**kwargs)
125		AdminInfo.for(self, @plan, **kwargs)
126	end
127
128	def info
129		CustomerInfo.for(self, @plan)
130	end
131
132	protected def_delegator :@plan, :expires_at
133end