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, :deregister!, :register!, :registered?, :set_ogm_url,
 30	               :fwd, :transcription_enabled, :alternate_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 self.extract(customer_id, jid, **kwargs)
 37		Customer.new(
 38			customer_id, jid,
 39			plan: CustomerPlan.extract(customer_id, kwargs),
 40			**kwargs.slice(:balance, :sgx, :tndetails)
 41		)
 42	end
 43
 44	def initialize(
 45		customer_id,
 46		jid,
 47		plan: CustomerPlan.new(customer_id),
 48		balance: BigDecimal(0),
 49		tndetails: {},
 50		sgx: TrivialBackendSgxRepo.new.get(customer_id)
 51	)
 52		@plan = plan
 53		@usage = CustomerUsage.new(customer_id)
 54		@financials = CustomerFinancials.new(customer_id)
 55		@customer_id = customer_id
 56		@jid = jid
 57		@balance = balance
 58		@tndetails = tndetails
 59		@sgx = sgx
 60	end
 61
 62	def with_balance(balance)
 63		self.class.new(
 64			@customer_id, @jid,
 65			plan: @plan, balance: balance,
 66			tndetails: @tndetails, sgx: @sgx
 67		)
 68	end
 69
 70	def with_plan(plan_name)
 71		self.class.new(
 72			@customer_id, @jid,
 73			plan: @plan.with_plan_name(plan_name),
 74			balance: @balance, tndetails: @tndetails, sgx: @sgx
 75		)
 76	end
 77
 78	def unused_invites
 79		promise = DB.query_defer(<<~SQL, [customer_id])
 80			SELECT code FROM unused_invites WHERE creator_id=$1
 81		SQL
 82		promise.then { |result| result.map { |row| row["code"] } }
 83	end
 84
 85	def stanza_to(stanza)
 86		stanza = stanza.dup
 87		stanza.to = jid.with(resource: stanza.to&.resource)
 88		stanza.from ||= Blather::JID.new("")
 89		stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
 90		block_given? ? yield(stanza) : (BLATHER << stanza)
 91	end
 92
 93	def stanza_from(stanza)
 94		BLATHER << @sgx.stanza(stanza)
 95	end
 96
 97	def fetch_vcard_temp(from_tel=nil)
 98		iq = Blather::Stanza::Iq::Vcard.new(:get)
 99		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
100		stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
101	end
102
103	def ogm(from_tel=nil)
104		CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(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