customer.rb

  1# frozen_string_literal: true
  2
  3require "forwardable"
  4
  5require_relative "./blather_ext"
  6require_relative "./customer_usage"
  7require_relative "./customer_plan"
  8require_relative "./customer_ogm"
  9require_relative "./customer_finacials"
 10require_relative "./backend_sgx"
 11require_relative "./invites_repo"
 12require_relative "./payment_methods"
 13require_relative "./plan"
 14require_relative "./proxied_jid"
 15require_relative "./sip_account"
 16require_relative "./trivial_backend_sgx_repo"
 17
 18class Customer
 19	extend Forwardable
 20
 21	attr_reader :customer_id, :balance, :jid, :tndetails
 22	alias billing_customer_id customer_id
 23
 24	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
 25	               :currency, :merchant_account, :plan_name, :minute_limit,
 26	               :message_limit, :monthly_overage_limit, :activation_date,
 27	               :expires_at, :monthly_price, :save_plan!
 28	def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
 29	               :fwd, :transcription_enabled
 30	def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage
 31	def_delegators :@financials, :payment_methods, :btc_addresses,
 32	               :add_btc_address, :declines, :mark_decline,
 33	               :transactions
 34
 35	def self.extract(customer_id, jid, **kwargs)
 36		klass, *keys = if kwargs[:parent_customer_id]
 37			[ChildCustomer, :parent_customer_id]
 38		else
 39			[Customer]
 40		end
 41
 42		klass.new(
 43			customer_id, jid,
 44			plan: CustomerPlan.extract(customer_id, kwargs),
 45			**kwargs.slice(:balance, :sgx, :tndetails, *keys)
 46		)
 47	end
 48
 49	def initialize(
 50		customer_id,
 51		jid,
 52		plan: CustomerPlan.new(customer_id),
 53		balance: BigDecimal(0),
 54		tndetails: {},
 55		sgx: TrivialBackendSgxRepo.new.get(customer_id)
 56	)
 57		@plan = plan
 58		@usage = CustomerUsage.new(customer_id)
 59		@financials = CustomerFinancials.new(customer_id)
 60		@customer_id = customer_id
 61		@jid = jid
 62		@balance = balance
 63		@tndetails = tndetails
 64		@sgx = sgx
 65	end
 66
 67	def with_balance(balance)
 68		self.class.new(
 69			@customer_id, @jid,
 70			plan: @plan, balance: balance,
 71			tndetails: @tndetails, sgx: @sgx
 72		)
 73	end
 74
 75	def with_plan(plan_name)
 76		self.class.new(
 77			@customer_id, @jid,
 78			plan: @plan.with_plan_name(plan_name),
 79			balance: @balance, tndetails: @tndetails, sgx: @sgx
 80		)
 81	end
 82
 83	def billing_customer(*)
 84		EMPromise.resolve(self)
 85	end
 86
 87	def auto_top_up_amount
 88		if @plan.auto_top_up_amount.positive? &&
 89		   balance < -@plan.auto_top_up_amount + 5
 90			-balance + @plan.auto_top_up_amount
 91		else
 92			@plan.auto_top_up_amount
 93		end
 94	end
 95
 96	def unused_invites
 97		InvitesRepo.new(DB).unused_invites(customer_id)
 98	end
 99
100	def stanza_to(stanza)
101		stanza = stanza.dup
102		stanza.to = jid.with(resource: stanza.to&.resource)
103		stanza.from ||= Blather::JID.new("")
104		stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
105		block_given? ? yield(stanza) : (BLATHER << stanza)
106	end
107
108	def stanza_from(stanza)
109		BLATHER << @sgx.stanza(stanza)
110	end
111
112	def fetch_pep(node, from_tel=nil)
113		iq = Blather::Stanza::PubSub::Items.new(:get)
114		iq.node = node
115		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
116		stanza_to(iq, &IQ_MANAGER.method(:write))
117	end
118
119	def ogm(from_tel=nil)
120		CustomerOGM.for(@sgx.ogm_url, -> { fetch_pep("urn:xmpp:vcard4", from_tel) })
121	end
122
123	def sip_account
124		SipAccount.find(customer_id)
125	end
126
127	def reset_sip_account
128		sip_account.with_random_password.put
129	end
130
131	def admin?
132		CONFIG[:admins].include?(jid.to_s)
133	end
134
135	class ChildCustomer < Customer
136		def initialize(*args, parent_customer_id:, **kwargs)
137			super(*args, **kwargs)
138			@parent_customer_id = parent_customer_id
139		end
140
141		def billing_customer_id
142			@parent_customer_id
143		end
144
145		def billing_customer(repo=CustomerRepo.new)
146			repo.find(billing_customer_id)
147		end
148	end
149end