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