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