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.registered?.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 return unless expires_at
41
42 { var: "Next renewal", value: expires_at.strftime("%Y-%m-%d") }
43 end
44
45 def monthly_amount
46 return unless plan.monthly_price
47
48 { var: "Renewal", value: "$%.4f / month" % plan.monthly_price }
49 end
50
51 def auto_top_up
52 {
53 label: "Auto Top-up"
54 }.merge(
55 if auto_top_up_amount.positive?
56 { value: auto_top_up_amount.to_s, var: "auto_top_up_amount" }
57 else
58 { value: "No" }
59 end
60 )
61 end
62
63 def fields
64 [
65 { var: "Account Status", value: account_status },
66 { var: "Phone Number", value: tel || "Not Registered" },
67 { var: "Balance", value: "$%.4f" % balance },
68 monthly_amount,
69 next_renewal,
70 auto_top_up,
71 { var: "Currency", value: (plan.currency || "No Currency").to_s }
72 ].compact
73 end
74end
75
76class AdminInfo
77 value_semantics do
78 jid ProxiedJID, coerce: ProxiedJID.method(:new)
79 customer_id String
80 info CustomerInfo
81 api API
82 end
83
84 def self.for(customer, plan, expires_at)
85 EMPromise.all([
86 CustomerInfo.for(customer, plan, expires_at),
87 customer.api
88 ]).then do |info, api_value|
89 new(
90 jid: customer.jid,
91 customer_id: customer.customer_id,
92 info: info, api: api_value
93 )
94 end
95 end
96
97 def fields
98 info.fields + [
99 { var: "JID", value: jid.unproxied.to_s },
100 { var: "Cheo JID", value: jid.to_s },
101 { var: "Customer ID", value: customer_id },
102 { var: "Plan", value: info.plan.plan_name || "No Plan" },
103 { var: "API", value: api.to_s }
104 ]
105 end
106end