customer.rb

  1# frozen_string_literal: true
  2
  3require "forwardable"
  4
  5require_relative "./api"
  6require_relative "./blather_ext"
  7require_relative "./customer_info"
  8require_relative "./customer_ogm"
  9require_relative "./customer_plan"
 10require_relative "./customer_usage"
 11require_relative "./backend_sgx"
 12require_relative "./ibr"
 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	def_delegators :@sgx, :register!, :registered?, :set_ogm_url,
 28	               :fwd, :transcription_enabled
 29	def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage
 30
 31	def initialize(
 32		customer_id,
 33		jid,
 34		plan: CustomerPlan.new(customer_id),
 35		balance: BigDecimal(0),
 36		tndetails: {},
 37		sgx: TrivialBackendSgxRepo.new.get(customer_id)
 38	)
 39		@plan = plan
 40		@usage = CustomerUsage.new(customer_id)
 41		@customer_id = customer_id
 42		@jid = jid
 43		@balance = balance
 44		@tndetails = tndetails
 45		@sgx = sgx
 46	end
 47
 48	def with_plan(plan_name)
 49		self.class.new(
 50			@customer_id,
 51			@jid,
 52			plan: @plan.with_plan_name(plan_name),
 53			balance: @balance,
 54			sgx: @sgx
 55		)
 56	end
 57
 58	def payment_methods
 59		BRAINTREE
 60			.customer
 61			.find(@customer_id)
 62			.catch { OpenStruct.new(payment_methods: []) }
 63			.then(PaymentMethods.method(:for_braintree_customer))
 64	end
 65
 66	def unused_invites
 67		promise = DB.query_defer(<<~SQL, [customer_id])
 68			SELECT code FROM unused_invites WHERE creator_id=$1
 69		SQL
 70		promise.then { |result| result.map { |row| row["code"] } }
 71	end
 72
 73	def stanza_to(stanza)
 74		stanza = stanza.dup
 75		stanza.to = jid.with(resource: stanza.to&.resource)
 76		stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
 77		block_given? ? yield(stanza) : (BLATHER << stanza)
 78	end
 79
 80	def stanza_from(stanza)
 81		BLATHER << @sgx.stanza(stanza)
 82	end
 83
 84	def fetch_vcard_temp(from_tel=nil)
 85		iq = Blather::Stanza::Iq::Vcard.new(:get)
 86		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
 87		stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
 88	end
 89
 90	def ogm(from_tel=nil)
 91		CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(from_tel) })
 92	end
 93
 94	def sip_account
 95		SipAccount.find(customer_id)
 96	end
 97
 98	def reset_sip_account
 99		sip_account.with_random_password.put
100	end
101
102	def btc_addresses
103		REDIS.smembers("jmp_customer_btc_addresses-#{customer_id}")
104	end
105
106	def add_btc_address
107		REDIS.spopsadd([
108			"jmp_available_btc_addresses",
109			"jmp_customer_btc_addresses-#{customer_id}"
110		]).then do |addr|
111			ELECTRUM.notify(
112				addr,
113				CONFIG[:electrum_notify_url].call(addr, customer_id)
114			)
115			addr
116		end
117	end
118
119	def admin?
120		CONFIG[:admins].include?(jid.to_s)
121	end
122
123	def api
124		API.for(self)
125	end
126
127	def admin_info
128		AdminInfo.for(self, @plan)
129	end
130
131	def info
132		CustomerInfo.for(self, @plan)
133	end
134
135	protected def_delegator :@plan, :expires_at
136end