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
# frozen_string_literal: true
require "bigdecimal"
require "forwardable"
require "relative_time"
require "value_semantics/monkey_patched"
require_relative "proxied_jid"
require_relative "customer_plan"
require_relative "form_template"
require_relative "promise_hash"
class PlanInfo
extend Forwardable
def_delegators :plan, :expires_at, :auto_top_up_amount
def self.for(plan)
return EMPromise.resolve(NoPlan.new) unless plan&.plan_name
plan.activation_date.then do |adate|
new(plan: plan, start_date: adate)
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
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)
PromiseHash.all(
plan_info: PlanInfo.for(plan),
tel: customer.registered? ? customer.registered?.phone : nil,
balance: customer.balance,
cnam: customer.tndetails.dig(:features, :lidb, :subscriber_information)
).then(&method(:new))
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)
PromiseHash.all(
jid: customer.jid,
customer_id: customer.customer_id,
fwd: customer.fwd,
info: CustomerInfo.for(customer, plan),
api: customer.api
).then(&method(:new))
end
def form
FormTemplate.render("admin_info", admin_info: self)
end
def tel_link
[
"https://dashboard.bandwidth.com/portal/r/a",
CONFIG[:creds][:account],
"numbers/details",
info.tel.gsub(/\A\+1/, "")
].join("/")
end
end