~singpolyma/sgx-jmp

ref: c7c48663714307e3598dc14848e539fb8672b0b7 sgx-jmp/lib/customer_info.rb -rw-r--r-- 2.3 KiB
c7c48663Christopher Vollick Customer Info Forms and More Info 4 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
# frozen_string_literal: true

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

class PlanInfo
	def self.for(plan)
		return EMPromise.resolve(NoPlan.new) unless plan&.plan_name

		EMPromise.all([
			plan.activation_date,
			plan.auto_top_up_amount
		]).then do |adate, atua|
			new(
				plan: plan, start_date: adate,
				auto_top_up_amount: atua
			)
		end
	end

	class NoPlan
		def template
			FormTemplate.new("")
		end

		def admin_template
			FormTemplate.new("")
		end

		def status
			"Transitional"
		end
	end

	value_semantics do
		plan CustomerPlan
		start_date Time
		auto_top_up_amount Integer
	end

	def expires_at
		plan.expires_at
	end

	def template
		FormTemplate.for("plan_info", plan_info: self)
	end

	def admin_template
		FormTemplate.for("admin_plan_info", plan_info: self)
	end

	def monthly_price
		"$%.4f / month" % plan.monthly_price
	end

	def relative_start_date
		RelativeTime.in_words(start_date)
	end

	def formatted_start_date
		start_date.strftime("%Y-%m-%d %H:%M:%S")
	end

	def currency
		(plan.currency || "No Currency").to_s
	end

	def status
		plan.active? ? "Active" : "Expired"
	end
end

class CustomerInfo
	value_semantics do
		plan_info Either(PlanInfo, PlanInfo::NoPlan)
		tel Either(String, nil)
		balance BigDecimal
		cnam Either(String, nil)
	end

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

	def form
		FormTemplate.render("customer_info", info: self)
	end
end

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

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

	def form
		FormTemplate.render("admin_info", admin_info: self)
	end
end