1# frozen_string_literal: true
2
3require "bigdecimal"
4require "value_semantics/monkey_patched"
5require_relative "proxied_jid"
6require_relative "customer_plan"
7
8class CustomerInfo
9 value_semantics do
10 plan CustomerPlan
11 auto_top_up_amount Integer
12 tel Either(String, nil)
13 balance BigDecimal
14 expires_at Either(Time, nil)
15 end
16
17 def self.for(customer, plan, expires_at)
18 plan.auto_top_up_amount.then do |auto_top_up_amount|
19 new(
20 plan: plan,
21 auto_top_up_amount: auto_top_up_amount,
22 tel: customer.registered? ? customer.regsitered?.phone : nil,
23 balance: customer.balance,
24 expires_at: expires_at
25 )
26 end
27 end
28
29 def account_status
30 if plan.plan_name.nil?
31 "Transitional"
32 elsif plan.active?
33 "Active"
34 else
35 "Expired"
36 end
37 end
38
39 def next_renewal
40 { var: "Next renewal", value: expires_at.strftime("%Y-%m-%d") } if expires_at
41 end
42
43 def monthly_amount
44 return unless plan.monthly_price
45 { var: "Renewal", value: "$%.4f / month" % plan.monthly_price }
46 end
47
48 def auto_top_up
49 {
50 label: "Auto Top-up"
51 }.merge(
52 if auto_top_up_amount.positive?
53 { value: auto_top_up_amount.to_s, var: "auto_top_up_amount" }
54 else
55 { value: "No" }
56 end
57 )
58 end
59
60 def fields
61 [
62 { var: "Account Status", value: account_status },
63 { var: "Phone Number", value: tel || "Not Registered" },
64 { var: "Balance", value: "$%.4f" % balance },
65 monthly_amount,
66 next_renewal,
67 auto_top_up,
68 { var: "Currency", value: (plan.currency || "No Currency").to_s }
69 ].compact
70 end
71end
72
73class AdminInfo
74 value_semantics do
75 jid ProxiedJID, coerce: ProxiedJID.method(:new)
76 customer_id String
77 info CustomerInfo
78 api API
79 end
80
81 def self.for(customer, plan, expires_at)
82 EMPromise.all([
83 CustomerInfo.for(customer, plan, expires_at),
84 customer.api
85 ]).then do |info, api_value|
86 new(
87 jid: customer.jid,
88 customer_id: customer.customer_id,
89 info: info, api: api_value
90 )
91 end
92 end
93
94 def fields
95 info.fields + [
96 { var: "JID", value: jid.unproxied.to_s },
97 { var: "Cheo JID", value: jid.to_s },
98 { var: "Customer ID", value: customer_id },
99 { var: "Plan", value: info.plan.plan_name || "No Plan" },
100 { var: "API", value: api.to_s }
101 ]
102 end
103end