~singpolyma/sgx-jmp

sgx-jmp/lib/customer.rb -rw-r--r-- 3.7 KiB
94298f5dStephen Paul Weber Refactor alt top up to use FormTemplate 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true

require "forwardable"

require_relative "./blather_ext"
require_relative "./customer_usage"
require_relative "./customer_plan"
require_relative "./customer_ogm"
require_relative "./customer_finacials"
require_relative "./backend_sgx"
require_relative "./invites_repo"
require_relative "./payment_methods"
require_relative "./plan"
require_relative "./proxied_jid"
require_relative "./sip_account"
require_relative "./trivial_backend_sgx_repo"

class Customer
	extend Forwardable

	attr_reader :customer_id, :balance, :jid, :tndetails
	alias billing_customer_id customer_id

	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
	               :currency, :merchant_account, :plan_name, :minute_limit,
	               :message_limit, :monthly_overage_limit, :activation_date,
	               :expires_at, :monthly_price, :save_plan!
	def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
	               :fwd, :transcription_enabled
	def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage,
	               :calling_charges_this_month
	def_delegators :@financials, :payment_methods, :btc_addresses,
	               :add_btc_address, :declines, :mark_decline,
	               :transactions

	def self.extract(customer_id, jid, **kwargs)
		klass, *keys = if kwargs[:parent_customer_id]
			[ChildCustomer, :parent_customer_id]
		else
			[Customer]
		end

		klass.new(
			customer_id, jid,
			plan: CustomerPlan.extract(customer_id, **kwargs),
			**kwargs.slice(:balance, :sgx, :tndetails, *keys)
		)
	end

	def initialize(
		customer_id,
		jid,
		plan: CustomerPlan.new(customer_id),
		balance: BigDecimal(0),
		tndetails: {},
		sgx: TrivialBackendSgxRepo.new.get(customer_id)
	)
		@plan = plan
		@usage = CustomerUsage.new(customer_id)
		@financials = CustomerFinancials.new(customer_id)
		@customer_id = customer_id
		@jid = jid
		@balance = balance
		@tndetails = tndetails
		@sgx = sgx
	end

	def with_balance(balance)
		self.class.new(
			@customer_id, @jid,
			plan: @plan, balance: balance,
			tndetails: @tndetails, sgx: @sgx
		)
	end

	def with_plan(plan_name)
		self.class.new(
			@customer_id, @jid,
			plan: @plan.with_plan_name(plan_name),
			balance: @balance, tndetails: @tndetails, sgx: @sgx
		)
	end

	def billing_customer(*)
		EMPromise.resolve(self)
	end

	def auto_top_up_amount
		if @plan.auto_top_up_amount.positive? &&
		   balance < -@plan.auto_top_up_amount + 5
			-balance + @plan.auto_top_up_amount
		else
			@plan.auto_top_up_amount
		end
	end

	def unused_invites
		InvitesRepo.new(DB).unused_invites(customer_id)
	end

	def stanza_to(stanza)
		stanza = stanza.dup
		stanza.to = jid.with(resource: stanza.to&.resource)
		stanza.from ||= Blather::JID.new("")
		stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
		block_given? ? yield(stanza) : (BLATHER << stanza)
	end

	def stanza_from(stanza)
		BLATHER << @sgx.stanza(stanza)
	end

	def fetch_pep(node, from_tel=nil)
		iq = Blather::Stanza::PubSub::Items.new(:get)
		iq.node = node
		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
		stanza_to(iq, &IQ_MANAGER.method(:write))
	end

	def ogm(from_tel=nil)
		fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) }
		CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard)
	end

	def sip_account
		SipAccount.find(customer_id)
	end

	def reset_sip_account
		sip_account.with_random_password.put
	end

	def admin?
		CONFIG[:admins].include?(jid.to_s)
	end

	class ChildCustomer < Customer
		def initialize(*args, parent_customer_id:, **kwargs)
			super(*args, **kwargs)
			@parent_customer_id = parent_customer_id
		end

		def billing_customer_id
			@parent_customer_id
		end

		def billing_customer(repo=CustomerRepo.new)
			repo.find(billing_customer_id)
		end
	end
end