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