~singpolyma/sgx-jmp

ref: 75658ba14b40d551488dae800e9a7d2bfeb239a8 sgx-jmp/lib/customer_info.rb -rw-r--r-- 2.3 KiB
75658ba1Stephen Paul Weber Use FormTemplate for activation form 8 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
# frozen_string_literal: true

require "bigdecimal"
require "value_semantics/monkey_patched"
require_relative "proxied_jid"
require_relative "customer_plan"

class CustomerInfo
	value_semantics do
		plan CustomerPlan
		auto_top_up_amount Integer
		tel Either(String, nil)
		balance BigDecimal
		expires_at Either(Time, nil)
		cnam Either(String, nil)
	end

	def self.for(customer, plan, expires_at)
		plan.auto_top_up_amount.then do |auto_top_up_amount|
			new(
				plan: plan,
				auto_top_up_amount: auto_top_up_amount,
				tel: customer.registered? ? customer.registered?.phone : nil,
				balance: customer.balance,
				expires_at: expires_at,
				cnam: customer.tndetails&.dig(:features, :lidb, :subscriber_information)
			)
		end
	end

	def account_status
		if plan.plan_name.nil?
			"Transitional"
		elsif plan.active?
			"Active"
		else
			"Expired"
		end
	end

	def next_renewal
		return unless expires_at

		{ var: "Next renewal", value: expires_at.strftime("%Y-%m-%d") }
	end

	def monthly_amount
		return unless plan.monthly_price

		{ var: "Renewal", value: "$%.4f / month" % plan.monthly_price }
	end

	def auto_top_up
		{
			label: "Auto Top-up"
		}.merge(
			if auto_top_up_amount.positive?
				{ value: auto_top_up_amount.to_s, var: "auto_top_up_amount" }
			else
				{ value: "No" }
			end
		)
	end

	def fields
		[
			{ var: "Account Status", value: account_status },
			({ var: "tel", label: "Phone Number", value: tel } if tel),
			({ var: "lidb_name", label: "CNAM", value: cnam } if cnam),
			{ var: "Balance", value: "$%.4f" % balance },
			monthly_amount,
			next_renewal,
			auto_top_up,
			{ var: "Currency", value: (plan.currency || "No Currency").to_s }
		].compact
	end
end

class AdminInfo
	value_semantics do
		jid ProxiedJID, coerce: ProxiedJID.method(:new)
		customer_id String
		info CustomerInfo
		api API
	end

	def self.for(customer, plan, expires_at)
		EMPromise.all([
			CustomerInfo.for(customer, plan, expires_at),
			customer.api
		]).then do |info, api_value|
			new(
				jid: customer.jid,
				customer_id: customer.customer_id,
				info: info, api: api_value
			)
		end
	end

	def fields
		info.fields + [
			{ var: "JID", value: jid.unproxied.to_s },
			{ var: "Cheo JID", value: jid.to_s },
			{ var: "Customer ID", value: customer_id },
			{ var: "Plan", value: info.plan.plan_name || "No Plan" },
			{ var: "API", value: api.to_s }
		]
	end
end